public void UnlinkGameObjectFromEntity(GameObject gameObject, Entity entity, ViewCommandBuffer viewCommandBuffer) { if (entityManager.Exists(entity)) { foreach (var component in gameObject.GetComponents <Component>()) { if (ReferenceEquals(component, null)) { continue; } var componentType = component.GetType(); if (entityManager.HasComponent(entity, componentType)) { viewCommandBuffer.RemoveComponent(entity, componentType); } } if (entityManager.HasComponent <GameObjectReference>(entity)) { viewCommandBuffer.RemoveComponent(entity, typeof(GameObjectReference)); } } var spatialOSComponent = gameObject.GetComponent <SpatialOSComponent>(); if (spatialOSComponent != null) { UnityObjectDestroyer.Destroy(spatialOSComponent); } }
public void LinkGameObjectToEntity(GameObject gameObject, Entity entity, EntityId spatialEntityId, ViewCommandBuffer viewCommandBuffer) { gameObjectComponentTypes.Clear(); foreach (var component in gameObject.GetComponents <Component>()) { var componentType = component.GetType(); if (gameObjectComponentTypes.Contains(componentType)) { logDispatcher.HandleLog(LogType.Warning, new LogEvent( "GameObject contains multiple instances of the same component type. Only one instance of each component type will be added to the corresponding ECS entity.") .WithField("EntityId", spatialEntityId) .WithField("ComponentType", componentType)); continue; } gameObjectComponentTypes.Add(componentType); viewCommandBuffer.AddComponent(entity, component.GetType(), component); } var spatialOSComponent = gameObject.AddComponent <SpatialOSComponent>(); spatialOSComponent.Entity = entity; spatialOSComponent.SpatialEntityId = spatialEntityId; spatialOSComponent.World = world; spatialOSComponent.LogDispatcher = logDispatcher; }
public EntityGameObjectLinker(World world) { this.world = world; entityManager = world.EntityManager; workerSystem = world.GetExistingSystem <WorkerSystem>(); lifecycleSystem = world.GetExistingSystem <RequireLifecycleSystem>(); viewCommandBuffer = new ViewCommandBuffer(entityManager, workerSystem.LogDispatcher); workerSystem = world.GetExistingSystem <WorkerSystem>(); if (workerSystem == null) { throw new ArgumentException( $"Can not create {nameof(EntityGameObjectLinker)}. {world.Name} does not contain a {nameof(workerSystem)}"); } subscriptionSystem = world.GetExistingSystem <SubscriptionSystem>(); if (subscriptionSystem == null) { throw new ArgumentException( $"Can not create {nameof(EntityGameObjectLinker)}. {world.Name} does not contain a {nameof(SubscriptionSystem)}"); } }
protected override void OnCreateManager(int capacity) { base.OnCreateManager(capacity); worker = Worker.GetWorkerFromWorld(World); viewCommandBuffer = new ViewCommandBuffer(EntityManager, worker.LogDispatcher); entityGameObjectCreator = new EntityGameObjectCreator(World); entityGameObjectLinker = new EntityGameObjectLinker(World, worker.LogDispatcher); }
public void LinkGameObjectToEntity(GameObject gameObject, Entity entity, ViewCommandBuffer viewCommandBuffer) { bool hasSpatialEntityId = entityManager.HasComponent <SpatialEntityId>(entity); bool isWorkerEntity = entityManager.HasComponent <WorkerEntityTag>(entity); if (!hasSpatialEntityId && !isWorkerEntity) { worker.LogDispatcher.HandleLog(LogType.Warning, new LogEvent( "Attempted to link GameObject to an entity that is not a SpatialOS entity or the worker entity") .WithField(LoggingUtils.LoggerName, nameof(EntityGameObjectLinker))); return; } EntityId spatialEntityId; if (hasSpatialEntityId) { spatialEntityId = entityManager.GetComponentData <SpatialEntityId>(entity).EntityId; } else // worker entity { spatialEntityId = WorkerEntityId; } gameObjectComponentTypes.Clear(); foreach (var component in gameObject.GetComponents <Component>()) { if (ReferenceEquals(component, null)) { continue; } var componentType = component.GetType(); if (gameObjectComponentTypes.Contains(componentType)) { worker.LogDispatcher.HandleLog(LogType.Warning, new LogEvent( "GameObject contains multiple instances of the same component type. Only one instance of each component type will be added to the corresponding ECS entity.") .WithField("EntityId", spatialEntityId) .WithField("ComponentType", componentType)); continue; } gameObjectComponentTypes.Add(componentType); viewCommandBuffer.AddComponent(entity, component.GetType(), component); } viewCommandBuffer.AddComponent(entity, new GameObjectReference { GameObject = gameObject }); var spatialOSComponent = gameObject.AddComponent <SpatialOSComponent>(); spatialOSComponent.World = world; spatialOSComponent.Worker = worker; spatialOSComponent.Entity = entity; spatialOSComponent.SpatialEntityId = spatialEntityId; }
public void Setup() { world = new World("TestWorld"); entityManager = world.GetOrCreateManager <EntityManager>(); entityGameObjectLinker = new EntityGameObjectLinker(world, new LoggingDispatcher()); testGameObject = new GameObject(); testEntity = entityManager.CreateEntity(); commandBuffer = new ViewCommandBuffer(entityManager, new LoggingDispatcher()); }
public void Setup() { world = new World("TestWorld"); entityManager = world.GetOrCreateManager <EntityManager>(); worker = world.CreateManager <WorkerSystem>(null, new LoggingDispatcher(), "TestWorker", Vector3.zero); entityGameObjectLinker = new EntityGameObjectLinker(world, worker); testGameObject = new GameObject(); testEntity = entityManager.CreateEntity(); viewCommandBuffer = new ViewCommandBuffer(entityManager, new LoggingDispatcher()); entityCommandBuffer = new EntityCommandBuffer(Allocator.TempJob); }
public void LinkGameObjectToEntity_adds_duplicate_GameObject_components_only_once() { testGameObject.AddComponent <TestMonoBehaviour>(); testGameObject.AddComponent <TestMonoBehaviour>(); var viewCommandBuffer = new ViewCommandBuffer(); entityGameObjectLinker.LinkGameObjectToEntity(testGameObject, testEntity, testSpatialEntityId, viewCommandBuffer); viewCommandBuffer.FlushBuffer(new MutableView(world, new LoggingDispatcher())); Assert.IsTrue(entityManager.HasComponent <TestMonoBehaviour>(testEntity)); }
private static void LinkEntityToGameObject(EntityId entityId, Entity entity, GameObject gameObject, World world) { var manager = world.GetOrCreateManager <EntityManager>(); var worker = world.GetExistingManager <WorkerSystem>(); worker.EntityIdToEntity.Add(entityId, entity); var viewCommandBuffer = new ViewCommandBuffer(manager, worker.LogDispatcher); viewCommandBuffer.AddComponent(entity, new GameObjectReference { GameObject = gameObject }); viewCommandBuffer.FlushBuffer(); }
protected override void OnCreateManager(int capacity) { base.OnCreateManager(capacity); worker = Worker.GetWorkerFromWorld(World); if (!SystemConfig.UnityClient.Equals(worker.WorkerType) && !SystemConfig.UnityGameLogic.Equals(worker.WorkerType)) { worker.LogDispatcher.HandleLog(LogType.Error, new LogEvent(UnsupportedArchetype) .WithField(LoggingUtils.LoggerName, LoggerName) .WithField("WorldName", World.Name) .WithField("WorkerType", worker)); } viewCommandBuffer = new ViewCommandBuffer(EntityManager, worker.LogDispatcher); }
public void TryGetGameObjectForEntity_returns_true_when_gameobject_is_linked() { worker.EntityIdToEntity.Add(testSpatialEntityId, testEntity); var viewCommandBuffer = new ViewCommandBuffer(entityManager, worker.LogDispatcher); viewCommandBuffer.AddComponent(testEntity, new GameObjectReference { GameObject = testGameObject }); viewCommandBuffer.FlushBuffer(); var component = testGameObject.GetComponent <SpatialOSComponent>(); Assert.NotNull(component); var succeeded = component.TryGetGameObjectForSpatialOSEntityId(testSpatialEntityId, out var linkedGameObject); Assert.IsTrue(succeeded); Assert.AreEqual(testGameObject, linkedGameObject); }
protected override void OnCreateManager() { base.OnCreateManager(); viewCommandBuffer = new ViewCommandBuffer(EntityManager, worker.LogDispatcher); }