Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Add system to PlayerLoop

Discussion in 'Entity Component System' started by YuriyVotintsev, Dec 28, 2021.

  1. YuriyVotintsev

    YuriyVotintsev

    Joined:
    Jun 11, 2013
    Posts:
    91
    How to add system to specific place in player loop. I know how to do it using custom bootstrap, but when using it i need to create world from scratch or full copypaste defaultworldinitialization. It there any simple way to add system to player loop in existed default world?
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    Code (CSharp):
    1. var simultaionGroup = World.GetOrCreateSystem<SimulationSystemGroup>(); //Can be any other top level group - Initialization or Presentation
    2. var mySystem = World.CreateSystem<MySystem>();
    3. simultaionGroup.AddSystemToUpdateList(mySystem);
    4. simultaionGroup.SortSystems(); //Mark dirty if !EnableSystemSorting or sorts systems in group depends on their attributes etc.
    5.  
    6. //Or when you want to add your group with your system, just do the same
    7.  
    8. var simultaionGroup = World.GetOrCreateSystem<SimulationSystemGroup>(); //Can be any other top level group - Initialization or Presentation
    9.  
    10. var myGroup = World.CreateSystem<MyGroup>();
    11. var mySystem = World.CreateSystem<MySystem>();
    12. myGroup.AddSystemToUpdateList(mySystem);
    13. myGroup.SortSystems(); //Mark dirty if !EnableSystemSorting or sorts systems in group depends on their attributes etc.
    14.  
    15. simultaionGroup.AddSystemToUpdateList(myGroup);
    16. simultaionGroup.SortSystems(); //Mark dirty if !EnableSystemSorting or sorts systems in group depends on their attributes etc.
     
    bb8_1 likes this.
  3. YuriyVotintsev

    YuriyVotintsev

    Joined:
    Jun 11, 2013
    Posts:
    91
    Thank you for answer, but looks like i have not explained clearly enough. I want to add system at some place not in standard initialize, simulation or presentation system groups. I want to add system at place where default Application.onBeforeRender occurs. And I want to do it not with manual system.Update() invoke, but using same methods as for standard initialize, simulation or presentation system groups does.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    With World - you can't. When the world is registered in the player loop it registers only these 3 top-level groups inside related player loop places. As a result, an update will be triggered only for these 3 root groups (and their inner systems hierarchy as
    AppendSystemToPlayerLoopListImpl
    is called recursively from the top of the player loop)
    upload_2021-12-28_12-46-54.png


    What you can do is not related to World at all, but to the Unity player loop itself. You just register your "system" (which can be just a class) and put this in the player loop (in the sample below it's just at the end of the player loop root, but you can order that whatever you want, get
    subSystemList
    find required
    PlayerLoopSystem
    and put your system inside).
    upload_2021-12-28_13-16-7.png

    As result, your
    updateDelegate
    will be called every frame in a specific player loop place.
    upload_2021-12-28_13-12-46.png
    upload_2021-12-28_13-9-23.png
    upload_2021-12-28_13-10-0.png
     
    Nit_Ram and Kmsxkuse like this.
  5. BelkinAlex

    BelkinAlex

    Joined:
    Sep 28, 2015
    Posts:
    9
  6. YuriyVotintsev

    YuriyVotintsev

    Joined:
    Jun 11, 2013
    Posts:
    91
    Ok, thanks. So there is no native easy way, so i need to code it myself.