Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What wrong with my code when i create an entity at runtime?

Discussion in 'Entity Component System' started by UNDETON, Dec 29, 2018.

  1. UNDETON

    UNDETON

    Joined:
    Aug 20, 2018
    Posts:
    8
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Mathematics;
    3. using Unity.Rendering;
    4. using Unity.Transforms;
    5. using UnityEngine;
    6.  
    7. public class CubeSpawner : JobComponentSystem
    8. {
    9.     private static EntityManager entityManager;
    10.     private static EntityArchetype cubeArchetype;
    11.     public static MeshInstanceRenderer cubeRenderer;
    12.     public static Entity entity;
    13.  
    14.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    15.     public static void Initialize()
    16.     {
    17.         entityManager = World.Active.GetOrCreateManager<EntityManager>();
    18.         cubeArchetype = entityManager.CreateArchetype
    19.             (
    20.             typeof(Position),
    21.             typeof(MeshInstanceRenderer),
    22.             typeof(LocalToWorld)
    23.             );
    24.         Spawn();
    25.     }
    26.  
    27.     protected override void OnCreateManager()
    28.     {
    29.         base.OnCreateManager();
    30.         Spawn();
    31.     }
    32.     private static void Spawn()
    33.     {
    34.         // Prepare cube entity prefab .
    35.         GameObject cube = Resources.Load("cube") as GameObject;
    36.         // I can done this way.
    37.         Entity entity1 = entityManager.Instantiate(cube);
    38.  
    39.         // Entity doesn't show up in scene. Why?
    40.         Entity entity2 = entityManager.CreateEntity(cubeArchetype);
    41.         entityManager.SetComponentData(entity2, new Position { Value = new float3(6, 0, 0) });
    42.         entityManager.SetSharedComponentData(entity2, new MeshInstanceRenderer { mesh = cube.GetComponent<MeshInstanceRendererComponent>().Value.mesh });
    43.         Debug.Log("Done.");
    44.     }
    45.  
    46. }
    47.  
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    is Debug.Log("Done."); printing out?
    I assume you have 0 errors.

    Is cube material set to GPU instancing?

    upload_2018-12-29_17-15-30.png
     
  3. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,554
    Your MIR is missing a material?
     
  4. UNDETON

    UNDETON

    Joined:
    Aug 20, 2018
    Posts:
    8
    When i change this class inherit from Monobehaviour it work. But Why? Why not Job or System?
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Mathematics;
    3. using Unity.Rendering;
    4. using Unity.Transforms;
    5. using UnityEngine;
    6.  
    7. public class CubeSpawner : MonoBehaviour
    8. {
    9.     private EntityManager entityManager;
    10.     private EntityArchetype cubeArchetype;
    11.     public Entity entity;
    12.  
    13.     private void Start()
    14.     {
    15.         entityManager = World.Active.GetOrCreateManager<EntityManager>();
    16.         cubeArchetype = entityManager.CreateArchetype
    17.             (
    18.             typeof(Position),
    19.             typeof(MeshInstanceRenderer)
    20.             );
    21.         Spawn();
    22.     }
    23.  
    24.     private void Spawn()
    25.     {
    26.         // Prepare cube entity prefab .
    27.         GameObject cube = Resources.Load("cube") as GameObject;
    28.  
    29.         Entity entity2 = entityManager.CreateEntity(cubeArchetype);
    30.         entityManager.SetComponentData(entity2, new Position { Value = new float3(6, 0, 0) });
    31.         entityManager.SetSharedComponentData(entity2, cube.GetComponent<MeshInstanceRendererComponent>().Value );
    32.         Debug.Log("Done.");
    33.     }
    34.  
    35. }
    36.  
     
    Last edited: Jan 16, 2019
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    I'm not certain Initialize runs before OnCreateManager

    Have you tested that?