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

Question How to do "Hybrid Components" (Companion GameObject) in 0.50?

Discussion in 'Entity Component System' started by Krooq, Jun 1, 2022.

  1. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    180
    In 0.50 Hybrid Components were "removed" from the DOTS workflows and documentation.
    I'm wondering, how are we meant to fill the gaps now?

    One example I'm struggling with is a simple name tag, i.e. a piece of text in world space.
    TextMeshPro on a GameObject seems like the correct choice, but how do I author this?
    I don't think I can use ConvertAndInjectGameObject because the component lives on a prefab to be spawned into a netcode world.
    I could spawn in the name tag as GameObject prefab and hook things up manually but then I don't get the transform handling of CompanionLink.

    Anyone able to shed light on the correct way to handle this?
     
  2. mattdymott

    mattdymott

    Joined:
    Dec 27, 2016
    Posts:
    29
    I used this to convert TextMeshPro to companion objects:

    Code (CSharp):
    1.  
    2. [UpdateInGroup(typeof(GameObjectAfterConversionGroup))]
    3. public class TextMeshProConversion : GameObjectConversionSystem
    4. {
    5.     protected override void OnUpdate()
    6.     {
    7.         Entities.ForEach((TextMeshPro textMeshPro, MeshRenderer meshRenderer, RectTransform rectTransform) =>
    8.         {
    9.             var entity = GetPrimaryEntity(textMeshPro);
    10.                 DstEntityManager.AddComponentObject(entity, textMeshPro);
    11.                 DstEntityManager.AddComponentObject(entity, meshRenderer);
    12.                 DstEntityManager.AddComponentObject(entity, rectTransform);
    13.         });
    14.     }
    15. }
    Then I would have a prefab with the TextMeshPro component attached and would have to spawn it with either EntityManager.Instantiate or CommandBuffer.Instantiate.
    For some reason it wouldn't work if the prefab was placed into the scene and ConvertToEntity was ticked or placed into a SubScene. It would only work if I spawned it. Not too sure why.

    To convert the prefab into an Entity prefab I would use this:
    Code (CSharp):
    1.  
    2. public class DamageTextPrefabAuthoring : MonoBehaviour, IConvertGameObjectToEntity, IDeclareReferencedPrefabs
    3.     {
    4.         public GameObject DamageTextPrefab;
    5.        
    6.         public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    7.         {
    8.             dstManager.AddComponentData(entity, new DamageTextPrefab
    9.             {
    10.                 Value = conversionSystem.GetPrimaryEntity(DamageTextPrefab)
    11.             });
    12.         }
    13.  
    14.         public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
    15.         {
    16.             referencedPrefabs.Add(DamageTextPrefab);
    17.         }
    18.     }
    19.  
     
    Last edited: Jun 1, 2022
    tmonestudio, bb8_1 and Krooq like this.
  3. eggsamurai

    eggsamurai

    Joined:
    Oct 10, 2015
    Posts:
    99
    bump,project broken by this... cant inject any more
    I can Object.Instantiate and Inject UnityObject in .17, but everything messed up in .50
     
  4. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    262
    AddComponentObject works for me in 0.50 and 0.51. To get audio and some other stuff to work in Editor I needed to change CompanionLink.cs as in this thread : https://forum.unity.com/threads/hybrid-rigidbodies-in-editor-0-50.1272119/#post-8073257

    But from your description I don't know if this is your problem.
     
  5. eggsamurai

    eggsamurai

    Joined:
    Oct 10, 2015
    Posts:
    99
    yes.. I replace ConverToEntity.cs with manually getComonent<IConvertGameObjectToEntity> and run Convert by my own code, a fast fix.
     
    tmonestudio likes this.
  6. OffgridChris

    OffgridChris

    Joined:
    May 20, 2021
    Posts:
    17
    I managed to follow this example on v0.51 but since upgrading to v1.0.0-pre.15, this no longer works. MeshRendererBakingUtility fails with the following error:

    Code (CSharp):
    1. Renderer is not converted because either the assigned mesh is null or no materials are assigned on GameObject
    Has anyone else had any success in v1?