public void UpdateInjectedComponentGroups_WhenObjectWithMatchingComponentExists_ComponentArrayIsPopulated()
        {
            var c = m_GameObject.AddComponent <BoxCollider>();

            GameObjectEntity.AddToEntityManager(m_Manager, m_GameObject);
            var manager = World.GetOrCreateManager <GameObjectArraySystem>();

            manager.UpdateInjectedComponentGroups();

            Assert.That(manager.group.colliders.ToArray(), Is.EqualTo(new[] { c }));
        }
Пример #2
0
        public static void InitializeWithScene()
        {
            var settingsGO = GameObject.Find("Settings");

            Settings = settingsGO?.GetComponent <Settings>() ?? new Settings();
            EntityManager em = World.Active.GetOrCreateManager <EntityManager>();

            // Initialize hybrid factory archetype with components from initial factory settings
            FactorySettings[] factories          = UnityEngine.Object.FindObjectsOfType <FactorySettings>();
            List <GameObject> outputConnectedGos = new List <GameObject>();

            foreach (FactorySettings factory in factories)
            {
                GameObject go = factory.gameObject;
                go.AddComponent <EnabledRecipesComponent>().Recipes = factory.EnabledRecipes;
                go.AddComponent <ProcessComponent>().ItemBufferIn   = Inventory.StackSettings.GetStacks(factory.StartingInputItems);
                go.AddComponent <ConnectorInComponent>();
                if (factory.OutputConnected != null)
                {
                    outputConnectedGos.Add(go);
                }
            }

            // Connect inputs/outputs from initial factory settings
            foreach (GameObject go in outputConnectedGos)
            {
                ConnectorInComponent otherConnector = go.GetComponent <FactorySettings>()?.OutputConnected?.GetComponent <ConnectorInComponent>();
                if (otherConnector != null)
                {
                    int guid = Guid.NewGuid().GetHashCode();
                    otherConnector.Guid = guid;
                    go.AddComponent <ConnectorOutComponent>().OtherConnectorGuid = guid;
                }
            }

            // Setup players
            PlayerSettings[] players = UnityEngine.Object.FindObjectsOfType <PlayerSettings>();

            // Add game objects with initialized components to ECS
            foreach (FactorySettings factory in factories)
            {
                Entity factoryEntity = GameObjectEntity.AddToEntityManager(em, factory.gameObject); // hybrid ECS
                em.AddComponentData(factoryEntity, new Interactable {
                    Focus = 0
                });
            }

            foreach (PlayerSettings player in players)
            {
                Entity playerEntity = GameObjectEntity.AddToEntityManager(em, player.gameObject);
                em.AddComponentData(playerEntity, new Interactor());
            }
        }
        public void GetComponentGroup_WhenObjectWithMatchingComponentExists_ComponentArrayIsPopulated()
        {
            var rb = m_GameObject.AddComponent <Rigidbody>();

            GameObjectEntity.AddToEntityManager(m_Manager, m_GameObject);

            var grp = EmptySystem.GetComponentGroup(typeof(Rigidbody));

            var array = grp.GetComponentArray <Rigidbody>();

            Assert.That(array.ToArray(), Is.EqualTo(new[] { rb }));
        }
Пример #4
0
    // EntityArchetype entityArchetype;

    void Start()
    {
        var particles = new List <DynamicBoneParticle> ();
        var bones     = new List <Transform> ();

        GenerateParticles(particles, bones);

        entityManager = World.Active.GetExistingManager <EntityManager> ();
        // entityArchetype = entityManager.CreateArchetype (
        //     ComponentType.Create<DynamicBoneRoot> (),
        //     ComponentType.FixedArray (typeof (DynamicBoneParticle), particles.Count)
        // );

        // var entity = bones[0].gameObject.AddComponent<GameObjectEntity> ().Entity;
        var entity = GameObjectEntity.AddToEntityManager(entityManager, bones[0].gameObject);

        entityManager.AddComponentData(entity, new DynamicBoneRoot()
        {
            length             = particles.Count,
            rootInvertRotation = Quaternion.Inverse(root.rotation),
            objectMove         = new float3(),
            objectPrevPosition = root.position
        });

        entityManager.AddComponentData(entity, new DynamicBoneTransform()
        {
            owner             = entity,
            index             = 0,
            initLocalPosition = bones[0].localPosition,
            initLocalRotation = bones[0].localRotation,
        });

        entityManager.AddComponent(entity, ComponentType.FixedArray(typeof(DynamicBoneParticle), particles.Count));
        var particleArray = entityManager.GetFixedArray <DynamicBoneParticle> (entity);

        for (int i = 0; i < particles.Count; i++)
        {
            particleArray[i] = particles[i];
        }

        for (int i = 1; i < particles.Count; i++)
        {
            // var boneEntity = bones[i].gameObject.AddComponent<GameObjectEntity> ().Entity;
            var boneEntity = GameObjectEntity.AddToEntityManager(entityManager, bones[i].gameObject);
            entityManager.AddComponentData(boneEntity, new DynamicBoneTransform()
            {
                owner             = entity,
                index             = i,
                initLocalPosition = bones[i].localPosition,
                initLocalRotation = bones[i].localRotation,
            });
        }
    }
Пример #5
0
    internal static void CreateEntitiesForGameObjectsRecurse(Transform transform, EntityManager gameObjectEntityManager, HashSet <GameObject> gameObjects)
    {
        GameObjectEntity.AddToEntityManager(gameObjectEntityManager, transform.gameObject);
        if (gameObjects != null)
        {
            gameObjects.Add(transform.gameObject);
        }

        foreach (Transform child in transform)
        {
            CreateEntitiesForGameObjectsRecurse(child, gameObjectEntityManager, gameObjects);
        }
    }
    internal static void CreateEntitiesForGameObjectsRecurse(Transform transform, EntityManager gameObjectEntityManager, HashSet <GameObject> gameObjects)
    {
        GameObjectEntity.AddToEntityManager(gameObjectEntityManager, transform.gameObject);
        if (gameObjects != null)
        {
            gameObjects.Add(transform.gameObject);
        }

        int childCount = transform.childCount;

        for (int i = 0; i != childCount; i++)
        {
            CreateEntitiesForGameObjectsRecurse(transform.GetChild(i), gameObjectEntityManager, gameObjects);
        }
    }
        public void RigidbodyComponentArray()
        {
            var go = new GameObject("test", typeof(Rigidbody));

            /*var entity =*/ GameObjectEntity.AddToEntityManager(m_Manager, go);

            var grp = m_Manager.CreateComponentGroup(typeof(Rigidbody));

            var arr = grp.GetComponentArray <Rigidbody>();

            Assert.AreEqual(1, arr.Length);
            Assert.AreEqual(go.GetComponent <Rigidbody>(), arr[0]);

            Object.DestroyImmediate(go);
        }
        public void ComponentsAddedOrNotBasedOnEnabledFlag()
        {
            var ge = new GameObject("with enabled");

            ge.AddComponent <MeshRenderer>().enabled = true;
            var gd = new GameObject("with disabled");

            gd.AddComponent <MeshRenderer>().enabled = false;

            var ee = GameObjectEntity.AddToEntityManager(m_Manager, ge);
            var ed = GameObjectEntity.AddToEntityManager(m_Manager, gd);

            Assert.That(m_Manager.HasComponent <MeshRenderer>(ee), Is.True);
            Assert.That(m_Manager.HasComponent <MeshRenderer>(ed), Is.False);
        }
        public void TransformArrayWorksWithTransformAccessArray()
        {
            var go = new GameObject("test");

            GameObjectEntity.AddToEntityManager(m_Manager, go);

            var manager = World.GetOrCreateManager <TransformWithTransformAccessSystem>();

            manager.UpdateInjectedComponentGroups();

            Assert.AreEqual(1, manager.group.CalculateLength());
            Assert.AreEqual(manager.group.GetComponentArray <Transform>()[0].gameObject, manager.group.GetTransformAccessArray()[0].gameObject);

            Object.DestroyImmediate(go);
            TearDown();
        }
        public void ComponentDataAndTransformArray()
        {
            var go     = new GameObject("test", typeof(EcsTestComponent));
            var entity = GameObjectEntity.AddToEntityManager(m_Manager, go);

            m_Manager.SetComponentData(entity, new EcsTestData(5));

            var grp = m_Manager.CreateComponentGroup(typeof(Transform), typeof(EcsTestData));
            var arr = grp.GetComponentArray <Transform>();

            Assert.AreEqual(1, arr.Length);
            Assert.AreEqual(go.transform, arr[0]);
            Assert.AreEqual(5, grp.GetComponentDataArray <EcsTestData>()[0].value);

            Object.DestroyImmediate(go);
        }
Пример #11
0
    void Start()
    {
        WavePlaneSystem.useTransformJob = useTransformJob;
        entityManager = World.Active.GetExistingManager <EntityManager> ();

        // var entity = gameObject.AddComponent<GameObjectEntity> ().Entity;
        entity = GameObjectEntity.AddToEntityManager(entityManager, gameObject);
        entityManager.AddComponent(entity, typeof(PlaneWaveTag));

        if (!WavePlaneSystem.useTransformJob)
        {
            entityManager.AddComponent(entity, typeof(Position));
            entityManager.AddComponent(entity, typeof(CopyTransformFromGameObject));
            entityManager.AddComponent(entity, typeof(CopyTransformToGameObject));
        }
    }
Пример #12
0
    Entity RegisterInternal(GameObject gameObject, bool isDynamic)
    {
        // If gameObject has GameObjectEntity it is already registered in entitymanager. If not we register it here
        var gameObjectEntity = gameObject.GetComponent <GameObjectEntity>();

        if (gameObjectEntity == null)
        {
            GameObjectEntity.AddToEntityManager(m_EntityManager, gameObject);
        }

        if (isDynamic)
        {
            m_dynamicEntities.Add(gameObject);
        }

        return(gameObjectEntity != null ? gameObjectEntity.Entity : Entity.Null);
    }
        public void GameObjectArrayIsPopulated()
        {
            var go = new GameObject("test", typeof(BoxCollider));

            GameObjectEntity.AddToEntityManager(m_Manager, go);

            var manager = World.GetOrCreateManager <GameObjectArraySystem>();

            manager.UpdateInjectedComponentGroups();

            Assert.AreEqual(1, manager.group.Length);
            Assert.AreEqual(go, manager.group.gameObjects[0]);
            Assert.AreEqual(go, manager.group.colliders[0].gameObject);

            Object.DestroyImmediate(go);
            TearDown();
        }
Пример #14
0
    void Awake()
    {
        // ugly singleton so we can read the movement speed at runtime from the movement system
        Instance = this;
        // use default world and entity manager so we can use existing systems
        EntityManager entityManager = World.Active.GetExistingManager <EntityManager>();

        if (pureECS) // avoid using GameObjects alltogether
        {
            // setup archetype
            EntityArchetype archetype = entityManager.CreateArchetype(
                ComponentType.Create <Position>(),
                ComponentType.Create <MoveInDirection>(),
                ComponentType.Create <TransformMatrix>(),
                ComponentType.Create <EmptyComponent>());
            // get instance renderer
            MeshInstanceRenderer meshInstanceRenderer = new MeshInstanceRenderer();
            var proto = Instantiate(ECSPrefab);
            meshInstanceRenderer = proto.GetComponent <MeshInstanceRendererComponent>().Value;
            Destroy(proto);
            // setup entities
            for (int j = 0; j < count; j++)
            {
                Entity entity = entityManager.CreateEntity(archetype);
                entityManager.SetComponentData(entity, new MoveInDirection(Random.insideUnitSphere));
                // add shared instance renderer
                entityManager.AddSharedComponentData(entity, meshInstanceRenderer);
            }
        }
        else // use GameObjects
        {
            for (int i = 0; i < count; i++)
            {
                // instanciate prefab
                GameObject go = Instantiate(UsualPrefab, transform);
                // get entity
                Entity entity = GameObjectEntity.AddToEntityManager(entityManager, go);
                // setup components for moving
                entityManager.AddComponentData(entity, new Position());
                entityManager.AddComponentData(entity, new MoveInDirection(Random.insideUnitSphere));
            }
        }
    }
Пример #15
0
    void Initialization_Pool()
    {
        GameObject  sourcePrefab = new GameObject();
        AudioSource audioSource  = sourcePrefab.AddComponent <AudioSource>();

        AudioService.ResetAudioSource(audioSource);

        for (int i = 0; i < _poolSize; i++)
        {
            GameObject sourceGO = Instantiate(sourcePrefab);
            Entity     entity   = GameObjectEntity.AddToEntityManager(s_entityManager, sourceGO);
            s_entityManager.AddComponentData(entity, new Position());
            s_entityManager.AddComponentData(entity, new CopyTransformToGameObject());
            s_entityManager.AddComponentData(entity, new AudioSourceHandle(entity));
            sourceGO.name = "Source " + sourceGO.GetComponent <AudioSource>().GetInstanceID();
        }

        Destroy(sourcePrefab);
    }
Пример #16
0
        public static Entity RotateGameObjectLocal(
            GameObject go,
            float time,
            quaternion to, quaternion from,
            EaseType easeType = EaseType.linear,
            int loop          = 0, bool pingPong = false,
            int tagId         = 0)
        {
            var entityManager = World.Active.EntityManager;
            var entity        = GameObjectEntity.AddToEntityManager(entityManager, go);

            Debug.Log("Entity: " + entity.Index);
            entityManager.AddComponentData(entity, new Rotation {
                Value = from
            });
            entityManager.AddComponentData(entity, new TweenGameObjectLocal());
            RotateEntity(entity, time, to, from, easeType, loop, pingPong, tagId);
            return(entity);
        }
Пример #17
0
    void Start()
    {
        cubes = GetComponent <SphereGenerator> ().GetTransforms();
        depth = GetDepth(cubes[0]);

        entityManager   = World.Active.GetExistingManager <EntityManager> ();
        entityArchetype = entityManager.CreateArchetype(
            ComponentType.Create <ChainRoot> (),
            ComponentType.FixedArray(typeof(ChainData), depth)
            );

        var time = Time.realtimeSinceStartup;

        for (int i = 0; i < cubes.Length; i++)
        {
            var entity = entityManager.CreateEntity(entityArchetype);
            entityManager.SetComponentData(entity, new ChainRoot()
            {
                Length = depth
            });

            var chain      = cubes[i].GetComponentsInChildren <Transform> ();
            var chainDatas = entityManager.GetFixedArray <ChainData> (entity);
            for (int j = 0; j < depth; j++)
            {
                chainDatas[j] = new ChainData()
                {
                    InitLocalPosition = chain[j].localPosition
                };
            }

            for (int j = 0; j < depth; j++)
            {
                // var subEntity = chain[j].gameObject.AddComponent<GameObjectEntity> ().Entity;
                var subEntity = GameObjectEntity.AddToEntityManager(entityManager, chain[j].gameObject);  // faster initialization
                entityManager.AddComponentData(subEntity, new Owner()
                {
                    Value = entity, Index = j
                });
            }
        }
        Debug.Log(Time.realtimeSinceStartup - time);
    }
Пример #18
0
    public override void OnPlayableCreate(Playable playable)
    {
        base.OnPlayableCreate(playable);

        if (!Application.isPlaying)
        {
            return;
        }

        //Debug.Log("creating marker " + Name);
        //var evt = new MarkerCreatedEvent(this);
        //Sequencer.EventManager.PublishMarkerCreatedEvent(evt);
        gameObject = new GameObject();
        //gameObject.AddComponent<GameObjectEntity>();
        var wrapper = gameObject.AddComponent <TimelineMarkerBehaviourWrapper>();

        wrapper.name = this.Name;
        wrapper.TimelineMarkerBehaviour = this;
        Entity = GameObjectEntity.AddToEntityManager(EntityManager, gameObject);
    }
Пример #19
0
        public static Entity RotateGameObject(
            GameObject go,
            float time,
            Quaternion to, Quaternion from,
            EaseType easeType = EaseType.linear,
            int loop          = 0, bool pingPong = false,
            int tagId         = 0)
        {
            quaternion fromq = new quaternion(from.x, from.y, from.z, from.w);
            quaternion toq   = new quaternion(to.x, to.y, to.z, to.w);

            var entityManager = World.Active.EntityManager;
            var entity        = GameObjectEntity.AddToEntityManager(entityManager, go);

            entityManager.AddComponentData(entity, new Rotation {
                Value = fromq
            });
            entityManager.AddComponentData(entity, new TweenGameObject());
            RotateEntity(entity, time, toq, fromq, easeType, loop, pingPong, tagId);
            return(entity);
        }
Пример #20
0
        public static Entity ScaleGameObject(
            GameObject go,
            float time,
            Vector3 to, Vector3 from,
            EaseType easeType = EaseType.linear,
            int loop          = -1, bool pingPong = false,
            int tagId         = 0)
        {
            float3 from3 = new float3(from.x, from.y, from.z);
            float3 to3   = new float3(to.x, to.y, to.z);

            var entityManager = World.Active.EntityManager;
            var entity        = GameObjectEntity.AddToEntityManager(entityManager, go);

            entityManager.AddComponentData(entity, new Translation {
                Value = from3
            });
            entityManager.AddComponentData(entity, new TweenGameObject());
            ScaleEntity(entity, time, to3, from3, easeType, loop, pingPong, tagId);
            return(entity);
        }
        unsafe public void ComponentEnumerator()
        {
            var go     = new GameObject("test", typeof(Rigidbody), typeof(Light));
            var entity = GameObjectEntity.AddToEntityManager(m_Manager, go);

            m_Manager.AddComponentData(entity, new EcsTestData(5));
            m_Manager.AddComponentData(entity, new EcsTestData2(6));

            int iterations = 0;

            foreach (var e in EmptySystem.GetEntities <MyEntity>())
            {
                Assert.AreEqual(5, e.testData->value);
                Assert.AreEqual(6, e.testData2->value0);
                Assert.AreEqual(go.GetComponent <Light>(), e.light);
                Assert.AreEqual(go.GetComponent <Rigidbody>(), e.rigidbody);
                iterations++;
            }
            Assert.AreEqual(1, iterations);

            Object.DestroyImmediate(go);
        }
Пример #22
0
    Entity RegisterInternal(GameObject gameObject, bool isDynamic, World ecsWorld = null)
    {
        // If gameObject has GameObjectEntity it is already registered in entitymanager. If not we register it here
        var gameObjectEntity = gameObject.GetComponent <GameObjectEntity>();

        var em = ecsWorld != null ? ecsWorld.EntityManager : m_EntityManager;

        Entity newEntity = Entity.Null;

        if (gameObjectEntity == null)
        {
            newEntity        = GameObjectEntity.AddToEntityManager(em, gameObject);
            gameObjectEntity = gameObject.GetComponent <GameObjectEntity>();
        }

        if (isDynamic)
        {
            m_dynamicEntities.Add(gameObject);
        }

        return(gameObjectEntity.Entity);
    }
        public void GameObjectArrayWorksWithTransformAccessArray()
        {
            var hook = new GameObjectArrayInjectionHook();

            InjectionHookSupport.RegisterHook(hook);

            var go = new GameObject("test");

            GameObjectEntity.AddToEntityManager(m_Manager, go);

            var manager = World.GetOrCreateManager <GameObjectArrayWithTransformAccessSystem>();

            manager.UpdateInjectedComponentGroups();

            Assert.AreEqual(1, manager.group.CalculateLength());
            Assert.AreEqual(go, manager.group.GetGameObjectArray()[0]);
            Assert.AreEqual(go, manager.group.GetTransformAccessArray()[0].gameObject);

            Object.DestroyImmediate(go);

            InjectionHookSupport.UnregisterHook(hook);

            TearDown();
        }
Пример #24
0
        void OnCollisionEnter(UnityEngine.Collision collisionInfo)
        {
            GameObjectEntity otherGameObjectEntity = collisionInfo.gameObject.GetComponent <GameObjectEntity>();

            if (!otherGameObjectEntity)
            {
                return;
            }
            Entity sourceEntity  = GetComponent <GameObjectEntity>().Entity;
            var    entityManager = World.Active.GetExistingManager <EntityManager>();
            //Debug.Log("before creating!!");

            Entity collisionEventEntity = entityManager.CreateEntity(typeof(CollisionData));
            var    tempObj = new GameObject();


            var data = tempObj.AddComponent <CollisionData>();

            data.source = this.gameObject;
            data.other  = collisionInfo.gameObject;
            //entityManager.SetComponentData(collisionEventEntity,
            GameObjectEntity.AddToEntityManager(entityManager, tempObj);
            //Debug.Log("after setting");
        }