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. //Basically a GameObject can be used to hold all the information needed to create //an Entity and later queries to build the entitity itself. //This allow to trigger a sort of polyformic code that can be re-used to //create several type of entities. IEntityDescriptorHolder[] entities = contextHolder.GetComponentsInChildren <IEntityDescriptorHolder>(); //However 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 though and create entities always //manually. Basically 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>()); } }
public void OnContextCreated(UnityContext contextHolder) { IEntityDescriptorHolder[] entities = contextHolder.GetComponentsInChildren <IEntityDescriptorHolder>(); for (int i = 0; i < entities.Length; i++) { _entityFactory.BuildEntity((entities[i] as MonoBehaviour).gameObject.GetInstanceID(), entities[i].BuildDescriptorType()); } }
void BuildBonusSpawnerEntity(UnityContext contextHolder) { var spawners = contextHolder.GetComponentsInChildren <BonusSpawnerImplementor>(); foreach (var spawner in spawners) { List <IImplementor> implementors = new List <IImplementor>(); spawner.GetComponents(implementors); _entityFactory.BuildEntity <BonusSpawnerEntityDescriptor>(spawner.GetInstanceID(), implementors.ToArray()); } }
private void BuildGridFromScene(UnityContext unityContext, IEntityFactory entityFactory) { var entities = unityContext.GetComponentsInChildren <IEntityDescriptorHolder>(); foreach (var entityHolder in entities) { entityFactory.BuildEntity( new EGID((uint)((MonoBehaviour)entityHolder).gameObject.GetInstanceID(), EcsGroups.GridGroup), entityHolder.GetDescriptor(), ((MonoBehaviour)entityHolder).GetComponents <IImplementor>() ); } }
void BuildEntitiesFromScene(UnityContext contextHolder) { IEntityDescriptorHolder[] entities = contextHolder.GetComponentsInChildren <IEntityDescriptorHolder>(); for (int i = 0; i < entities.Length; i++) { var entityDescriptorHolder = entities[i]; var entityViewsToBuild = entityDescriptorHolder.GetEntitiesToBuild(); _entityFactory.BuildEntity (((MonoBehaviour)entityDescriptorHolder).gameObject.GetInstanceID(), entityViewsToBuild, (entityDescriptorHolder as MonoBehaviour).GetComponentsInChildren <IImplementor>()); } }
/// <summary> /// This is a possible approach to create Entities from already existing GameObject in the scene /// It is absolutely not necessary and I wouldn't rely on this in production /// </summary> /// <param name="contextHolder"></param> void BuildEntitiesFromScene(UnityContext contextHolder) { //An EntityDescriptorHolder is a special Svelto.ECS class created to exploit GameObjects to dynamically //retrieve implementors attached to it and then build a Svelto Entity. var entities = contextHolder.GetComponentsInChildren <IEntityDescriptorHolder>(); //This pattern is usually useful for guis where complex hierarchy of gameobjects are necessary, but //otherwise you should always create entities in factories. A Better GUI only example is necessary for (var i = 0; i < entities.Length; i++) { var entityDescriptorHolder = entities[i]; var entityViewsToBuild = entityDescriptorHolder.GetDescriptor(); _entityFactory .BuildEntity( new EGID((uint)((MonoBehaviour)entityDescriptorHolder).gameObject.GetInstanceID(), ECSGroups.ExtraStuff), entityViewsToBuild, (entityDescriptorHolder as MonoBehaviour).GetComponentsInChildren <IImplementor>()); } }
public void OnContextCreated(UnityContext contextHolder) { IEntityDescriptorHolder[] entities = contextHolder.GetComponentsInChildren <IEntityDescriptorHolder>(); foreach (IEntityDescriptorHolder entity in entities) { IEntityDescriptorInfo entityInfo = entity.RetrieveDescriptor(); m_EntityFactory.BuildEntity(((MonoBehaviour)entity).gameObject.GetInstanceID(), entityInfo, (entity as MonoBehaviour).GetComponentsInChildren <IImplementor>()); } GameObject player = Camera.main.gameObject; List <IImplementor> implementors = new List <IImplementor>(); player.GetComponents(implementors); implementors.Add(new PlayerInputImplementor()); m_EntityFactory.BuildEntity <PlayerEntity>(player.GetInstanceID(), implementors.ToArray()); }
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. //Basically a GameObject can be used to hold all the information needed to create //an Entity and later queries to build the entitity itself. //This allow to trigger a sort of polyformic code that can be re-used to //create several type of entities. IEntityDescriptorHolder[] entities = contextHolder.GetComponentsInChildren <IEntityDescriptorHolder>(); 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>()); } }
void ICompositionRoot.OnContextCreated(UnityContext contextHolder) { #if FIRST_TIER_EXAMPLE || SECOND_TIER_EXAMPLE || THIRD_TIER_EXAMPLE || FOURTH_TIER_EXAMPLE var tasksCount = NumberOfEntities.value; #if DONT_TRY_THIS_AT_HOME for (int i = 0; i < tasksCount; i++) { GameObject crazyness = new GameObject(); crazyness.AddComponent <UnityWay>(); } #else _enginesRoot = new EnginesRoot(new Schedulers.Unity.UnitySumbmissionEntityViewScheduler()); IEntityFactory entityFactory = _enginesRoot.GenerateEntityFactory(); var boidsEngine = new BoidsEngine(); _enginesRoot.AddEngine(boidsEngine); _contextNotifier.AddFrameworkDestructionListener(boidsEngine); #if FIRST_TIER_EXAMPLE || SECOND_TIER_EXAMPLE || THIRD_TIER_EXAMPLE var implementorArray = new object[1]; #endif for (int i = 0; i < tasksCount; i++) { #if FIRST_TIER_EXAMPLE || SECOND_TIER_EXAMPLE || THIRD_TIER_EXAMPLE implementorArray[0] = new Boid(); entityFactory.BuildEntity <BoidEntityDescriptor>(i, implementorArray); #else entityFactory.BuildEntity <BoidEntityDescriptor>(i); #endif } entityFactory.BuildEntity <GUITextEntityDescriptor>(0, contextHolder.GetComponentsInChildren <PrintIteration>()); #endif #endif }