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

Is there a boolean field for turning on/off components?

Discussion in 'Entity Component System' started by Arowx, Jun 25, 2018.

  1. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    So I would like to make some components flash and therefore need to turn their rendering state on and off but it looks like the basic ECS rendering system is under the hood and the only way to turn it off and on again would be to remove and add components?
    Or can I add a boolean and overload the rendering system with my own.
    Side Note: There seems to be lots of little built in systems that are on by default with ECS e.g. rendering and moveforward systems this is fundamentally wrong IMHO as any system should be programmer controlled even if we just have to enable it in a Bootstrap one liner.
     
  2. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    Last edited: Jun 25, 2018
  3. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,131
    Or just do not use any of the components unity provides in the ecs package. In case you want to use one, copy it into your own namespace (together with the systems that you want) - this also allows you to customize them, where needed. Much cleaner to have all components and systems in your project structure
     
  4. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,554
    If you don't want to add-remove MeshInstanceRenderer you can add-remove MeshCulledComponent instead. The render system is looking for that component subtractively.

    Side note : If you still want default world auto instantiated but without Unity's system you can remove them manually afterwards using approach similar to default world initialization. That is looping through AppDomain.CurrentDomain.GetAssemblies().GetTypes() but choose only one with Unity's assembly and then use World.DestroyManager. Note that all of Unity system's OnCreateManager will still execute once before you can remove.
     
    Antypodish likes this.
  5. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Why can't I overload the default behaviour with a new system and function.
     
  6. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,554
    The default mechanism works because of 2 things :

    1. Attribute : [RuntimeInitializeOnLoadMethod]. It is a part of regular Unity attribute. It scans every method in your project and call them. The default world creation code use this attribute. The method using this must be static like regular C# program if we want to start from somewhere it has to be static.
    2. Static method can only call other static method. In C# you cannot overload static methods therefore you cannot alter it's behaviour from outside the lib.

    One might think Unity team could tweak this to be extensible like this :
    1. making `(internal) static class DefaultWorldInitialization` to be public instead.
    2. Adding additional `params ScriptBehaviourManager[]` to the method `Initialize(string worldName, bool editorWorld)` inside the static class.
    3. Then we can put `UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP` to stop the default one, and then we call DefaultWorldInitialization.Initialize("..",false, system, system, ...) somewhere in the code and get a hand crafted world.

    ...but it would not be automatic as in the end have to call it somewhere by yourself. And it would be almost equivalent to `new`ing a World and add system one by one by yourself defeating the point of modifying DefaultWorldInitialization. So you can see you cannot have automatic workflow and overriding at the same time.

    One possible way I can think of that a static can change behavior is by making it read from a fixed physical config file in a similar fashion to package.json, etc. (basically bypassing the language) then have that file link to Unity's setting menu. Do you think that outside-of-the-code approach is better?

    I am wondering what kind of usage interface you are looking for in order to override the bootstrap behaviour that is better than new a world and put systems in it. If you can type a pseudo code what it would look like ideally for you I think Unity team might consider it.
     
  7. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    In an ideal situation the default world and it's systems would appear in the Unity IDE with a little tick box so I can turn them on or off if I want them.
     
  8. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,554
    Well for IDE stuff you would then need a setting file to remember your check box and also IDE code.

    While waiting for this feature you can just make a C# file with this code and think of it as your setting file + IDE when you open it in the code editor. You also get auto completion via your code editor so I think it is not that bad and even comparable to IDE checkboxes.

    Code (CSharp):
    1. #if !UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP
    2. static class AutomaticWorldBootstrapModification
    3. {
    4.     private static Type[] RemovalList = new Type[]
    5.     {
    6.         typeof(MoveForwardSystem),
    7.         typeof(MoveForward2DSystem),
    8.         typeof(TransformSystem),
    9.         typeof(MeshInstanceRendererSystem),
    10.         typeof(MeshFrustumCullingSystem),
    11.     };
    12.  
    13.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    14.     static void Initialize()
    15.     {
    16.         foreach(Type t in RemovalList)
    17.         {
    18.             World.Active.DestroyManager(World.Active.GetExistingManager(t));
    19.         }
    20.         ScriptBehaviourUpdateOrder.UpdatePlayerLoop(World.Active);
    21.     }
    22. }
    23. #endif
    Systems in that array will not be in your default world. Because majority of systems that you want to use will be your own I think it is better to selectively remove only Unity ones like this also.

    But personally I would not bother trying to remove them because when I don't have entities that match they will not run anyways, and when I do have those entities that's when I want them to exist.