Created
May 16, 2018 20:30
-
-
Save unity3dcollege/8967e0d6eb1eded319cc2427962908ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
using Unity.Mathematics; | |
using Unity.Rendering; | |
using Unity.Transforms; | |
using Unity.Transforms2D; | |
using UnityEngine; | |
public class CubeSpawner : MonoBehaviour | |
{ | |
private static EntityManager entityManager; | |
private static MeshInstanceRenderer cubeRenderer; | |
private static EntityArchetype cubeArchetype; | |
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | |
public static void Initalize() | |
{ | |
entityManager = World.Active.GetOrCreateManager<EntityManager>(); | |
cubeArchetype = entityManager.CreateArchetype( | |
typeof(Position2D), | |
typeof(Heading2D), | |
typeof(TransformMatrix), | |
typeof(MoveSpeed)); | |
} | |
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] | |
public static void InitalizeWithScene() | |
{ | |
cubeRenderer = GameObject.FindObjectOfType<MeshInstanceRendererComponent>().Value; | |
for (int i = 0; i < 100000; i++) | |
{ | |
SpawnCube(); | |
} | |
} | |
private static void SpawnCube() | |
{ | |
Entity cubeEntity = entityManager.CreateEntity(cubeArchetype); | |
float2 direction = new float2(UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f)); | |
entityManager.SetComponentData(cubeEntity, new Position2D { Value = new float2(0, 0) }); | |
entityManager.SetComponentData(cubeEntity, new Heading2D { Value = direction }); | |
entityManager.SetComponentData(cubeEntity, new MoveSpeed { speed = 1 }); | |
entityManager.AddSharedComponentData(cubeEntity, cubeRenderer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment