@@ -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 ( ) ;
0 commit comments