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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Jobified Spawner - MeshInstanceRenderer Issue [PureECS]

Discussion in 'Graphics for ECS' started by avvie, May 15, 2018.

  1. avvie


    Jan 26, 2014
    [Disclaimer: I have no idea what i am doing, but it seemed to be working until now]

    I am trying to learn in babysteps. So at the moment i am trying to write a system that will spawn x amount of stuff. As long as the spawner exists it will check if there exist the correct amount of stuff in the scene and if so then it will do nothing. Given that it runs the defined job in the first frame.

    I managed to make it work BUT my cubes had no renderer no messes nothing. So I am attempting to add that.
    The job looks like this

    Code (CSharp):
    2. struct SpawnCubes : IJob {
    3.      [ReadOnly]public float count;
    4.     [ReadOnly]public float radius;
    5.     public EntityCommandBuffer CommandBuffer;
    6.     [ReadOnly]public EntityArchetype Cube;
    7.     [ReadOnly]public MeshInstanceRenderer mir;
    8.     public void Execute(){
    9.         for (int i = 0; i < count; i++){
    10.             // Debug.Log(i);
    11.             CommandBuffer.CreateEntity(Cube);
    12.             CommandBuffer.SetSharedComponent<MeshInstanceRenderer>(mir);
    13.         }
    14.     }
    15. }
    I get no compile errors but at runtime:
    InvalidOperationException: SpawnCubes.mir.mesh is not a value type. Job structs may not contain any reference types.

    I literally wasnt expecting this one :D I find there is a lot of juice in having jobified spawners that i can pass a few MeshInstanceRenderers and spawn a lot of the same with different looks.
    Is there a way to do this inside a job?

    EDIT: Before the I added the line "CommandBuffer.SetSharedComponent<MeshInstanceRenderer>(mir);" In the entity debugger i was getting the 20 entities i am trying to spawn. So i know the entities exist and are instantiated correctly
  2. recursive


    Jul 12, 2012
    Unfortunately, you cannot pass reference types to a job at this time (this also includes structs that contain reference types, such as the MeshInstanceRenderer).

    What you might do is have some kind of mapping, a proxy component for MeshInstanceRenderer that another, regular ComponentSystem swaps out for the appropriate MeshInstanceRenderer Data, (like VisualProxy : ISharedComponent, with a key integer or something that maps to the specific MeshInstanceRenderer settings).

    There will probably be a better way to handle this in the future as they add more serialization features, which I believe they'll have more information on soonish / circa Unite Berlin.

    Edit: Bonus Feature Idea

    If you have the mapping defined by Entities, IE you have Entities with both VisualProxy, and some data to assemble a MeshInstanceRenderer instance, you could basically have free variations. Consider all instance that filter to the same data as variations, so even if you have more than one, you could randomly pick and apply the mesh instance renderer data.
    avvie likes this.