Exemple #1
0
    void Spawn()
    {
        PrefabHolder prefabHolder       = PrefabHolder.GetActive();
        GameObject   prefab             = prefabHolder.prefab;
        int          numberToSpawn      = prefabHolder.numberToSpawn;
        float        spawnAreaSize      = prefabHolder.spawnAreaSize;
        float        accelerationScaler = prefabHolder.accelerationScaler;

        Entity        prefabEntity  = GameObjectConversionUtility.ConvertGameObjectHierarchy(prefab, GameObjectConversionSettings.FromWorld(this.World, null));
        EntityManager entityManager = this.World.EntityManager;

        Unity.Mathematics.Random rand = new Unity.Mathematics.Random(0x6E624EB7u);

        for (int i = 0; i < numberToSpawn; i++)
        {
            Entity instance = entityManager.Instantiate(prefabEntity);

            float  randomAngle   = rand.NextFloat();
            float  randomRadius  = rand.NextFloat();
            float2 pointOnCircle = GetPointOnCircle(randomAngle) * randomRadius;
            float3 randFloat3    = new float3(pointOnCircle.x, 0, pointOnCircle.y) * spawnAreaSize;

            entityManager.SetComponentData(instance, new Translation {
                Value = randFloat3
            });
            entityManager.SetComponentData(instance, new Acceleration {
                Value = -randFloat3 * accelerationScaler
            });
        }
    }
    protected override void OnCreate()
    {
        m_Group = GetEntityQuery(typeof(Translation), typeof(Speed), typeof(UpdateOrder));

        GameObject prefab        = PrefabHolder.GetActive().prefab;
        int        numberToSpawn = PrefabHolder.GetActive().numberToSpawn;

        Entity prefabEntity = GameObjectConversionUtility.ConvertGameObjectHierarchy(prefab, GameObjectConversionSettings.FromWorld(this.World, null));

        for (int i = 0; i < numberToSpawn; i++)
        {
            Entity      entity      = EntityManager.Instantiate(prefabEntity);
            UpdateOrder updateOrder = EntityManager.GetSharedComponentData <UpdateOrder>(entity);

            updateOrder.Value = i % updateFrequency;

            EntityManager.SetSharedComponentData(entity, updateOrder);
        }
    }