@@ -163,6 +163,7 @@ private void Awake()
163163 /// </summary>
164164 private void OnDestroy ( )
165165 {
166+ ExecuteItems ( _onDestroyTaskItems ) ;
166167 UnsafeUtility . Free ( ItemList , Allocator . Persistent ) ;
167168 UnsafeUtility . Free ( UsageList , Allocator . Persistent ) ;
168169 GC . RemoveMemoryPressure ( sizeof ( LifeCycleItem ) * MaxSize ) ;
@@ -204,6 +205,11 @@ private void OnDestroy()
204205 /// </summary>
205206 private readonly List < IntPtr > _onceTaskItems = new List < IntPtr > ( 100 ) ;
206207
208+ /// <summary>
209+ /// All on destory task methods
210+ /// </summary>
211+ private readonly List < IntPtr > _onDestroyTaskItems = new List < IntPtr > ( 100 ) ;
212+
207213 /// <summary>
208214 /// no gc search for awake objs
209215 /// </summary>
@@ -287,7 +293,8 @@ public void AddUpdateItem(object instance, MethodInfo method)
287293 /// <param name="method"></param>
288294 /// <param name="parent"></param>
289295 /// <param name="cond"></param>
290- public void AddUpdateItem < T > ( T instance , MethodInfo method , GameObject parent , Func < bool > cond = null ) where T : class
296+ public void AddUpdateItem < T > ( T instance , MethodInfo method , GameObject parent , Func < bool > cond = null )
297+ where T : class
291298 {
292299 void * ptr = UnsafeUtility . PinGCObjectAndGetAddress ( instance , out var address ) ;
293300 _updateItems . Add ( GetLifeCycleItem ( in ptr , in address ,
@@ -362,11 +369,12 @@ public void AddLateUpdateItem(object instance, MethodInfo method)
362369 /// <param name="method"></param>
363370 /// <param name="parent"></param>
364371 /// <param name="cond"></param>
365- public void AddLateUpdateItem < T > ( T instance , MethodInfo method , GameObject parent , Func < bool > cond = null ) where T : class
372+ public void AddLateUpdateItem < T > ( T instance , MethodInfo method , GameObject parent , Func < bool > cond = null )
373+ where T : class
366374 {
367375 void * ptr = UnsafeUtility . PinGCObjectAndGetAddress ( instance , out var address ) ;
368376 _lateUpdateItems . Add ( GetLifeCycleItem ( in ptr , in address ,
369- ( ) => method ? . Invoke ( instance , ConstMgr . NullObjects ) ,
377+ ( ) => method ? . Invoke ( instance , ConstMgr . NullObjects ) ,
370378 ( ) => cond == null ? parent . activeInHierarchy : parent . activeInHierarchy && cond . Invoke ( ) ) ) ;
371379 }
372380
@@ -408,11 +416,12 @@ public void AddFixedUpdateItem(object instance, MethodInfo method)
408416 /// <param name="method"></param>
409417 /// <param name="parent"></param>
410418 /// <param name="cond"></param>
411- public void AddFixedUpdateItem < T > ( T instance , MethodInfo method , GameObject parent , Func < bool > cond = null ) where T : class
419+ public void AddFixedUpdateItem < T > ( T instance , MethodInfo method , GameObject parent , Func < bool > cond = null )
420+ where T : class
412421 {
413422 void * ptr = UnsafeUtility . PinGCObjectAndGetAddress ( instance , out var address ) ;
414423 _fixedUpdateItems . Add ( GetLifeCycleItem ( in ptr , in address ,
415- ( ) => method ? . Invoke ( instance , ConstMgr . NullObjects ) ,
424+ ( ) => method ? . Invoke ( instance , ConstMgr . NullObjects ) ,
416425 ( ) => cond == null ? parent . activeInHierarchy : parent . activeInHierarchy && cond . Invoke ( ) ) ) ;
417426 }
418427
@@ -465,7 +474,8 @@ public Guid AddTask(Action action, Func<bool> condition)
465474 /// <param name="instance"></param>
466475 /// <param name="action"></param>
467476 /// <returns></returns>
468- public void AddTask < T > ( T instance , Action action ) => AddTask ( action , ( ) => true ) ;
477+ public void AddTask < T > ( T instance , Action action ) where T : class
478+ => AddTask ( instance , action , ( ) => true ) ;
469479
470480 /// <summary>
471481 /// Add a task that will call once in the main thread when condition is true
@@ -480,6 +490,55 @@ public void AddTask<T>(T instance, Action action, Func<bool> condition) where T
480490 _onceTaskItems . Add ( GetLifeCycleItem ( in ptr , in address , action , condition ) ) ;
481491 }
482492
493+ /// <summary>
494+ /// Add a task that will call once in the main thread when application is quitting
495+ /// </summary>
496+ /// <param name="action"></param>
497+ /// <returns></returns>
498+ public Guid AddOnDestroyTask ( Action action ) => AddOnDestroyTask ( action , ( ) => true ) ;
499+
500+ /// <summary>
501+ /// Add a task that will call once in the main thread when condition is true when application is quitting
502+ /// </summary>
503+ /// <param name="action"></param>
504+ /// <param name="condition"></param>
505+ /// <returns></returns>
506+ public Guid AddOnDestroyTask ( Action action , Func < bool > condition )
507+ {
508+ Guid guid = Guid . NewGuid ( ) ;
509+ var guidIdent = new IntPtr ( guid . GetHashCode ( ) ) ;
510+ while ( _onDestroyTaskItems . Exists ( i => ( ( LifeCycleItem * ) i ) ->InstancePtr == guidIdent ) )
511+ {
512+ guid = Guid . NewGuid ( ) ;
513+ guidIdent = new IntPtr ( guid . GetHashCode ( ) ) ;
514+ }
515+
516+ _onDestroyTaskItems . Add ( GetLifeCycleItem ( ( void * ) guidIdent , 0 , action , condition ) ) ;
517+ return guid ;
518+ }
519+
520+ /// <summary>
521+ /// Add a task that will call once in the main thread when application is quitting
522+ /// </summary>
523+ /// <param name="instance"></param>
524+ /// <param name="action"></param>
525+ /// <returns></returns>
526+ public void AddOnDestroyTask < T > ( T instance , Action action ) where T : class
527+ => AddOnDestroyTask ( instance , action , ( ) => true ) ;
528+
529+ /// <summary>
530+ /// Add a task that will call once in the main thread when condition is true when application is quitting
531+ /// </summary>
532+ /// <param name="instance"></param>
533+ /// <param name="action"></param>
534+ /// <param name="condition"></param>
535+ /// <returns></returns>
536+ public void AddOnDestroyTask < T > ( T instance , Action action , Func < bool > condition ) where T : class
537+ {
538+ void * ptr = UnsafeUtility . PinGCObjectAndGetAddress ( instance , out var address ) ;
539+ _onDestroyTaskItems . Add ( GetLifeCycleItem ( in ptr , in address , action , condition ) ) ;
540+ }
541+
483542 /// <summary>
484543 /// Remove a task that will call once in the main thread
485544 /// </summary>
@@ -717,7 +776,7 @@ private void LateUpdate()
717776 _instances . Add ( item ->InstancePtr ) ;
718777 }
719778 }
720-
779+
721780 //调用start,并记录本帧处理的对象
722781 ExecuteItems ( _startItems , true , InstancesContains ,
723782 iterate : il => _startObjs . Remove ( il . InstancePtr ) ) ;
@@ -731,7 +790,7 @@ private void LateUpdate()
731790 item = ( LifeCycleItem * ) _startItems [ i ] ;
732791 _instances . Add ( item ->InstancePtr ) ;
733792 }
734-
793+
735794 ExecuteItems ( _startItems , iterate : il => _startObjs . Remove ( il . InstancePtr ) ) ;
736795 }
737796 }
0 commit comments