Search Unity

Enabling and Disabling Systems for Unity ECS

Discussion in 'Entity Component System' started by Ootgard, May 23, 2018.

  1. Ootgard

    Ootgard

    Joined:
    Dec 4, 2012
    Posts:
    3
    Is there a way to enable and disable individual systems programmatically for Unity ECS?
     
  2. Prastiwar

    Prastiwar

    Joined:
    Jul 29, 2017
    Posts:
    125
    Yes, systems, so ScriptBehaviourManagers have property "Enabled"

    EDIT: In newer version Enabled property is from ComponentSystemBase
     
    Last edited: Jun 2, 2018
  3. hellowill89

    hellowill89

    Joined:
    Jan 8, 2013
    Posts:
    45
    Do you have a more complete example of how to do this?
     
  4. Prastiwar

    Prastiwar

    Joined:
    Jul 29, 2017
    Posts:
    125
    Code (CSharp):
    1. public class ComponentSys : ComponentSystem
    2. {
    3.     [Inject] private SomeSystem someSystem;
    4.  
    5.     protected override void OnUpdate()
    6.     {
    7.         if (disableSomeSystem)
    8.             someSystem.Enabled = false;
    9.     }
    10. }
    or

    Code (CSharp):
    1. public class SomeClass : MonoBehaviour
    2. {
    3.     private void SomeMethod()
    4.     {
    5.         World.Active.GetExistingManager<SomeSystem>().Enabled = false;
    6.     }
    7. }
     
    dherault, DonPuno, oAzuehT and 6 others like this.
  5. hellowill89

    hellowill89

    Joined:
    Jan 8, 2013
    Posts:
    45
    I see, awesome. I didn't know the system would be the type of the `GetExistingManager` call.
     
  6. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    In case anyone needs this, GetExistingManager is now renamed to GetExistingSystem
     
  7. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    can you disable a system precompiled? So it is not even in the game?
     
  8. psuong

    psuong

    Joined:
    Jun 11, 2014
    Posts:
    126
    Hmm - I think at that point you may not even want to create the System automatically using the [DisableAutoCreation] attribute on the system.

    If you need later you can always create the system into the world.
     
    Pawciu and GameDeveloper1111 like this.
  9. craig4android

    craig4android

    Joined:
    May 8, 2019
    Posts:
    124
    ty this is what I was looking for
     
    oAzuehT and bugfinders like this.
  10. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    Another option... use a disable tag in your SystemBase derived class like this:

    Code (CSharp):
    1. Entities.WithAll<DisableTag>(....).Foreach(() => { .... });
    And then when you want to disable it:

    Code (CSharp):
    1.  
    2. private void ToggleMyComponent(in EntityManager em, in Entity entity, bool enable)
    3. {
    4.     if (enable) {
    5.         if (em.HasComponent<DisableTag>(entity)) { em.RemoveComponent<DisableTag>(entity); }
    6.     } else {
    7.         if (!em.HasComponent<DisableTag>(entity)) { em.AddComponent<DisableTag>(entity); }
    8.     }
    9. }
    (you could replace em with EntityCommandBuffer.ParallelWriter ecb if you're toggling within a parallelized job).

    The system scheduler is smart enough to know that if nothing matches the query in the SystemBase, the Update() function won't get called at all.
     
  11. mariobobi

    mariobobi

    Joined:
    Jul 21, 2021
    Posts:
    1
    For Unity 2019.4.28f1 with HybridRenderer 0.5.2-preview 4 i used this to toggle the MeshRendererV2:

    Code (CSharp):
    1. World.DefaultGameObjectInjectionWorld.GetExistingSystem<Unity.Rendering.RenderMeshSystemV2>().Enabled
    I put it inside a monobehaviour class for a UI button and it correctly toggles the renderer on and off.
     
    lfent likes this.
  12. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    540
    strange I don't have public property Enabled for the system upload_2023-10-24_12-19-11.png
     
  13. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,267
    The method is returning a SystemHandle. You need to resolve that to a SystemState ref.
     
    bb8_1 and nicloay like this.