Skip to content

Commit

Permalink
allow jbehaviour call end while application quitting
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonXuDeveloper committed Oct 20, 2023
1 parent dda6391 commit 3792bd4
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ private void Awake()
/// </summary>
private void OnDestroy()
{
ExecuteItems(_onDestroyTaskItems);
UnsafeUtility.Free(ItemList, Allocator.Persistent);
UnsafeUtility.Free(UsageList, Allocator.Persistent);
GC.RemoveMemoryPressure(sizeof(LifeCycleItem) * MaxSize);
Expand Down Expand Up @@ -204,6 +205,11 @@ private void OnDestroy()
/// </summary>
private readonly List<IntPtr> _onceTaskItems = new List<IntPtr>(100);

/// <summary>
/// All on destory task methods
/// </summary>
private readonly List<IntPtr> _onDestroyTaskItems = new List<IntPtr>(100);

/// <summary>
/// no gc search for awake objs
/// </summary>
Expand Down Expand Up @@ -287,7 +293,8 @@ public void AddUpdateItem(object instance, MethodInfo method)
/// <param name="method"></param>
/// <param name="parent"></param>
/// <param name="cond"></param>
public void AddUpdateItem<T>(T instance, MethodInfo method, GameObject parent, Func<bool> cond = null) where T : class
public void AddUpdateItem<T>(T instance, MethodInfo method, GameObject parent, Func<bool> cond = null)
where T : class
{
void* ptr = UnsafeUtility.PinGCObjectAndGetAddress(instance, out var address);
_updateItems.Add(GetLifeCycleItem(in ptr, in address,
Expand Down Expand Up @@ -362,11 +369,12 @@ public void AddLateUpdateItem(object instance, MethodInfo method)
/// <param name="method"></param>
/// <param name="parent"></param>
/// <param name="cond"></param>
public void AddLateUpdateItem<T>(T instance, MethodInfo method, GameObject parent, Func<bool> cond = null) where T : class
public void AddLateUpdateItem<T>(T instance, MethodInfo method, GameObject parent, Func<bool> cond = null)
where T : class
{
void* ptr = UnsafeUtility.PinGCObjectAndGetAddress(instance, out var address);
_lateUpdateItems.Add(GetLifeCycleItem(in ptr, in address,
() => method?.Invoke(instance, ConstMgr.NullObjects),
() => method?.Invoke(instance, ConstMgr.NullObjects),
() => cond == null ? parent.activeInHierarchy : parent.activeInHierarchy && cond.Invoke()));
}

Expand Down Expand Up @@ -408,11 +416,12 @@ public void AddFixedUpdateItem(object instance, MethodInfo method)
/// <param name="method"></param>
/// <param name="parent"></param>
/// <param name="cond"></param>
public void AddFixedUpdateItem<T>(T instance, MethodInfo method, GameObject parent, Func<bool> cond = null) where T : class
public void AddFixedUpdateItem<T>(T instance, MethodInfo method, GameObject parent, Func<bool> cond = null)
where T : class
{
void* ptr = UnsafeUtility.PinGCObjectAndGetAddress(instance, out var address);
_fixedUpdateItems.Add(GetLifeCycleItem(in ptr, in address,
() => method?.Invoke(instance, ConstMgr.NullObjects),
() => method?.Invoke(instance, ConstMgr.NullObjects),
() => cond == null ? parent.activeInHierarchy : parent.activeInHierarchy && cond.Invoke()));
}

Expand Down Expand Up @@ -465,7 +474,8 @@ public Guid AddTask(Action action, Func<bool> condition)
/// <param name="instance"></param>
/// <param name="action"></param>
/// <returns></returns>
public void AddTask<T>(T instance, Action action) => AddTask(action, () => true);
public void AddTask<T>(T instance, Action action) where T : class
=> AddTask(instance, action, () => true);

/// <summary>
/// Add a task that will call once in the main thread when condition is true
Expand All @@ -480,6 +490,55 @@ public void AddTask<T>(T instance, Action action, Func<bool> condition) where T
_onceTaskItems.Add(GetLifeCycleItem(in ptr, in address, action, condition));
}

/// <summary>
/// Add a task that will call once in the main thread when application is quitting
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
public Guid AddOnDestroyTask(Action action) => AddOnDestroyTask(action, () => true);

/// <summary>
/// Add a task that will call once in the main thread when condition is true when application is quitting
/// </summary>
/// <param name="action"></param>
/// <param name="condition"></param>
/// <returns></returns>
public Guid AddOnDestroyTask(Action action, Func<bool> condition)
{
Guid guid = Guid.NewGuid();
var guidIdent = new IntPtr(guid.GetHashCode());
while (_onDestroyTaskItems.Exists(i => ((LifeCycleItem*)i)->InstancePtr == guidIdent))
{
guid = Guid.NewGuid();
guidIdent = new IntPtr(guid.GetHashCode());
}

_onDestroyTaskItems.Add(GetLifeCycleItem((void*)guidIdent, 0, action, condition));
return guid;
}

/// <summary>
/// Add a task that will call once in the main thread when application is quitting
/// </summary>
/// <param name="instance"></param>
/// <param name="action"></param>
/// <returns></returns>
public void AddOnDestroyTask<T>(T instance, Action action) where T : class
=> AddOnDestroyTask(instance, action, () => true);

/// <summary>
/// Add a task that will call once in the main thread when condition is true when application is quitting
/// </summary>
/// <param name="instance"></param>
/// <param name="action"></param>
/// <param name="condition"></param>
/// <returns></returns>
public void AddOnDestroyTask<T>(T instance, Action action, Func<bool> condition) where T : class
{
void* ptr = UnsafeUtility.PinGCObjectAndGetAddress(instance, out var address);
_onDestroyTaskItems.Add(GetLifeCycleItem(in ptr, in address, action, condition));
}

/// <summary>
/// Remove a task that will call once in the main thread
/// </summary>
Expand Down Expand Up @@ -717,7 +776,7 @@ private void LateUpdate()
_instances.Add(item->InstancePtr);
}
}

//调用start,并记录本帧处理的对象
ExecuteItems(_startItems, true, InstancesContains,
iterate: il => _startObjs.Remove(il.InstancePtr));
Expand All @@ -731,7 +790,7 @@ private void LateUpdate()
item = (LifeCycleItem*)_startItems[i];
_instances.Add(item->InstancePtr);
}

ExecuteItems(_startItems, iterate: il => _startObjs.Remove(il.InstancePtr));
}
}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
16 changes: 11 additions & 5 deletions UnityProject/HotUpdateScripts/JEngine/Core/JBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ static JBehaviour()
{
CoroutineMgr.Instance.StartCoroutine(JBehavioursLoop());
});
LifeCycleMgr.Instance.AddOnDestroyTask(() =>
{
foreach (var jb in JBehaviours.Values.ToList())
{
if (!jb._hidden && !jb._paused)
{
jb.Destroy();
}
}
});
}

/// <summary>
Expand All @@ -86,7 +96,6 @@ private static IEnumerator JBehavioursLoop()
Stopwatch sw = new Stopwatch();
for (;;)
{
if (!Application.isPlaying) break;
yield return ConstMgr.WaitFor1Sec;
int cnt = LoopJBehaviours.Count;
for (int i = 0; i < cnt; i++)
Expand Down Expand Up @@ -575,10 +584,7 @@ private void Destroy()
JBehaviours.Remove(_instanceID);
GameObjectJBehaviours.Remove(_gameObject);
_gameObject = null;
if (Application.isPlaying)
{
End();
}
End();
}


Expand Down

0 comments on commit 3792bd4

Please sign in to comment.