Entities 结构

PlayerLoop

命名空间:UnityEngine.LowLevel dll:UnityEngine.CoreModule.dll

引擎默认PlayerLoopSystem

  • 1.UnityEngine.PlayerLoop.TimeUpdate
  • 2.UnityEngine.PlayerLoop.Initialization
  • 3.UnityEngine.PlayerLoop.EarlyUpdate
  • 4.UnityEngine.PlayerLoop.FixedUpdate
  • 5.UnityEngine.PlayerLoop.PreUpdate
  • 6.UnityEngine.PlayerLoop.Update
  • 7.UnityEngine.PlayerLoop.PreLateUpdate
  • 8.UnityEngine.PlayerLoop.PostLateUpdate

对比MonoBehaviour执行顺序

MonoBehaviour.Awake/OnEnable 捆绑立即执行。

  • 1.UnityEngine.PlayerLoop.Initialization

  • 2.UnityEngine.PlayerLoop.EarlyUpdate

  • a.MonoBehaviour.Start // 或无

  • 3.UnityEngine.PlayerLoop.FixedUpdate

  • b.MonoBehaviour.FixedUpdate // 或无

  • 4.UnityEngine.PlayerLoop.PreUpdate

  • 5.UnityEngine.PlayerLoop.Update

  • c.MonoBehaviour.Update // 或无

  • 6.UnityEngine.PlayerLoop.PreLateUpdate

  • d.MonoBehaviour.PlayerLoop.LateUpdate// 或无

  • 7.UnityEngine.PlayerLoop.PostLateUpdate

  • 8.UnityEngine.PlayerLoop.TimeUpdate

可以看出对应的相同函数都优先于mono的对应函数。 常用的System类型:Initialization,Update,PreLateUpdate

PlayerLoopSystem 原型:

namespace UnityEngine.LowLevel
{
    //
    // 摘要:
    //     The representation of a single system being updated by the player loop in Unity.
    [MovedFrom("UnityEngine.Experimental.LowLevel")]
    public struct PlayerLoopSystem
    {
        public Type type;
        public PlayerLoopSystem[] subSystemList;
        public UpdateFunction updateDelegate;
        public IntPtr updateFunction;
        public IntPtr loopConditionFunction;

        public override string ToString();

        public delegate void UpdateFunction();
    }
}

PlayerLoop 的简单测试使用

1.直接注册方法

internal class PlayerLoopSystemFunctionTest
{ 
    internal void UnityEngine_PlayerLoop_TimeUpdate(){

        Debug.Log("UnityEngine_PlayerLoop_TimeUpdate  excuted" + ",Frame:"+Time.frameCount);
    }

    internal void UnityEngine_PlayerLoop_Initialization(){

        Debug.Log("UnityEngine_PlayerLoop_Initialization  excuted" + ",Frame:"+Time.frameCount);
    }

    internal void UnityEngine_PlayerLoop_EarlyUpdate(){

        Debug.Log("UnityEngine_PlayerLoop_EarlyUpdate  excuted" + ",Frame:"+Time.frameCount);
    }

    internal void UnityEngine_PlayerLoop_FixedUpdate(){

        Debug.Log("UnityEngine_PlayerLoop_FixedUpdate  excuted" + ",Frame:"+Time.frameCount);
    }

    internal void UnityEngine_PlayerLoop_PreUpdate(){

        Debug.Log("UnityEngine_PlayerLoop_PreUpdate  excuted" + ",Frame:"+Time.frameCount);
    }

    internal void UnityEngine_PlayerLoop_Update(){

        Debug.Log("UnityEngine_PlayerLoop_Update  excuted" + ",Frame:"+Time.frameCount);
    }

    internal void UnityEngine_PlayerLoop_PreLateUpdate(){

        Debug.Log("UnityEngine_PlayerLoop_PreLateUpdate  excuted" + ",Frame:"+Time.frameCount);
    }

    
    internal void UnityEngine_PlayerLoop_PostLateUpdate(){

        Debug.Log("UnityEngine_PlayerLoop_PostLateUpdate  excuted" + ",Frame:"+Time.frameCount);
    }

}

注册添加代码

PlayerLoopSystem rootPlayerLoopSystem = PlayerLoop.GetDefaultPlayerLoop();

rootPlayerLoopSystem.subSystemList[0].updateDelegate = functionTest.UnityEngine_PlayerLoop_TimeUpdate;
rootPlayerLoopSystem.subSystemList[1].updateDelegate = functionTest.UnityEngine_PlayerLoop_Initialization;
rootPlayerLoopSystem.subSystemList[2].updateDelegate = functionTest.UnityEngine_PlayerLoop_EarlyUpdate;
rootPlayerLoopSystem.subSystemList[3].updateDelegate = functionTest.UnityEngine_PlayerLoop_FixedUpdate;
rootPlayerLoopSystem.subSystemList[4].updateDelegate = functionTest.UnityEngine_PlayerLoop_PreUpdate;
rootPlayerLoopSystem.subSystemList[5].updateDelegate = functionTest.UnityEngine_PlayerLoop_Update;
rootPlayerLoopSystem.subSystemList[6].updateDelegate = functionTest.UnityEngine_PlayerLoop_PreLateUpdate;
rootPlayerLoopSystem.subSystemList[7].updateDelegate = functionTest.UnityEngine_PlayerLoop_PostLateUpdate;

PlayerLoop.SetPlayerLoop(rootPlayerLoopSystem);

2.注册ubSystem

// **rootPlayerLoopSystem 的type 为null。**
//playerLoopSystemType 为引擎默认`PlayerLoopSystem`的类型之一。
static bool AppendSystemToPlayerLoopListImpl(ComponentSystemBase system, ref PlayerLoopSystem playerLoop,
    Type playerLoopSystemType)
{
    if (playerLoop.type == playerLoopSystemType)
    {
        var del = new DummyDelegateWrapper(system);
        int oldListLength = (playerLoop.subSystemList != null) ? playerLoop.subSystemList.Length : 0;
        var newSubsystemList = new PlayerLoopSystem[oldListLength + 1];
        for (var i = 0; i < oldListLength; ++i)
            newSubsystemList[i] = playerLoop.subSystemList[i];
        newSubsystemList[oldListLength].type = system.GetType();
        newSubsystemList[oldListLength].updateDelegate = del.TriggerUpdate;
        playerLoop.subSystemList = newSubsystemList;
        return true;
    }
    if (playerLoop.subSystemList != null)
    {
        for(int i=0; i<playerLoop.subSystemList.Length; ++i)
        {
            if (AppendSystemToPlayerLoopListImpl(system, ref playerLoop.subSystemList[i], playerLoopSystemType))
                return true;
        }
    }
    return false;
}

文章作者: Yonggang Long
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Yonggang Long !
 上一篇
2022-08-10 Yonggang Long
下一篇 
2022-08-10 Yonggang Long
  目录