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

Spawn Entities from Prefab with different scale and color

Discussion in 'Entity Component System' started by angel1st, Jan 14, 2020.

  1. angel1st

    angel1st

    Joined:
    Nov 16, 2019
    Posts:
    35
    Hi there,
    I quite new to ECS / DOTS, so first of all, apologies if the question I am about to ask is already answered.
    My use case - I would like to create I bunch of entities from the existing prefab and for each entity, I would like to set:
    - Position
    - Scale
    - Color
    Now, I already saw how I can set the position:
    Code (CSharp):
    1. manager.SetComponentData(rotatingCubeEntity, new Translation { Value = new float3(posX, 0.0f, posZ) });
    However, I have issue to find how to set scale and color.
    For scale, I can see, I can create an entity archetype and to include Scale, however, looking at Unity.Transforms.Scale struct, I can see just a single Value, so it is not clear to me how I can scale in 3-dimensional world.
    Regarding the color - the way I found, the color is set is basically like that:
    Code (CSharp):
    1. RenderMesh newrend =  manager.GetSharedComponentData<RenderMesh>(rotatingCubeEntity);
    2. newrend.material.color = gradient.Evaluate(UnityEngine.Random.Range(0f, 1f));
    3. manager.SetSharedComponentData(rotatingCubeEntity, newrend);
    however, with this approach, since the renderMesh is shared among all entities, I set the new color to all entities. So basically I have all entities in the same color.
    I would appreciate either a scripting example or reference to the existing one.
    Thanks
     
    Last edited: Jan 15, 2020
  2. digitaliliad

    digitaliliad

    Joined:
    Jul 1, 2018
    Posts:
    64
    Last edited: Jan 14, 2020
  3. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Regarding the scale you should read this, the short version is that you probably want the NonUniformScale component. Per-instance color is more complicated. The hybrid renderer only supports per-instance data in HDRP right now and it's most definitely not beginner friendly to get it working. See this thread for more information.
     
  4. angel1st

    angel1st

    Joined:
    Nov 16, 2019
    Posts:
    35
    Thanks, @digitaliliad and @Sarkahn for your answers. I found the latter thread very informative since I am also using LWRP / URP in my project - something I forgot to mention. So basically, ECS in terms of dynamic rendering for URP is way too far from being implemented. Am I correct?
     
  5. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Seems that way yes. Last I heard is we can expect to hear some news on that front sometime in the next few months, but no more details than that.
     
    angel1st likes this.