Search Unity

How to create a custom SystemGroup?

Discussion in 'Entity Component System' started by Mr-Mechanical, Mar 19, 2019.

  1. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Code (CSharp):
    1. [UpdateInGroup(typeof(InitializationSystemGroup))]
    2. public class MySystemsGroupInOtherPlace : ComponentSystemGroup
    3. {
    4.    
    5. }
    6.  
    7. [UpdateInGroup(typeof(MySystemsGroupInOtherPlace))]
    8. public class TestSystem1 : JobComponentSystem
    9. {
    10.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    11.     {
    12.         return inputDeps;
    13.     }
    14. }
    15.  
    16. //By default all groups and systems in SimulationSystemGroup
    17. public class MySystemsGroupInSimulationGroup : ComponentSystemGroup
    18. {
    19.    
    20. }
    21.  
    22. [UpdateInGroup(typeof(MySystemsGroupInSimulationGroup))]
    23. public class TestSystem2 : JobComponentSystem
    24. {
    25.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    26.     {
    27.         return inputDeps;
    28.     }
    29. }
    upload_2019-3-19_18-32-45.png
     
  3. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    !!!! Thanks. Exactly what I was looking for.
     
  4. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    To have the SystemGroup update in FixedUpdate, you need to manually handle the PlayerLoop as SimulationSystemGroup is in the Update loop right now.

    Code (CSharp):
    1. public static void UpdatePlayerLoop(World world)
    2. {
    3.     var playerLoop = PlayerLoop.GetDefaultPlayerLoop();
    4.     if (world != null)
    5.     {
    6.         // Insert the root-level systems into the appropriate PlayerLoopSystem subsystems:
    7.         for (var i = 0; i < playerLoop.subSystemList.Length; ++i)
    8.         {
    9.             int subsystemListLength = playerLoop.subSystemList[i].subSystemList.Length;
    10.             if (playerLoop.subSystemList[i].type == typeof(Update))
    11.             {
    12.                 var newSubsystemList = new PlayerLoopSystem[subsystemListLength + 1];
    13.                 for (var j = 0; j < subsystemListLength; ++j)
    14.                     newSubsystemList[j] = playerLoop.subSystemList[i].subSystemList[j];
    15.                 InsertManagerIntoSubsystemList(newSubsystemList,
    16.                     subsystemListLength + 0, world.GetOrCreateManager<SimulationSystemGroup>());
    17.                 playerLoop.subSystemList[i].subSystemList = newSubsystemList;
    18.             }
    19.             else if (playerLoop.subSystemList[i].type == typeof(PreLateUpdate))
    20.             {
    21.                 var newSubsystemList = new PlayerLoopSystem[subsystemListLength + 1];
    22.                 for (var j = 0; j < subsystemListLength; ++j)
    23.                     newSubsystemList[j] = playerLoop.subSystemList[i].subSystemList[j];
    24.                 InsertManagerIntoSubsystemList(newSubsystemList,
    25.                     subsystemListLength + 0, world.GetOrCreateManager<PresentationSystemGroup>());
    26.                 playerLoop.subSystemList[i].subSystemList = newSubsystemList;
    27.             }
    28.             else if (playerLoop.subSystemList[i].type == typeof(Initialization))
    29.             {
    30.                 var newSubsystemList = new PlayerLoopSystem[subsystemListLength + 1];
    31.                 for (var j = 0; j < subsystemListLength; ++j)
    32.                     newSubsystemList[j] = playerLoop.subSystemList[i].subSystemList[j];
    33.                 InsertManagerIntoSubsystemList(newSubsystemList,
    34.                     subsystemListLength + 0, world.GetOrCreateManager<InitializationSystemGroup>());
    35.                 playerLoop.subSystemList[i].subSystemList = newSubsystemList;
    36.             }
    37.         }
    38.     }
    39.  
    40.     PlayerLoop.SetPlayerLoop(playerLoop);
    41.     currentPlayerLoop = playerLoop;
    42. }
     
    Mr-Mechanical likes this.
  5. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    If you have multiple systems in a group would you use [UpdateInGroup(typeof(MySystemsGroup))] for all of your systems or just the first one (when also using UpdateAfter)?
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    All systems which should be in different from Simulation group - should use UpdateInGroup attribute
     
    Mr-Mechanical likes this.