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 to disable/enable ECS systems

Discussion in 'Entity Component System' started by jazzbach, Oct 5, 2023.

  1. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Hello,

    I'd like to ask if there's a way to disable and/or enable systems from, let's say, a mono behaviour ?

    I'm trying to have a "catalog" of systems that I'd like to enable and disable at will instead of them starting automatically. Maybe there's already a built-in way to do it. If so, could you point me to where to check ?

    I tried the code from the following thread but it's deprecated (The "Enabled = false" section)
    https://forum.unity.com/threads/enabling-and-disabling-systems-for-unity-ecs.532808/#post-7606426

    Code (CSharp):
    1. World.DefaultGameObjectInjectionWorld.GetExistingSystem<MySystem>().Enabled = false
    Thanks a lot in advance
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Unless I am blind, the Enabled property is not deprecated. I suspect what is actually deprecated for you might be the API you are using to retrieve the system, of which there is likely an alternative you should be using instead that is just a little bit different but not enough to cause you major problems.
     
  3. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Hi @DreamingImLatios . Thanks for your response.

    I'm actually using Entities 1.0.8 and, I cannot access the "Enabled" property. You're right: Maybe it's not deprecated and I used the wrong word but what's true is that I cannot use this property at all. Is there something I'm doing wrong ? Or, is there another way to enable/disable systems ? (or, if it's a good idea to do so) ?
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Are you trying to access the Enabled property on an ISystem? If so, that's your problem. You need to resolve the SystemState first, which has the Enabled property.
     
  5. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    No. I'm trying to access it externally, either from a different system (SystemBase or ISystem) but preferably from a MonoBehaviour.
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Sorry. I just realized what is going on. There's so many variants to the APIs that have changed over the years. I suspect that GetExistingSystem is returning a SystemHandle. From that, you need to pass that handle into World.Unmanaged.ResolveSystemStateRef().
     
  7. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Hey @DreamingImLatios , no problem !! I've run into that api variant issue a lot as well. :D

    Yes, GetExistingSystem des return a SystemHandle as you mentioned. However, "World.Unmanaged" doesn't exist at all in the API. Maybe it's happening what you mentioned here as well about another API change
     
  8. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    You need an instance of the World to access Unmanaged. It is not a static field. In SystemBase and SystemState, those also have a property written exactly identical to the class name, hence the confusion.
     
  9. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Ohh, I see. Yes, I was able to get the WorldUnmanaged reference out of a World ref. Thanks for the help.

    Now, sorry to ask again but, what should I do with the SystemState returned by the "ResolveSystemStateRef()" method ?
     
  10. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
     
  11. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Ohh, I see. Perfect. Thanks a lot for your help ! I tried it and indeed, I was able to find the Enabled property. However, it looks like it's not doing anything to the system. Am I missing something ?
     
  12. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    I was able to solve thanks to @DreamingImLatios help in his Discord channel ! His suggestion is to get the SystemHandle from the ResolveSystemStateRef() method as a ref.

    Here's an example of how it can be used from within a MonoBehaviour:


    public void DisableSystem()
    {

    // Get the default world
    var world = World.DefaultGameObjectInjectionWorld;
    // Get the specific ECS system you want to disable
    SystemHandle mySystem = world.GetExistingSystem<YOUR_SYSTEM_TYPE>();
    ref SystemState state = ref world.Unmanaged.ResolveSystemStateRef(mySystem); // REF USED HERE
    state.Enabled = false;
    }
    public void EnableSystem()
    {
    // Get the default world
    var world = World.DefaultGameObjectInjectionWorld;
    // Get the specific ECS system you want to enable
    SystemHandle mySystem = world.GetExistingSystem<YOUR_SYSTEM_TYPE>();
    ref SystemState state = ref world.Unmanaged.ResolveSystemStateRef(mySystem); // REF USED HERE
    state.Enabled = true;
    }