BEFORE READ: A lot of good changes in ECS was made since I wrote it, so the article is outdated. I promise to update it when ECS API becomes more stable in releases (soon™)
Part 1: Unity ECS - briefly about ecs
Part 2: Unity ECS - project design
Part 3: Unity ECS - operations on Entities
Part 4: Unity ECS - ECS and Jobs
The rule of thumb explanation
How does ComponentSystem often look like:
What is group?
Group is filter of all Entities in active world that have Foo and Bar component
It's like array of matching entities with references to all specific given components.
So, group is like EntityArray but with references to components, EntityArray itself is just array of Entities (and entity is just an index).
Group is constructed with a set of required components, subtractive components. It's also synced.
Why do we have Length field? Isn't Foos.Length the same?
Yes, you are right! They're the same. Length is length of EntityArray, so also Length of Foos and Bars, because each entity has Foo and Bar component
It's more obvious in this case
How do I manage lifetime of systems?
Well, you don't really need to!
Unity takes care about that. Systems track injected groups and will be disabled if there are no matching Entities and will be enabled if there appears one as well.
But if you really want to, take a look upwards, see OnStartRunning and OnStopRunning ? I mentioned ComponentSystemBase.Enabled = true which is probably what are you looking for. It's property that allows you to active/disable system manually.
Systems don't update in order I want
Convenience attributes for rescue! Since all systems are updated on the main thread, you need to think about order of updates, there are some attributes to help you with it:
[UpdateAfter(typeof(OtherSystem))], [UpdateBefore(typeof(OtherSystem))] and also [UpdateInGroup(typeof(UpdateGroup))], where UpdateGroup is empty class. You can even control update before/after Unity's phases by typeof(UnityEngine.Experimental.PlayerLoop.FixedUpdate) or other phases in same namespace.
How can I get access to system I want?
You can just inject it just like [Inject] private YourSystem yourSystem; easy as that. You can also use World.Active.GetExistingManager<YourSystem>() or if you're not sure if it exists but it should, use World.Active.GetOrCreateManager<YourSystem>()
Why to use EntityArray in system? What can I do with this index?
Since systems most often operates on components, not directly on entities, it raises question "Why do I even need this index". Saying "it's just and index" doesn't mean that is not usable at all. It's very important integer.
If you haven't tried Unity's ECS implementation, you probably don't know where is it needed.
It's needed among others for functionality from EntityManager that holds EntityData and controls adding/removing (and much more) components from a given entity.
But actually you don't want to add/remove components via EntityManager. You'd rather do it after update to not break the group (you'll get error about accessing deallocated nativearray), so you want to use PostUpdateCommands (EntityCommandBuffer). More about that in one of futures part of article.
World vs EntityManager
EntityManager is not weird, magic class, it's ScriptBehaviourManager and "merges" entities and their components.
In ECS we have a lot of managers. ComponentSystem is also ScriptBehaviourManager !
World holds all managers in one piece. We could colloquially say it manages the managers.
I know what's your question - yes, we can create multiple worlds, sounds interesting, isn't it? Maybe we'll take a look at this in future.
ComponentData and SharedComponentData. What's the difference?
The difference is trivial, ComponentData is just a component, SharedComponentData is as it says, component shared between different Entities. Very good explanation you can read in docs:
IComponentData is appropriate for data that varies between Entities, such as storing a world position. ISharedComponentData is useful when many Entities have something in common, for example in the boid demo we instantiate many Entities from the same Prefab and thus the MeshInstanceRenderer between many boid Entities is exactly the same.
So, with same IComponentData (eg. Position) changes from Entity0 won't change Position from Entity1, but with same SharedComponent (eg. Renderer) if you change material from Entity0, it'll change also material from Entity1. However you don't really want to change SharedComponents a lot, actually very rarely. More details THERE.
Well, there is one more difference - ComponentData have to be blittable.
Part 2: Unity ECS - project design
Give me some feedback in comment section, don't worry I won't hate you if you show me some "anomaly" in my article.
If you enjoy my article - like it and follow me. It'll motivate me to write more articles. See you.