示例#1
0
    void Awake()
    {
        instance = this;

        float screenRatio = (float)Screen.width / (float)Screen.height;
        float targetRatio = ((float)GridWidth * CellSize) / ((float)GridHeight * CellSize);

        if (screenRatio >= targetRatio)
        {
            Camera.main.orthographicSize = ((float)GridHeight * CellSize) / 2;
        }
        else
        {
            float differenceInSize = targetRatio / screenRatio;
            Camera.main.orthographicSize = ((float)GridHeight * CellSize) / 2 * differenceInSize;
        }

        //Grab Entity Manager
        em = World.DefaultGameObjectInjectionWorld.EntityManager;

        //Cell ArchType
        CellArchtype = em.CreateArchetype(
            typeof(LocalToWorld),
            typeof(Translation),
            typeof(Rotation),
            typeof(NonUniformScale),
            typeof(CellComponent));

        // Generate our grid
        CreateGrid();
    }
示例#2
0
文件: Spawner.cs 项目: mhrfky/boids
    private void makeBait()
    {
        Unity.Entities.EntityManager eM = World.DefaultGameObjectInjectionWorld.EntityManager;
        EntityArchetype bait            = eM.CreateArchetype(
            typeof(Translation),
            typeof(RenderMesh),
            typeof(Scale),
            typeof(Rotation),
            typeof(RenderBounds),
            typeof(bait),
            typeof(LocalToWorld));
        Entity entity = eM.CreateEntity(bait);

        eM.AddComponentData(entity, new Translation
        {
            Value = new float3(0, 0, 0)
        }
                            );
        eM.SetSharedComponentData(entity, new RenderMesh
        {
            mesh     = baitMesh,
            material = baitMaterial
        }
                                  );
    }
示例#3
0
        /// <summary>
        /// Creates all new entities described in the <see cref="EntityChangeSet"/>
        /// </summary>
        /// <remarks>
        /// This method only creates the entities and does not set any data.
        /// </remarks>
        private static unsafe void ApplyCreateEntities(
            EntityManager entityManager,
            EntityChangeSet changeSet,
            NativeMultiHashMap <int, Entity> packedEntities)
        {
            var types = stackalloc ComponentType[0];
            var entityGuidArchetype = entityManager.CreateArchetype(types, 0);

            using (var entities = new NativeArray <Entity>(changeSet.CreatedEntityCount, Allocator.Temp))
            {
                entityManager.CreateEntity(entityGuidArchetype, entities);
                for (var i = 0; i < changeSet.CreatedEntityCount; ++i)
                {
                    packedEntities.Add(i, entities[i]);
                }
            }
        }
示例#4
0
文件: Spawner.cs 项目: mhrfky/boids
    private void makeEntity(int i)
    {
        Unity.Entities.EntityManager eM = World.DefaultGameObjectInjectionWorld.EntityManager;
        EntityArchetype boid            = eM.CreateArchetype(
            typeof(Translation),
            typeof(RenderMesh),
            typeof(Scale),
            typeof(Rotation),
            typeof(RenderBounds),
            typeof(Flock_settings),
            typeof(LocalToWorld));
        Entity entity = eM.CreateEntity(boid);

        eM.AddComponentData(entity, new Translation
        {
            Value = new float3(i % 24, (int)i / 4, 0)
        }
                            );
        eM.AddComponentData(entity, new Flock_settings
        {
            direction         = new float3(0, 0, 0),
            flockHeading      = new float3(0, 0, 0),
            flockCentre       = new float3(0, 0, 0),
            separationHeading = new float3(0, 0, 0),
            velocity          = new float3(0, 0, 0),
            index             = i,
            numFlockmates     = 0
        }
                            );
        eM.SetSharedComponentData(entity, new RenderMesh
        {
            mesh     = boidMesh,
            material = boidMaterial
        }
                                  );
        eM.AddComponentData(entity, new Scale
        {
            Value = .1f
        }
                            );
    }
示例#5
0
        // TODO: Very wrong error messages when creating entity with empty ComponentType array?
        public static Entity AddToEntityManager(EntityManager entityManager, GameObject gameObject)
        {
            var goe = gameObject.GetComponent <GameObjectEntity>();

            if (goe == null)
            {
                goe = gameObject.AddComponent <GameObjectEntity>();
            }
            else if (goe.Entity != Entity.Null)
            {
                return(goe.Entity);
            }

            ComponentType[] types;
            Component[]     components;
            GetComponents(gameObject, true, out types, out components);

            EntityArchetype archetype;

            try
            {
                archetype = entityManager.CreateArchetype(types);
            }
            catch (Exception e)
            {
                for (int i = 0; i < types.Length; ++i)
                {
                    if (Array.IndexOf(types, types[i]) != i)
                    {
                        Debug.LogWarning($"GameObject '{gameObject}' has multiple {types[i]} components and cannot be converted, skipping.");
                        return(Entity.Null);
                    }
                }

                throw e;
            }

            goe.m_Entity = CreateEntity(entityManager, archetype, components, types);

            return(goe.m_Entity);
        }
示例#6
0
        unsafe public static Entity Instantiate(this EntityManager entityManager, GameObject srcGameObject)
        {
            var components = entityManager.m_CachedComponentList;

            srcGameObject.GetComponents(components);
            var            count          = components.Count;
            ComponentType *componentTypes = stackalloc ComponentType[count];

            for (var t = 0; t != count; ++t)
            {
                componentTypes[t] = components[t].GetComponentType(entityManager);
            }

            var srcEntity = entityManager.CreateEntity(entityManager.CreateArchetype(componentTypes, count));

            for (var t = 0; t != count; ++t)
            {
                components[t].UpdateComponentData(entityManager, srcEntity);
            }

            return(srcEntity);
        }
        public static unsafe Entity Instantiate(this EntityManager entityManager, GameObject srcGameObject)
        {
            srcGameObject.GetComponents(s_ReusableComponentList);
            var            count          = s_ReusableComponentList.Count;
            ComponentType *componentTypes = stackalloc ComponentType[count];

            for (var t = 0; t != count; ++t)
            {
                componentTypes[t] = s_ReusableComponentList[t].GetComponentType();
            }

            var srcEntity = entityManager.CreateEntity(entityManager.CreateArchetype(componentTypes, count));

            for (var t = 0; t != count; ++t)
            {
                s_ReusableComponentList[t].UpdateComponentData(entityManager, srcEntity);
            }

            s_ReusableComponentList.Clear();

            return(srcEntity);
        }
示例#8
0
 internal EntityArchetype CreateArchetype(ComponentType *types, int count)
 {
     return(m_Manager.CreateArchetype(types, count));
 }