Search Unity

  1. Dismiss Notice
  2. We have added a sub-forum for DOTS Graphics. Please use the new space for related discussions.
    Dismiss Notice
  3. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice

Help Wanted How do I 'add' a monobehavior to an Entity?

Discussion in 'Data Oriented Technology Stack' started by TheGabelle, Feb 19, 2021.

  1. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    229
    I know that Monobehaviors can be associated with an Entity using the conversion tool. I am trying to do it manually without success.

    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Burst;
    3. using Unity.Collections;
    4. using Unity.Entities;
    5. using Unity.Jobs;
    6. using Unity.Mathematics;
    7. using Unity.Transforms;
    8.  
    9. public class PolygonCreateLineRendererSystem : SystemBase
    10. {
    11.  
    12.     EndSimulationEntityCommandBufferSystem m_EndSimulationEcbSystem;
    13.     protected override void OnCreate()
    14.     {
    15.         base.OnCreate();
    16.         m_EndSimulationEcbSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    17.     }
    18.  
    19.  
    20.     protected override void OnUpdate()
    21.     {
    22.         //var ecb = m_EndSimulationEcbSystem.CreateCommandBuffer();
    23.         Entities
    24.             .WithoutBurst()
    25.             .WithAll<Polygon, PolygonCreateLineRendererTag>()
    26.             .ForEach((Entity entity, DynamicBuffer<PolygonPoint> buffer) => {
    27.                 var lr = new LineRenderer();
    28.                 lr.positionCount = buffer.Length;
    29.                 for(var i = 0; i < buffer.Length; i++)
    30.                 {
    31.                     Debug.Log(buffer[i].Value);
    32.                     lr.SetPosition(i, buffer[i].Value);
    33.                 }
    34.                 //ecb.SetComponent(entity, lr);
    35.                 World.EntityManager.SetComponentData<LineRenderer>(entity, lr);
    36.                 World.EntityManager.RemoveComponent<PolygonCreateLineRendererTag>(entity);
    37.  
    38.                 Debug.Log("PolygonCreateLineRendererSystem hit!");
    39.             })
    40.             .Run();
    41.             //.Schedule();
    42.             //m_EndSimulationEcbSystem.AddJobHandleForProducer(this.Dependency);
    43.     }
    44. }
     
  2. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    273
    There is a method on EntityManager called AddObject or something like that
     
  3. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    597
    You should never do
    new SomeMonoBehabiour()
    , this is not how they work.

    You have two options here:

    A. Do that at conversion time using AddHybridComponent, which has its own limitations
    B. Create an empty GameObject to be the container, doing an AddComponent<LineRenderer>() on it, then do EntityManager.AddComponentObject to link the entity to it. Just be aware that destroying the GameObject will destroy the LineRenderer, but destroying the Entity will not destroy the LineRenderer or the GameObject automatically for you.

    Sample codes:
    Code (CSharp):
    1. // option A
    2. public class HybridLineRendererAuthoring : MonoBehaviour, IConvertGameObjectToEntity
    3. {
    4.     public LineRenderer Value;
    5.  
    6.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    7.     {
    8.         conversionSystem.AddHybridComponent(Value);
    9.     }
    10. }
    11.  
    12. // option B
    13. LineRenderer value = new GameObject($"Entity {entity} Container").AddCompoment<LineRenderer>();
    14. EntityManager.AddComponentObject(entity, value);
     
    filod likes this.
unityunity