添加System到引擎的PlayerLoop

todo: SystemState、

只支持继承自 ComponentSystemBaseISystemBase.

添加至World 缓存

获取所有System

反射获取继承自ComponentSystemBase/ISystemBase 的所有类型。 (TypeManager 缓存)

添加System (AddSystemToRootLevelSystemGroupsInternal)

添加至 World.m_Systems

  • 1.创建 InitializationSystemGroupSimulationSystemGroupPresentationSystemGroup进行分类。

  • 2.进行分类。 继承自ComponentSystemBase的分为managedTypes类,继承自ISystemBase的分为unmanagedTypes类,

继承自ComponentSystemBasemanagedTypes类,

AddSystemToUpdateList

  • 1.创建类型对象,添加至World.m_Systems。 并将ComponentSystemBase 以下的type 和 system对象添加至World.m_SystemLookup缓存。

  • 2.调用system.CreateInstance创建SystemState成员。, 并调用周期函数 OnBeforeCreateInternal、 OnCreateForCompiler、OnCreate/OnBeforeDestroyInternal、OnAfterDestroyInternal(异常时调用). ++Version.

  • 3.得到正确添加的types的System 。

  • 4.添加update系统。排除直接等于InitializationSystemGroupSimulationSystemGroupPresentationSystemGroup的类型。(这三个开始独立添加了)。

    • 1.不包含UpdateInGroupAttribute属性的添加至simulationSystemGroup.AddSystemToUpdateList(system);
    • 2.包含UpdateInGroupAttribute属性的,遍历UpdateInGroupAttribute属性。 属性的Group 必须继承自ComponentSystemGroup, 并且OrderFirst/OrderLast只能选一个。 如果group存在,添加至group。group.AddSystemToUpdateList(system)

继承自ISystemBaseunmanagedTypes类,

AddUnmanagedSystemToUpdateList

  • 1.调用world.CreateUnmanagedSystem(type); 创建
  • 2.不含UpdateInGroupAttribute属性的添加至simulationSystemGroup.AddUnmanagedSystemToUpdateList(system);
  • 3.不含UpdateInGroupAttribute属性.遍历UpdateInGroupAttribute属性。 属性的Group 必须继承自ComponentSystemGroup, 并且OrderFirst/OrderLast只能选一个。 如果group存在,添加至group。group.AddUnmanagedSystemToUpdateList(system)

Update player loop

// Update player loop
initializationSystemGroup.SortSystems();
simulationSystemGroup.SortSystems();
presentationSystemGroup.SortSystems();

添加System (ScriptBehaviourUpdateOrder.AddWorldToCurrentPlayerLoop(world))

添加至引擎PlayerLoopSystem 执行。

  • 1.主要通过框架的 ScriptBehaviourUpdateOrder辅助函数,将 InitializationSystemGroupSimulationSystemGroupPresentationSystemGroup的类型的Group进行Append添加至PlayerLoopSystem.对应的 UnityEngine.PlayerLoop.InitializationUnityEngine.PlayerLoop.UpdateUnityEngine.PlayerLoop.PreLateUpdate
    1. ScriptBehaviourUpdateOrder

[AttributeUsage(AttributeTargets.Class|AttributeTargets.Struct)]
public class UpdateInGroupAttribute : Attribute
{
    public bool OrderFirst = false;
    public bool OrderLast = false;

    public UpdateInGroupAttribute(Type groupType)
    {
        if (groupType == null)
            throw new ArgumentNullException(nameof(groupType));

        GroupType = groupType;
    }

    public Type GroupType { get; }
}

添加至PlayerLoopSystem对应的delegate包装

 // FIXME: HACK! - mono 4.6 has problems invoking virtual methods as delegates from native, so wrap the invocation in a non-virtual class
internal class DummyDelegateWrapper
{
    internal ComponentSystemBase System => m_System;
    private readonly ComponentSystemBase m_System;

    public DummyDelegateWrapper(ComponentSystemBase sys)
    {
        m_System = sys;
    }

    public unsafe void TriggerUpdate()
    {
        if (m_System.m_StatePtr != null)
        {
            m_System.Update();
        }
    }
}
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Struct, AllowMultiple = true)]
public class UpdateBeforeAttribute : Attribute
{
    public UpdateBeforeAttribute(Type systemType)
    {
        if (systemType == null)
            throw new ArgumentNullException(nameof(systemType));

        SystemType = systemType;
    }

    public Type SystemType { get; }
}

[AttributeUsage(AttributeTargets.Class|AttributeTargets.Struct)]
    public class UpdateInGroupAttribute : Attribute
    {
        public bool OrderFirst = false;
        public bool OrderLast = false;

        public UpdateInGroupAttribute(Type groupType)
        {
            if (groupType == null)
                throw new ArgumentNullException(nameof(groupType));

            GroupType = groupType;
        }

        public Type GroupType { get; }
    }


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