I created this video guide to help explain Unity ECS

6 Likes

I have updated the docs with info about the ComponentSystem being auto-created & fixed the freaking code example on intro page… Oops sorry, no idea how that happened… Will be in the next ECS release.

3 Likes

Thank you!

Is it possible to disable automatic creation of default world ?
I’d like to go “all manual” way without any magic auto include of all systems…

The automatic creation is done in AutomaticWorldBootstrap.cs in the com.unity.entities package.

Basically the file looks like this

using UnityEngine;

namespace Unity.Entities
{
#if !UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP
    static class AutomaticWorldBootstrap
    {
        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        static void Initialize()
        {
            DefaultWorldInitialization.Initialize("Default World", false);
        }
    }
#endif
}

So to disable automatic creation just make sure to set the define UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP.

Then all you need to do is create your world (line 30 below) and make sure Unity knows about it (line 59 below), and dont forget to make sure you also clean up on DomainReload (line 38 below). To see how this is done check DefaultWorldInitialization.cs below

using System;
using System.Linq;
using UnityEngine;

namespace Unity.Entities
{
    static class DefaultWorldInitialization
    {
        static void DomainUnloadShutdown()
        {
            World.DisposeAllWorlds();
            ScriptBehaviourUpdateOrder.UpdatePlayerLoop();
        }

        static void GetBehaviourManagerAndLogException(World world, Type type)
        {
            try
            {
                world.GetOrCreateManager(type);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }
        }

        public static void Initialize(string worldName, bool editorWorld)
        {
            var world = new World(worldName);
            World.Active = world;

            // Register hybrid injection hooks
            InjectionHookSupport.RegisterHook(new GameObjectArrayInjectionHook());
            InjectionHookSupport.RegisterHook(new TransformAccessArrayInjectionHook());
            InjectionHookSupport.RegisterHook(new ComponentArrayInjectionHook());

            PlayerLoopManager.RegisterDomainUnload (DomainUnloadShutdown, 10000);

            foreach (var ass in AppDomain.CurrentDomain.GetAssemblies())
            {
                var allTypes = ass.GetTypes();

                // Create all ComponentSystem
                var systemTypes = allTypes.Where(t =>
                    t.IsSubclassOf(typeof(ComponentSystemBase)) &&
                    !t.IsAbstract &&
                    !t.ContainsGenericParameters &&
                    t.GetCustomAttributes(typeof(DisableAutoCreationAttribute), true).Length == 0);
                foreach (var type in systemTypes)
                {
                    if (editorWorld && type.GetCustomAttributes(typeof(ExecuteInEditMode), true).Length == 0)
                        continue;
                      
                    GetBehaviourManagerAndLogException(world, type);
                }
            }

            ScriptBehaviourUpdateOrder.UpdatePlayerLoop(world);
        }
    }
}
8 Likes

Awesome, thank you

Great! The auto-creation info will be perfect for my upcoming vid on Hybrid ECS vs Pure ECS.

Is it possible to create a separate world for normal objects as well?
So we could have two simulations (server-objects and client-objects) in the same process for networking tests, without having to essentially share objects (as it currently is with UNET)

Will there be UI improvements so these systems show up in the editor and could be interacted with in the editor e.g. enabled/disabled or even configured to handle more complex Archetypes or filters other than the standard ones?

A way to setup System sequences e.g. what runs first and last or even to run systems at multiple times within the main loop would be very beneficial e.g. Different damage types interleaved with cleaning up dead entities and triggering effects.