VContainer + ECS (beta)
VContainer supports integration between Unity's ECS (Entity Component System) and regular C# World. ( This is an experimental feature. Any feedback is wellcome! :0 )
caution
Currently, this feature requires Unity 2019.3 or later versions.
#
SetupECS features for VContainer is enabled if the project has the com.unity.entities
package installed.
- Currently, ECS is a preview version. You may need the settings:
[Windows] -> [Package Manager]
and[Advanced] -> [Show preview packages]
. - Select package of
Entities
and Press[Install]
.
If the com.unity.entities
package exists, the VCONTAINER_ECS_INTEGRATION
symbol is defined and the following features are enabled.
#
When using Unity's default WorldBy default, ECS will automatically instantiate classes that inherits the ComponentSystemBase
defined in your project, add it to the default world, and running on it.
In this mode, you can use method injection to ECS System
.
( The constructor is automatically used by Unity, so it cannot be used.)
(Optional) The above can also be declared by grouping as below:
Internaly, this is an automation of the following processes:
note
In default (UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP
is not used), The SystemGroup to which the System belongs can be controlled by Attribute ( e.g: [UpdateInGroup(typeof(SystemGroupType))]
.
#
Example of setup entities (with Default World)#
When to use your custom worldECS also allows you to create and register your own system.
There are two ways to disable Unity's automatic system bootstrap.
- By setting the define symbol
UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP
will disable all World and System auto-bootstrap. - Or, By adding the
[DisableAutoCreation]
attribute to the class definition to disable auto bootstrap per system.
For System that have auto bootstrap disabled, constructor injection can be used.
To use this System, you need to set up World yourself.
VContainer supports to instantiate of World and configuration.
(Optional) The above can also be declared by grouping as below:
Internaly, If you use above methods, the following setup will be performed automatically:
note
- Currently, VContainer is registering the World using
ScriptBehaviourUpdateOrder.UpdatePlayerLoop
. - This is an alias that registers 3 SystemGroups to PlayerLoop, so VContainer also creates these SystemGroups internally.
By default, VContainer will register System to SimulationSystemGroup
. If you want to change this, you can use .IntoGroup<T>()
:
#
Lifeime of World and SystemsRegisterNewWorld(...)
or UseNewWorld(...)
can accept Lifetime as an argument.
- This new World is placed under the control of VContainer.
- World holds System. Therefore, the lifetime of System is the same as World.
- If
Lifetime.Scoped
is specified, when the scope is destroyed, Dispose of all the systems belonging to that World will be called.