private static void CreateAndSpawnEntities(EntityManager entityManager, int count, MeshInstanceRenderer look, float3 startPos, float3 floatDirection)
    {
        // if you spawn more entities, it's more performant to do it with NativeArray
        // if you want to spawn just one entity, do:
        // var entity = em.CreateEntity(EntityArchetype);
        var entities = new NativeArray <Entity>(count, Allocator.Temp);

        // Spawns entities and attaches all components from the floatyCubes archetype
        entityManager.CreateEntity(floatyCubeArchetype, entities);

        // Setting up start values for the components
        for (int i = 0; i < count; i++)
        {
            var floaterComponent = new FloaterComponent();
            floaterComponent.floatSpeed     = UnityEngine.Random.Range(1.0f, 5.0f);
            floaterComponent.floatDirection = new float3(
                floatDirection.x == 0 ? UnityEngine.Random.Range(-1.0f, 1.0f) : floatDirection.x,
                floatDirection.y == 0 ? UnityEngine.Random.Range(-1.0f, 1.0f) : floatDirection.y,
                floatDirection.z == 0 ? UnityEngine.Random.Range(-1.0f, 1.0f) : floatDirection.z
                );

            var rotatorComponent = new RotatorComponent();
            rotatorComponent.rotationSpeed = floaterComponent.floatSpeed;
            rotatorComponent.direction     = new float3(UnityEngine.Random.Range(-1.0f, 1.0f), UnityEngine.Random.Range(-1.0f, 1.0f), UnityEngine.Random.Range(-1.0f, 1.0f));

            entityManager.SetComponentData(entities[i], new Position()
            {
                Value = startPos
            });
            entityManager.SetComponentData(entities[i], new Rotation()
            {
                Value = quaternion.identity
            });
            entityManager.SetComponentData(entities[i], new Scale()
            {
                Value = new float3(1f, 1f, 1f)
            });
            entityManager.SetComponentData(entities[i], rotatorComponent);
            entityManager.SetComponentData(entities[i], floaterComponent);

            // This shared component decides the rendered look of the entity
            entityManager.AddSharedComponentData(entities[i], look);
        }

        // All NativeArray's need to be disposed of manually. This will not destroy any entities, just state that we will not be using the array anymore.
        entities.Dispose();
    }
Example #2
0
    protected override void OnStartRunning()
    {
        // For now we only support one emitter for the sake of simplicity
        var emitter = BootstrapperParticles.particleEmitterTemplate;

        randomEmissionDirections = new NativeArray <float3>(emitter.maxParticles, Allocator.Persistent);
        randomRotatorComponents  = new NativeArray <RotatorComponent>(emitter.maxParticles, Allocator.Persistent);

        for (int i = 0; i < emitter.maxParticles; i++)
        {
            randomEmissionDirections[i] = new float3
                                          (
                UnityEngine.Random.Range(-emitter.rangeX, emitter.rangeX),
                UnityEngine.Random.Range(-emitter.rangeY, emitter.rangeY),
                UnityEngine.Random.Range(-emitter.rangeZ, emitter.rangeZ)
                                          );

            var rotator = new RotatorComponent();
            rotator.rotationSpeed      = UnityEngine.Random.Range(1.0f, 5.0f);
            rotator.direction          = new float3(UnityEngine.Random.Range(-1.0f, 1.0f), UnityEngine.Random.Range(-1.0f, 1.0f), UnityEngine.Random.Range(-1.0f, 1.0f));
            randomRotatorComponents[i] = rotator;
        }
    }