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 How can I runing different system in defferent scene?

Discussion in 'Entity Component System' started by junsenLi, May 11, 2022.

  1. junsenLi

    junsenLi

    Joined:
    Feb 22, 2019
    Posts:
    18
    For example, I want to runing customSystemA only when opend sceneA, and runing customSystemB only when opend sceneB, but in fact, the systemA and systemB will runing both in any scene if I do not any thing.

    I tried to set a judgement in system, like this:
    Code (CSharp):
    1.   if (SceneManager.GetActiveScene().name != "SceneA")
    2.         {
    3.              Enable = false;
    4.         }
    It work, but.. not graceful. so I want to ask has any other resolvelation?

    It is use World and ICustomBootstrap? but I can not find any document, and I do not know how to move system to custom world from default world and enable or disable world.
     
  2. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    You can create two structs implementing IComponentData. Author one scene to have an entity with one of those components and the other scene to have an entity with the other component. Then both systems should call RequireSingletonForUpdate<T>() in their respective OnCreate() methods, one with the first component type passed in, and the other with the second component type passed in.
     
    brunocoimbra likes this.
  3. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    Achieving this using ICustomBootstrap should look something like this. Just drop it in your project it's looked up using reflection.
    Code (CSharp):
    1.     class WorldCreator : ICustomBootstrap
    2.     {
    3.         public bool Initialize(string defaultWorldName)
    4.         {
    5.             var world = new World(defaultWorldName, WorldFlags.Game);
    6.             World.DefaultGameObjectInjectionWorld = world;
    7.  
    8.             var systemList = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default).ToList();
    9.             if (SceneManager.GetActiveScene().name == "scene-a")
    10.                 systemList.Add(typeof(SceneASystem));
    11.             if (SceneManager.GetActiveScene().name == "scene-b")
    12.                 systemList.Add(typeof(SceneBSystem));
    13.                
    14.             DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(world, systemList);
    15.             ScriptBehaviourUpdateOrder.AddWorldToCurrentPlayerLoop(world);
    16.            
    17.             return true;
    18.         }
    19.     }
    20.  
     
    CookieStealer2 likes this.
  4. junsenLi

    junsenLi

    Joined:
    Feb 22, 2019
    Posts:
    18
    Hi, this way is nearly what I want, thanks.Through my test, you need to set the attribute of "DisableAutoCreation" and "WorldSystemFilter(WorldSystemFilterFlags.ProcessAfterLoad)", otherwise customSystem will create automatictly and they are categorized in the default system list, in the other mean, they still in GetAllSystems(WorldSystemFilterFlags.Default).ToList().

    like this:
    Code (CSharp):
    1. [DisableAutoCreation]
    2. [WorldSystemFilter(WorldSystemFilterFlags.ProcessAfterLoad)]
    3. public partial class customASystem : SystemBase
    4. {