Selectively Running Systems in Scenes with Unity ECS

Learn how to selectively run jobs only in certain scenes with Unity ECS.

Last updated on June 23, 2020. Created on November 4, 2019.

One Approach

One way to control which systems run in a given scene is to use scene-based components. For instance, if a job only processes entities when they include a SceneAComponent, then you could restrict processing to a so-called SceneA like so:

Entities
    .WithAny<SceneAComponent>()
    .ForEach((ref SomeComponent someComponent) =>
    {
        ...
    })
    .WithName("SomeSceneAJob")
    .Schedule(inputDeps);

In that case we process any entity that has a SceneAComponent by way of WithAny. We can even perform negations! We could say WithNone(SceneBComponent), for example, meaning, "I don't want to process any entities that have the SceneBComponent."

A Different Approach

We can also disable systems conditionally.

Basically, you can use the SceneManager to disable a system on creation if it doesn't detect the required scene. The import you want is UnityEngine.SceneManagement. Here's some example code of this approach:

protected override void OnCreate()
{
    if (!SceneManager.GetActiveScene().name.Equals("IntendedScene"))
        Enabled = false;
}

So if it's not the scene intended for the system, then Enabled is set to false, which means OnUpdate won't be executed. Just be sure you get the name right.

Hope this helps!

© Reese Schultz

My code is released under the MIT license.