How to disable/enable ECS systems

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)

World.DefaultGameObjectInjectionWorld.GetExistingSystem<MySystem>().Enabled = false

Thanks a lot in advance

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.

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) ?

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.

No. I’m trying to access it externally, either from a different system (SystemBase or ISystem) but preferably from a MonoBehaviour.

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().

Hey @DreamingImLatios , no problem !! I’ve run into that api variant issue a lot as well. :smile:

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

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.

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 ?

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 ?

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:

{

// 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;
}```

I want to know why it didn’t work…

It should be said that why the game object did not stop

Use code tags. Don’t send screenshots of code, those are pointless.
https://discussions.unity.com/t/481379
You’ve also provided basically no context, like what objects you’re talking about or the exact outcome you want. You should start a new detailed thread.

void PauseSystem()
    {
        //停止各系统
        var world = World.DefaultGameObjectInjectionWorld;



        SystemHandle redSystem = world.GetExistingSystem(typeof(RedLeadSystem));
        ref SystemState redSysState = ref world.Unmanaged.ResolveSystemStateRef(redSystem); // 此处使用参考
        redSysState.Enabled = false;

        SystemHandle blueSystem = world.GetExistingSystem(typeof(BlueLeadSystem));
        ref SystemState blueSysState = ref world.Unmanaged.ResolveSystemStateRef(blueSystem); // 此处使用参考
        blueSysState.Enabled = false;

        SystemHandle xiuQiuSystem = world.GetExistingSystem(typeof(XiuQiuSystem));
        ref SystemState xiuQiuSysState = ref world.Unmanaged.ResolveSystemStateRef(xiuQiuSystem); // 此处使用参考
        xiuQiuSysState.Enabled = false;

        SystemHandlezhongSystem = world.GetExistingSystem(typeof(ZhongBoomSystem));
        ref SystemStatezhongSysState = ref world.Unmanaged.ResolveSystemStateRef(ZhongSystem); // 此处使用参考
        zhongSysState.Enabled = false;

        SystemHandle LvSystem = world.GetExistingSystem(typeof(LvSystem));
        ref SystemState LvSysState = ref world.Unmanaged.ResolveSystemStateRef(LvSystem); // 此处使用参考
        LvSysState.Enabled = false;

        SystemHandle HuSystem = world.GetExistingSystem(typeof(HuSystem));
        ref SystemState HuSysState = ref world.Unmanaged.ResolveSystemStateRef(HuSystem); // 此处使用参考
        HuSysState.Enabled = false;

        SystemHandle WuGuiSystem = world.GetExistingSystem(typeof(WuGuiSystem));
        ref SystemState WuGuiSysState = ref world.Unmanaged.ResolveSystemStateRef(WuGuiSystem); // 此处使用参考
        WuGuiSysState.Enabled = false;

        SystemHandle gamelogicSystem = world.GetExistingSystem(typeof(GameLogicSystem));
        ref SystemState gamelogicSysState = ref world.Unmanaged.ResolveSystemStateRef(gamelogicSystem); // 此处使用参考
        gamelogicSysState.Enabled = false;

        LogManager.DisPlayLog("ECS系统已终止");
    }[/代码]