-
-
Notifications
You must be signed in to change notification settings - Fork 95
Svelto.ECS and Unity
Svelto.ECS (as well as Svelto.Tasks) is designed to be platform agnostic. However I mainly use it with unity, so Unity extensions are provided. The EntityDescriptorHolder is an example. Using implementors as Monobehaviour let to exploit most of the Unity callbacks, but on other platforms the reasoning could be very similar. All that said, Svelto.ECS gives you the chance to abstract from Unity and you should use Unity classes as less as possible, especially Monobehaviours. It's also important to keep in mind that the creation of GameObject(s) is uncoupled from the creation of Entities, the only thing they have in common is the fact that gameobject monobehaviours can be implementors. You can see from the example that enemy gameobjects and their ECS entities are built independently.
Q. How can I build entities from gameobjects already present in the scene?
The option is available, but not advices to use, as it's always better to create entities explicitly. In the Survival Example you can find this code:
void BuildEntitiesFromScene(UnityContext contextHolder)
{
//An EntityDescriptorHolder is a special Svelto.ECS class created to exploit
//GameObjects to dynamically retrieve the Entity information attached to it.
//A GameObject can be used to hold all the information needed to create
//an Entity.
//This allows to trigger a sort of polymorphic code that can be re-used to
//create several type of entities.
IEntityDescriptorHolder[] entities = contextHolder.GetComponentsInChildren<IEntityDescriptorHolder>();
//Although this common pattern in Svelto.ECS application exists to automatically
//create entities from gameobjects already presented in the scene,
//I still suggest to avoid this method and create entities always
//explcitly. EntityDescriptorHolder should be avoided
//whenver not strictly necessary.
for (int i = 0; i < entities.Length; i++)
{
var entityDescriptorHolder = entities[i];
var entityDescriptor = entityDescriptorHolder.RetrieveDescriptor();
_entityFactory.BuildEntity
(((MonoBehaviour) entityDescriptorHolder).gameObject.GetInstanceID(),
entityDescriptor,
(entityDescriptorHolder as MonoBehaviour).GetComponentsInChildren<IImplementor>());
}
}
Q. How does Svelto ECS interact with Unity?
Svelto ECS interacts with the underlying platform (which may be Unity) through the implementors. Implementors allow to abstract the Engines code from the platform code. For example, if you need to know when a collider enters in a trigger area using the classic unity way, you can do something like:
public class EnemyTriggerImplementor : MonoBehaviour, IImplementor, IEnemyTriggerComponent
{
public EnemyCollisionData entityInRange { get; private set; }
void OnTriggerEnter(Collider other)
{
entityInRange = new EnemyCollisionData(new EGID(other.gameObject.GetInstanceID()), true);
}
void OnTriggerExit(Collider other)
{
entityInRange = new EnemyCollisionData(new EGID(other.gameObject.GetInstanceID()), false);
}
bool _targetInRange;
}
the result is then stored and retrieved through the designated entity component interface.