Advertisement
DMeville

Untitled

Mar 19th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. using Unity.Entities;
  6. using Unity.Transforms;
  7. using Unity.Rendering;
  8. using Unity.Mathematics;
  9. using Unity.Collections;
  10.  
  11. public class Bootstrap:MonoBehaviour {
  12.     //doesn't have to be a monobehaviour, but needs to be run before the scene starts?
  13.  
  14.     private static EntityManager entityManager;
  15.     private static RenderMesh entityRenderer; //this can be shared by OBJECTS THAT ARE THE SAME (mesh, colour maybe?) (used to be MeshInstanceRenderer)
  16.  
  17.     private static EntityArchetype toastArchtype; //Can create an entity using the archtype, which speeds up some stuff under the hood (how/where in mem the entity is stored, so it's by it's friends and can be processed fast)
  18.    
  19.     public static Mesh mesh;
  20.    
  21.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] //nothing special or important here, just triggering a method to run at a specific time (startup before scene load)
  22.     public static void Initialize() {
  23.  
  24.         //create our entity manager
  25.         //create any archtypes we are going to be using?
  26.  
  27.         entityManager = World.Active.GetOrCreateManager<EntityManager>();
  28.         toastArchtype = entityManager.CreateArchetype(typeof(Translation), typeof(Rotation), typeof(RenderMesh)); //TransformMatrix is not used anymore? Any entity with pos/rot/scale gets one automatically under the hood?
  29.        
  30.     }
  31.  
  32.    
  33.  
  34.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
  35.     public static void SceneStartup() {
  36.  
  37.         entityRenderer.mesh = GameObject.FindObjectOfType<MeshFilter>().mesh;
  38.         entityRenderer.material = GameObject.FindObjectOfType<MeshRenderer>().material;
  39.  
  40.         //NativeArray<Entity> el = new NativeArray<Entity>(50, Allocator.Temp); //create an list to store the entities we are about to create
  41.         for(int c = 0; c < 50; c++) {
  42.             Entity e = entityManager.CreateEntity(toastArchtype); //create the entity from the archtype template (x50).  Since we create from template, we don't need to add components and then set them, just set them?
  43.  
  44.             entityManager.SetComponentData<Translation>(e, new Translation { Value = new float3(0f, 0f, 0) }); //assign some values
  45.             entityManager.SetComponentData<Rotation>(e, new Rotation { Value = new quaternion(0, 0, 0, 1) });
  46.             entityManager.SetSharedComponentData<RenderMesh>(e, entityRenderer); //setting this to the entityRenderer component *should* render it? Need to somehow set entityRenderer.mesh though. Not sure where the entry point is
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement