using System.Collections.Generic; using Unity.Collections; using Unity.Entities; using Unity.Jobs; using Unity.Mathematics; using Unity.Rendering; using Unity.Transforms; using UnityEngine; using UnityEngine.Rendering; public class AddComponentsExample : MonoBehaviour { public Mesh Mesh; public Material m_material; public bool m_differentMaterial = false; public int m_w = 30; public int m_h = 30; // Example Burst job that creates many entities [GenerateTestsForBurstCompatibility] public struct SpawnJob : IJobParallelFor { public Entity Prototype; public int w; public int h; public bool singleMat; public EntityCommandBuffer.ParallelWriter Ecb; public void Execute(int index) { // Clone the Prototype entity to create a new entity. var e = Ecb.Instantiate(index, Prototype); // Prototype has all correct components up front, can use SetComponent to // set values unique to the newly created entity, such as the transform. int matIndex = singleMat ? 0 : index; Ecb.SetComponent(index, e, MaterialMeshInfo.FromRenderMeshArrayIndices(matIndex, 0)); Ecb.SetComponent(index, e, new LocalToWorld {Value = ComputeTransform(index)}); } public float4x4 ComputeTransform(int index) { int y = index / w; int x = index % h; float3 pos = new float3(x - (float)w * 0.5f, 0, y - (float)h * 0.5f); return float4x4.Translate(pos); } } void Start() { var world = World.DefaultGameObjectInjectionWorld; var entityManager = world.EntityManager; EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.TempJob); // Create a RenderMeshDescription using the convenience constructor // with named parameters. var desc = new RenderMeshDescription( shadowCastingMode: ShadowCastingMode.Off, receiveShadows: false); int objCount = m_w * m_h; var matList = new List(); if ( m_differentMaterial ) { for (int i=0;i