Skip to content

Commit

Permalink
fixed that lifeCycleMgr should not invoke update if a gameObject is i…
Browse files Browse the repository at this point in the history
…nactive
  • Loading branch information
JasonXuDeveloper committed Sep 22, 2022
1 parent a4056a6 commit b4ef258
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ private class LifeCycleItem
{
public readonly ILTypeInstance ItemInstance;
public readonly MethodInfo Method;
public readonly Func<bool> ExecuteCondition;

public LifeCycleItem(ILTypeInstance itemInstance, MethodInfo method)
public LifeCycleItem(ILTypeInstance itemInstance, MethodInfo method, Func<bool> cond)
{
ItemInstance = itemInstance;
Method = method;
ExecuteCondition = cond;
}
}

Expand Down Expand Up @@ -113,7 +115,7 @@ public LifeCycleItem(ILTypeInstance itemInstance, MethodInfo method)
/// <param name="method"></param>
public void AddAwakeItem(ILTypeInstance instance, MethodInfo method)
{
_awakeItems.Add(new LifeCycleItem(instance, method));
_awakeItems.Add(new LifeCycleItem(instance, method, () => true));
_awakeObjs.Add(instance);
}

Expand All @@ -124,7 +126,7 @@ public void AddAwakeItem(ILTypeInstance instance, MethodInfo method)
/// <param name="method"></param>
public void AddOnEnableItem(ILTypeInstance instance, MethodInfo method)
{
_enableItems.Add(new LifeCycleItem(instance, method));
_enableItems.Add(new LifeCycleItem(instance, method, () => true));
_enableObjs.Add(instance);
}

Expand All @@ -135,7 +137,7 @@ public void AddOnEnableItem(ILTypeInstance instance, MethodInfo method)
/// <param name="method"></param>
public void AddStartItem(ILTypeInstance instance, MethodInfo method)
{
_startItems.Add(new LifeCycleItem(instance, method));
_startItems.Add(new LifeCycleItem(instance, method, () => true));
_startObjs.Add(instance);
}

Expand All @@ -144,9 +146,21 @@ public void AddStartItem(ILTypeInstance instance, MethodInfo method)
/// </summary>
/// <param name="instance"></param>
/// <param name="method"></param>
[Obsolete("Please provide a gameObject that holds this instance to be able to monitor whether or not it should update")]
public void AddUpdateItem(ILTypeInstance instance, MethodInfo method)
{
_updateItems.Add(new LifeCycleItem(instance, method));
_updateItems.Add(new LifeCycleItem(instance, method, () => (instance.GetGameObject().gameObject.activeInHierarchy)));
}

/// <summary>
/// Add update task
/// </summary>
/// <param name="instance"></param>
/// <param name="method"></param>
/// <param name="parent"></param>
public void AddUpdateItem(ILTypeInstance instance, MethodInfo method, GameObject parent)
{
_updateItems.Add(new LifeCycleItem(instance, method, () => parent.activeInHierarchy));
}

/// <summary>
Expand All @@ -163,9 +177,21 @@ public void RemoveUpdateItem(ILTypeInstance instance)
/// </summary>
/// <param name="instance"></param>
/// <param name="method"></param>
[Obsolete("Please provide a gameObject that holds this instance to be able to monitor whether or not it should lateUpdate")]
public void AddLateUpdateItem(ILTypeInstance instance, MethodInfo method)
{
_lateUpdateItems.Add(new LifeCycleItem(instance, method));
_lateUpdateItems.Add(new LifeCycleItem(instance, method, () => (instance.GetGameObject().gameObject.activeInHierarchy)));
}

/// <summary>
/// Add lateUpdate task
/// </summary>
/// <param name="instance"></param>
/// <param name="method"></param>
/// <param name="parent"></param>
public void AddLateUpdateItem(ILTypeInstance instance, MethodInfo method, GameObject parent)
{
_lateUpdateItems.Add(new LifeCycleItem(instance, method, () => parent.activeInHierarchy));
}

/// <summary>
Expand All @@ -182,9 +208,21 @@ public void RemoveLateUpdateItem(ILTypeInstance instance)
/// </summary>
/// <param name="instance"></param>
/// <param name="method"></param>
[Obsolete("Please provide a gameObject that holds this instance to be able to monitor whether or not it should fixedUpdate")]
public void AddFixedUpdateItem(ILTypeInstance instance, MethodInfo method)
{
_fixedUpdateItems.Add(new LifeCycleItem(instance, method));
_fixedUpdateItems.Add(new LifeCycleItem(instance, method, () => (instance.GetGameObject().gameObject.activeInHierarchy)));
}

/// <summary>
/// Add fixedUpdate task
/// </summary>
/// <param name="instance"></param>
/// <param name="method"></param>
/// <param name="parent"></param>
public void AddFixedUpdateItem(ILTypeInstance instance, MethodInfo method, GameObject parent)
{
_fixedUpdateItems.Add(new LifeCycleItem(instance, method, () => parent.activeInHierarchy));
}

/// <summary>
Expand Down Expand Up @@ -225,7 +263,7 @@ private void ExecuteItems(List<LifeCycleItem> items, bool removeAfterInvoke = tr
{
var item = items[i];
//忽略
if (ignoreCondition != null && ignoreCondition(item.ItemInstance))
if (ignoreCondition != null && ignoreCondition(item.ItemInstance)|| !item.ExecuteCondition())
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
LifeCycleMgr.Instance.AddOnEnableItem(instance, GetMethodInfo(type, "OnEnable"));
}
LifeCycleMgr.Instance.AddStartItem(instance, GetMethodInfo(type, "Start"));
LifeCycleMgr.Instance.AddFixedUpdateItem(instance, GetMethodInfo(type, "FixedUpdate"));
LifeCycleMgr.Instance.AddUpdateItem(instance, GetMethodInfo(type, "Update"));
LifeCycleMgr.Instance.AddLateUpdateItem(instance, GetMethodInfo(type, "LateUpdate"));
LifeCycleMgr.Instance.AddFixedUpdateItem(instance, GetMethodInfo(type, "FixedUpdate"), gameObject);
LifeCycleMgr.Instance.AddUpdateItem(instance, GetMethodInfo(type, "Update"), gameObject);
LifeCycleMgr.Instance.AddLateUpdateItem(instance, GetMethodInfo(type, "LateUpdate"), gameObject);

isAwaking = false;
awaked = true;
Expand Down
Binary file not shown.
Binary file not shown.
6 changes: 3 additions & 3 deletions UnityProject/Assets/Scripts/Adapters/MonoBehaviourAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ public async void Awake()
LifeCycleMgr.Instance.AddOnEnableItem(instance, GetMethodInfo(type, "OnEnable"));
}
LifeCycleMgr.Instance.AddStartItem(instance, GetMethodInfo(type, "Start"));
LifeCycleMgr.Instance.AddFixedUpdateItem(instance, GetMethodInfo(type, "FixedUpdate"));
LifeCycleMgr.Instance.AddUpdateItem(instance, GetMethodInfo(type, "Update"));
LifeCycleMgr.Instance.AddLateUpdateItem(instance, GetMethodInfo(type, "LateUpdate"));
LifeCycleMgr.Instance.AddFixedUpdateItem(instance, GetMethodInfo(type, "FixedUpdate"), gameObject);
LifeCycleMgr.Instance.AddUpdateItem(instance, GetMethodInfo(type, "Update"), gameObject);
LifeCycleMgr.Instance.AddLateUpdateItem(instance, GetMethodInfo(type, "LateUpdate"), gameObject);

isAwaking = false;
awaked = true;
Expand Down

0 comments on commit b4ef258

Please sign in to comment.