Search Unity

Question Enable/Disable Systems Programmatically in 1.0

Discussion in 'Entity Component System' started by Vacummus, Jan 21, 2023.

  1. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Couldn't find anything around this for 1.0. What's the best way to enable/disable systems programmatically?
     
    Last edited: Jan 21, 2023
  2. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    I think I figured it out:

    Code (CSharp):
    1. World.Systems[0].enabled = false;
    Just need to iterate over the systems to find the one you want.
     
  3. MaNaRz

    MaNaRz

    Joined:
    Aug 24, 2017
    Posts:
    117
    You likely want something like this:
    World.GetExistingSystemManaged<MySys>().Enabled = false;
     
  4. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Nice. That's actually better. How about unmanaged systems (systems that inherit from ISystem)? Can't find a way to enable/disable them.
     
  5. MaNaRz

    MaNaRz

    Joined:
    Aug 24, 2017
    Posts:
    117
    Thats the way id do it:

    Code (CSharp):
    1. var systemHandle = World.GetExistingSystem<MyUnmanagedSys>();
    2. var unmanagedSystem = World.Unmanaged.GetUnsafeSystemRef<MyUnmanagedSys>(systemHandle);
    3. unmanagedSystem.Enabled = false;
    Maybe there is a better way idk.

    I don't know your use case but you should also consider
    RequireForUpdate(entityQuery) as an option. Thats usually what i go with.
     
    Last edited: Jan 21, 2023
    Vacummus likes this.
  6. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    That's perfect. Thanks. I have used RequireForUpdate before. My use case is more so for some custom debugging purposes. Not anything to be used for production so doesn't need to be fancy.
     
  7. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Hmm, just tried it and "Enabled" does not exist on "ISystem".
     
  8. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
  9. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    I also just found that you can use "GetExistingSystemState":

    Code (CSharp):
    1. World.Unmanaged.GetExistingSystemState<MyUnmanagedSys>().Enabled = false;
     
    MaNaRz likes this.
  10. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    Is this for 1.0? I use Entities 1.0.0-pre 15. this API is not valid for me.
     
  11. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    It is. I am using 1.0.0-pre.15 as well. What error are you seeing? Note that this API is for unmanaged systems (systems that inherit from ISystem). If your system is managed (inherits from SystemBase) then use this API:

    Code (CSharp):
    1. world.GetExistingSystemManaged<MyManagedSystem>().Enabled = enabled
     
  12. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,162
    I could use it as;

    Code (CSharp):
    1. var systemHandle = World.DefaultGameObjectInjectionWorld.GetExistingSystem<ChildMoveSystem>();
    How can I get unmanaged system and access it's custom fields? World.DefaultGameObjectInjectionWorld.GetExistingSystem<ChildMoveSystem>() returns SystemHandle. I want to access my system ChildMoveSystem.
     
  13. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
    If your ChildMoveSystem is unmanaged system
    Code (CSharp):
    1. var handle = World.DefaultGameObjectInjectionWorld.GetExistingSystem<T>();
    2. var systemReference = World.DefaultGameObjectInjectionWorld.Unmanaged.GetUnsafeSystemRef<T>(handle);
    If your system is managed
    Code (CSharp):
    1. var systemReference = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<T>();
     
    charleshendry, lclemens and atmuc like this.