Skip to content

Commit 77e8f2b

Browse files
committed
Fantasy.Unity 增加LateUpdateSystem系统,对应的Unity的LateUpdate。
1 parent 73fbe9e commit 77e8f2b

File tree

30 files changed

+629
-169
lines changed

30 files changed

+629
-169
lines changed

‎Fantasy.Net/Fantasy.Net/Runtime/Core/Entitas/Component/EntityComponent.cs‎

Lines changed: 91 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,15 @@ public override int GetHashCode()
6767
return HashCode.Combine(CustomEventType, EntitiesType);
6868
}
6969
}
70-
70+
7171
/// <summary>
7272
/// Entity管理组件
7373
/// </summary>
74+
#if FANTASY_UNITY
75+
public sealed class EntityComponent : Entity, ISceneUpdate, ISceneLateUpdate, IAssembly
76+
#else
7477
public sealed class EntityComponent : Entity, ISceneUpdate, IAssembly
78+
#endif
7579
{
7680
private readonly OneToManyList<long, Type> _assemblyList = new();
7781
private readonly OneToManyList<long, Type> _assemblyHashCodes = new();
@@ -80,15 +84,19 @@ public sealed class EntityComponent : Entity, ISceneUpdate, IAssembly
8084
private readonly Dictionary<Type, IUpdateSystem> _updateSystems = new();
8185
private readonly Dictionary<Type, IDestroySystem> _destroySystems = new();
8286
private readonly Dictionary<Type, IDeserializeSystem> _deserializeSystems = new();
83-
private readonly Dictionary<Type, IFrameUpdateSystem> _frameUpdateSystem = new();
8487

8588
private readonly OneToManyList<long, CustomEntitiesSystemKey> _assemblyCustomSystemList = new();
8689
private readonly Dictionary<CustomEntitiesSystemKey, ICustomEntitiesSystem> _customEntitiesSystems = new Dictionary<CustomEntitiesSystemKey, ICustomEntitiesSystem>();
8790

8891
private readonly Dictionary<Type, long> _hashCodes = new Dictionary<Type, long>();
8992
private readonly Queue<UpdateQueueInfo> _updateQueue = new Queue<UpdateQueueInfo>();
90-
private readonly Queue<FrameUpdateQueueInfo> _frameUpdateQueue = new Queue<FrameUpdateQueueInfo>();
93+
9194
private readonly Dictionary<long, UpdateQueueInfo> _updateQueueDic = new Dictionary<long, UpdateQueueInfo>();
95+
#if FANTASY_UNITY
96+
private readonly Dictionary<Type, ILateUpdateSystem> _lateUpdateSystems = new();
97+
private readonly Queue<UpdateQueueInfo> _lateUpdateQueue = new Queue<UpdateQueueInfo>();
98+
private readonly Dictionary<long, UpdateQueueInfo> _lateUpdateQueueDic = new Dictionary<long, UpdateQueueInfo>();
99+
#endif
92100

93101
internal async FTask<EntityComponent> Initialize()
94102
{
@@ -172,12 +180,14 @@ private void LoadInner(long assemblyIdentity)
172180
_updateSystems.Add(entitiesType, iUpdateSystem);
173181
break;
174182
}
175-
case IFrameUpdateSystem iFrameUpdateSystem:
183+
#if FANTASY_UNITY
184+
case ILateUpdateSystem iLateUpdateSystem:
176185
{
177-
entitiesType = iFrameUpdateSystem.EntitiesType();
178-
_frameUpdateSystem.Add(entitiesType, iFrameUpdateSystem);
186+
entitiesType = iLateUpdateSystem.EntitiesType();
187+
_lateUpdateSystems.Add(entitiesType, iLateUpdateSystem);
179188
break;
180-
}
189+
}
190+
#endif
181191
default:
182192
{
183193
Log.Error($"IEntitiesSystem not support type {entitiesSystemType}");
@@ -216,10 +226,12 @@ private void OnUnLoadInner(long assemblyIdentity)
216226
_awakeSystems.Remove(type);
217227
_updateSystems.Remove(type);
218228
_destroySystems.Remove(type);
229+
#if FANTASY_UNITY
230+
_lateUpdateSystems.Remove(type);
231+
#endif
219232
_deserializeSystems.Remove(type);
220-
_frameUpdateSystem.Remove(type);
221233
}
222-
234+
223235
_assemblyList.RemoveByKey(assemblyIdentity);
224236
}
225237

@@ -329,29 +341,26 @@ public void CustomSystem(Entity entity, int customEventType)
329341
#region Update
330342

331343
/// <summary>
332-
/// 将实体加入更���队列,准备进行更新
344+
/// 将实体加入Update队列,准备进行Update
333345
/// </summary>
334346
/// <param name="entity">实体对象</param>
335347
public void StartUpdate(Entity entity)
336348
{
337349
var type = entity.Type;
338350
var entityRuntimeId = entity.RuntimeId;
339351

340-
if (_updateSystems.ContainsKey(type))
341-
{
342-
var updateQueueInfo = new UpdateQueueInfo(type, entityRuntimeId);
343-
_updateQueue.Enqueue(updateQueueInfo);
344-
_updateQueueDic.Add(entityRuntimeId, updateQueueInfo);
345-
}
346-
347-
if (_frameUpdateSystem.ContainsKey(type))
352+
if (!_updateSystems.ContainsKey(type))
348353
{
349-
_frameUpdateQueue.Enqueue(new FrameUpdateQueueInfo(type, entityRuntimeId));
354+
return;
350355
}
356+
357+
var updateQueueInfo = new UpdateQueueInfo(type, entityRuntimeId);
358+
_updateQueue.Enqueue(updateQueueInfo);
359+
_updateQueueDic.Add(entityRuntimeId, updateQueueInfo);
351360
}
352361

353362
/// <summary>
354-
/// 停止实体进行更新
363+
/// 停止实体Update
355364
/// </summary>
356365
/// <param name="entity">实体对象</param>
357366
public void StopUpdate(Entity entity)
@@ -365,7 +374,7 @@ public void StopUpdate(Entity entity)
365374
}
366375

367376
/// <summary>
368-
/// 执行实体系统的更新逻辑
377+
/// 执行实体系统的Update
369378
/// </summary>
370379
public void Update()
371380
{
@@ -406,44 +415,84 @@ public void Update()
406415
}
407416
}
408417

418+
#endregion
419+
420+
#if FANTASY_UNITY
421+
#region LateUpdate
422+
423+
/// <summary>
424+
/// 将实体加入LateUpdate队列,准备进行LateUpdate
425+
/// </summary>
426+
/// <param name="entity">实体对象</param>
427+
public void StartLateUpdate(Entity entity)
428+
{
429+
var type = entity.Type;
430+
var entityRuntimeId = entity.RuntimeId;
431+
432+
if (!_lateUpdateSystems.ContainsKey(type))
433+
{
434+
return;
435+
}
436+
437+
var updateQueueInfo = new UpdateQueueInfo(type, entityRuntimeId);
438+
_lateUpdateQueue.Enqueue(updateQueueInfo);
439+
_lateUpdateQueueDic.Add(entityRuntimeId, updateQueueInfo);
440+
}
441+
409442
/// <summary>
410-
/// 执行实体系统的帧更新逻辑
443+
/// 停止实体进行LateUpdate
411444
/// </summary>
412-
public void FrameUpdate()
445+
/// <param name="entity">实体对象</param>
446+
public void StopLateUpdate(Entity entity)
447+
{
448+
if (!_lateUpdateQueueDic.Remove(entity.RuntimeId, out var updateQueueInfo))
449+
{
450+
return;
451+
}
452+
453+
updateQueueInfo.IsStop = true;
454+
}
455+
456+
public void LateUpdate()
413457
{
414-
var count = _frameUpdateQueue.Count;
458+
var lateUpdateQueue = _lateUpdateQueue.Count;
415459

416-
while (count-- > 0)
460+
while (lateUpdateQueue-- > 0)
417461
{
418-
var frameUpdateQueueStruct = _frameUpdateQueue.Dequeue();
462+
var lateUpdateQueueStruct = _lateUpdateQueue.Dequeue();
463+
464+
if (lateUpdateQueueStruct.IsStop)
465+
{
466+
continue;
467+
}
419468

420-
if (!_frameUpdateSystem.TryGetValue(frameUpdateQueueStruct.Type, out var frameUpdateSystem))
469+
if (!_lateUpdateSystems.TryGetValue(lateUpdateQueueStruct.Type, out var lateUpdateSystem))
421470
{
422471
continue;
423472
}
424473

425-
var entity = Scene.GetEntity(frameUpdateQueueStruct.RunTimeId);
426-
474+
var entity = Scene.GetEntity(lateUpdateQueueStruct.RunTimeId);
475+
427476
if (entity == null || entity.IsDisposed)
428477
{
478+
_lateUpdateQueueDic.Remove(lateUpdateQueueStruct.RunTimeId);
429479
continue;
430480
}
431-
432-
_frameUpdateQueue.Enqueue(frameUpdateQueueStruct);
433-
481+
482+
_lateUpdateQueue.Enqueue(lateUpdateQueueStruct);
483+
434484
try
435485
{
436-
frameUpdateSystem.Invoke(entity);
486+
lateUpdateSystem.Invoke(entity);
437487
}
438488
catch (Exception e)
439489
{
440-
Log.Error($"{frameUpdateQueueStruct.Type.FullName} FrameUpdate Error {e}");
490+
Log.Error($"{lateUpdateQueueStruct.Type.FullName} Update Error {e}");
441491
}
442492
}
443493
}
444-
445494
#endregion
446-
495+
#endif
447496
public long GetHashCode(Type type)
448497
{
449498
return _hashCodes[type];
@@ -455,14 +504,17 @@ public long GetHashCode(Type type)
455504
public override void Dispose()
456505
{
457506
_updateQueue.Clear();
458-
_frameUpdateQueue.Clear();
459-
507+
_updateQueueDic.Clear();
508+
#if FANTASY_UNITY
509+
_lateUpdateQueue.Clear();
510+
_lateUpdateQueueDic.Clear();
511+
_lateUpdateSystems.Clear();
512+
#endif
460513
_assemblyList.Clear();
461514
_awakeSystems.Clear();
462515
_updateSystems.Clear();
463516
_destroySystems.Clear();
464517
_deserializeSystems.Clear();
465-
_frameUpdateSystem.Clear();
466518

467519
AssemblySystem.UnRegister(this);
468520
base.Dispose();

‎Fantasy.Net/Fantasy.Net/Runtime/Core/Entitas/Entity.cs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ public static Entity Create(Scene scene, Type type, long id, bool isPool, bool i
169169
{
170170
scene.EntityComponent.Awake(entity);
171171
scene.EntityComponent.StartUpdate(entity);
172+
#if FANTASY_UNITY
173+
scene.EntityComponent.StartLateUpdate(entity);
174+
#endif
172175
}
173176

174177
return entity;
@@ -210,6 +213,9 @@ public static Entity Create(Scene scene, Type type, long id, bool isPool, bool i
210213
{
211214
scene.EntityComponent.Awake(entity);
212215
scene.EntityComponent.StartUpdate(entity);
216+
#if FANTASY_UNITY
217+
scene.EntityComponent.StartLateUpdate(entity);
218+
#endif
213219
}
214220

215221
return entity;
@@ -232,6 +238,9 @@ public static Entity Create(Scene scene, Type type, long id, bool isPool, bool i
232238
AddComponent(entity);
233239
Scene.EntityComponent.Awake(entity);
234240
Scene.EntityComponent.StartUpdate(entity);
241+
#if FANTASY_UNITY
242+
Scene.EntityComponent.StartLateUpdate(entity);
243+
#endif
235244
return entity;
236245
}
237246

@@ -248,6 +257,9 @@ public static Entity Create(Scene scene, Type type, long id, bool isPool, bool i
248257
AddComponent(entity);
249258
Scene.EntityComponent.Awake(entity);
250259
Scene.EntityComponent.StartUpdate(entity);
260+
#if FANTASY_UNITY
261+
Scene.EntityComponent.StartLateUpdate(entity);
262+
#endif
251263
return entity;
252264
}
253265

@@ -406,6 +418,9 @@ public Entity AddComponent(Type type, bool isPool = true)
406418
AddComponent(entity);
407419
Scene.EntityComponent.Awake(entity);
408420
Scene.EntityComponent.StartUpdate(entity);
421+
#if FANTASY_UNITY
422+
Scene.EntityComponent.StartLateUpdate(entity);
423+
#endif
409424
return entity;
410425
}
411426

Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
1+
#if FANTASY_UNITY
12
using System;
23

34
namespace Fantasy.Entitas.Interface
45
{
5-
internal interface IFrameUpdateSystem : IEntitiesSystem { }
6+
internal interface ILateUpdateSystem : IEntitiesSystem { }
7+
68
/// <summary>
7-
/// 帧更新时间的抽象接口
9+
/// 实体的LateUpdate事件的抽象接口
810
/// </summary>
9-
/// <typeparam name="T"></typeparam>
10-
public abstract class FrameUpdateSystem<T> : IFrameUpdateSystem where T : Entity
11+
/// <typeparam name="T">实体的泛型类型</typeparam>
12+
public abstract class LateUpdateSystem<T> : ILateUpdateSystem where T : Entity
1113
{
1214
/// <summary>
1315
/// 实体的类型
1416
/// </summary>
1517
/// <returns></returns>
1618
public Type EntitiesType() => typeof(T);
19+
1720
/// <summary>
1821
/// 事件的抽象方法,需要自己实现这个方法
1922
/// </summary>
2023
/// <param name="self">触发事件的实体实例</param>
21-
protected abstract void FrameUpdate(T self);
24+
protected abstract void LateUpdate(T self);
25+
2226
/// <summary>
23-
/// 框架内部调用的触发FrameUpdate的方法
27+
/// 框架内部调用的触发Awake的方法。
2428
/// </summary>
25-
/// <param name="self"></param>
29+
/// <param name="self">触发事件的实体实例</param>
2630
public void Invoke(Entity self)
2731
{
28-
FrameUpdate((T) self);
32+
LateUpdate((T)self);
2933
}
3034
}
31-
}
35+
}
36+
#endif

‎Fantasy.Net/Fantasy.Net/Runtime/Core/Platform/Unity/Entry.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#if FANTASY_UNITY
2+
using System;
23
using System.Linq;
34
using Fantasy.Assembly;
45
using Fantasy.Async;
@@ -86,6 +87,11 @@ private void Update()
8687
ThreadScheduler.Update();
8788
}
8889

90+
private void LateUpdate()
91+
{
92+
ThreadScheduler.LateUpdate();
93+
}
94+
8995
private void OnDestroy()
9096
{
9197
AssemblySystem.Dispose();

‎Fantasy.Net/Fantasy.Net/Runtime/Core/Scene/ISceneUpdate.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,23 @@ internal sealed class EmptySceneUpdate : ISceneUpdate
99
{
1010
public void Update()
1111
{
12+
13+
}
14+
}
15+
16+
#if FANTASY_UNITY
17+
internal interface ISceneLateUpdate
18+
{
19+
void LateUpdate();
20+
}
21+
22+
internal sealed class EmptySceneLateUpdate : ISceneLateUpdate
23+
{
24+
public void LateUpdate()
25+
{
1226

1327
}
1428
}
29+
#endif
30+
1531
}

0 commit comments

Comments
 (0)