Permalink
Cannot retrieve contributors at this time
EntityComponentSystemSamples/ECSSamples/Assets/HelloCube/4. SpawnFromMonoBehaviour/Spawner_FromMonoBehaviour.cs /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
34 lines (30 sloc)
1.31 KB
This file contains 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.Transforms; | |
| using UnityEngine; | |
| // ReSharper disable once InconsistentNaming | |
| [AddComponentMenu("DOTS Samples/SpawnFromMonoBehaviour/Spawner")] | |
| public class Spawner_FromMonoBehaviour : MonoBehaviour | |
| { | |
| public GameObject Prefab; | |
| public int CountX = 100; | |
| public int CountY = 100; | |
| void Start() | |
| { | |
| // Create entity prefab from the game object hierarchy once | |
| var settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, null); | |
| var prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(Prefab, settings); | |
| var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager; | |
| for (var x = 0; x < CountX; x++) | |
| { | |
| for (var y = 0; y < CountY; y++) | |
| { | |
| // Efficiently instantiate a bunch of entities from the already converted entity prefab | |
| var instance = entityManager.Instantiate(prefab); | |
| // Place the instantiated entity in a grid with some noise | |
| var position = transform.TransformPoint(new float3(x * 1.3F, noise.cnoise(new float2(x, y) * 0.21F) * 2, y * 1.3F)); | |
| entityManager.SetComponentData(instance, new Translation {Value = position}); | |
| } | |
| } | |
| } | |
| } |