Systems

ComponentSystemBase、SystemBase、ISystemBase、SystemState、Update

继承关系

  • ComponentSystemBase
    • SystemBase
    • JobComponentSystem
    • ComponentSystem
      • EntityPatcherBlobAssetSystem
      • RetainBlobAssetSystem
      • ComponentSystemGroup
        • InitializationSystemGroup
        • FixedStepSimulationSystemGroup
        • LateSimulationSystemGroup
        • SimulationSystemGroup
        • PresentationSystemGroup
      • EntityCommandBufferSystem
        • BeginInitializationEntityCommandBufferSystem
        • EndInitializationEntityCommandBufferSystem
        • BeginFixedStepSimulationEntityCommandBufferSystem
        • EndFixedStepSimulationEntityCommandBufferSystem
        • BeginSimulationEntityCommandBufferSystem
        • EndSimulationEntityCommandBufferSystem
        • BeginPresentationEntityCommandBufferSystem

生命周期

1.Create

  • OnCreateForCompiler(); 被调用于CreateInstance,被调用于 World.CreateSystemInternal
  • OnCreate 被调用于CreateInstance,被调用于 World.CreateSystemInternal
internal void CreateInstance(World world)
{
    m_StatePtr = SystemState.Allocate();
    m_StatePtr->InitManaged(world, GetType());

    OnBeforeCreateInternal(world);
    try
    {
        OnCreateForCompiler();
        OnCreate();
    }
    catch
    {
        OnBeforeDestroyInternal();
        OnAfterDestroyInternal();
        throw;
    }
}

2.OnDestroy

3. Update (主要)

  • ComponentSystem.Update
public sealed override void Update()
{
    var state = CheckedState();
#if ENABLE_PROFILER
    using (state->m_ProfilerMarker.Auto())
#endif

    {
        state->BeforeUpdateResetRunTracker();

        if (Enabled && ShouldRunSystem())
        {
            if (!state->m_PreviouslyEnabled)
            {
                state->m_PreviouslyEnabled = true;
                OnStartRunning();
            }

            BeforeOnUpdate();

            var oldExecutingSystem = ms_ExecutingSystem;
            ms_ExecutingSystem = this;

            try
            {
                OnUpdate();
            }
            finally
            {
                ms_ExecutingSystem = oldExecutingSystem;
                AfterOnUpdate();
            }
        }
        else if (state->m_PreviouslyEnabled)
        {
            state->m_PreviouslyEnabled = false;
            OnStopRunningInternal();
        }
    }
}
  • JobComponentSystem.Update
public sealed override void Update()
{
    var state = CheckedState();

#if ENABLE_PROFILER
    using (state->m_ProfilerMarker.Auto())
#endif
    {
        if (Enabled && ShouldRunSystem())
        {
            if (!state->m_PreviouslyEnabled)
            {
                state->m_PreviouslyEnabled = true;
                OnStartRunning();
            }

            var inputJob = BeforeOnUpdate();
            JobHandle outputJob = new JobHandle();

            var oldExecutingSystem = ms_ExecutingSystem;
            ms_ExecutingSystem = this;
            try
            {
                outputJob = OnUpdate(inputJob);
            }
            catch
            {
                ms_ExecutingSystem = oldExecutingSystem;

                AfterOnUpdate(outputJob, false);
                throw;
            }

            ms_ExecutingSystem = oldExecutingSystem;

            AfterOnUpdate(outputJob, true);
        }
        else if (state->m_PreviouslyEnabled)
        {
            state->m_PreviouslyEnabled = false;
            OnStopRunning();
        }
    }
}
  • SystemBase
public sealed override void Update()
{
    var state = CheckedState();

#if ENABLE_PROFILER
    using (state->m_ProfilerMarker.Auto())
#endif
    {
        state->BeforeUpdateResetRunTracker();

        if (Enabled && ShouldRunSystem())
        {
            if (!state->m_PreviouslyEnabled)
            {
                state->m_PreviouslyEnabled = true;
                OnStartRunning();
            }

            state->BeforeOnUpdate();

            var oldExecutingSystem = ms_ExecutingSystem;
            ms_ExecutingSystem = this;
            try
            {
                OnUpdate();
            }
            catch
            {
                ms_ExecutingSystem = oldExecutingSystem;

                state->AfterOnUpdate();

#if ENABLE_UNITY_COLLECTIONS_CHECKS
                var hasSafetyError = false;
                var details = default(SystemDependencySafetyUtility.SafetyErrorDetails);
                state->CheckSafety(ref details, ref hasSafetyError);
#endif

                throw;
            }

            ms_ExecutingSystem = oldExecutingSystem;

            state->AfterOnUpdate();

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            {
                var hasSafetyError = false;
                var details = default(SystemDependencySafetyUtility.SafetyErrorDetails);
                state->CheckSafety(ref details, ref hasSafetyError);
                if (hasSafetyError)
                {
                    throw new InvalidOperationException(details.FormatToString(GetType()));
                }
            }
#endif
        }
        else if (state->m_PreviouslyEnabled)
        {
            state->m_PreviouslyEnabled = false;
            OnStopRunning();
        }
    }
}

包含重要对象

  • SystemState

其他分组属性

UpdateInGroup

UpdateAfter

UpdateBefore


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