Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ConvertToEntity and RectTransform

Discussion in 'Entity Component System' started by JohanF_TF, May 13, 2019.

  1. JohanF_TF

    JohanF_TF

    Joined:
    May 20, 2015
    Posts:
    75
    Just wanted to put this out there in case someone runs into the same issue I did

    We went over our uses of GameObjectEntity and converted to the conversion workflow instead, which included some of our UI elements. Everything was dandy before, but after we created the appropriate authoring scripts and tried to make use of ConvertToEntity, the Convert function on the authoring scripts were never called. The entities were created, but IConvertGameObjectToEntity.Convert was never run.

    The issue is of course that the UI elements don't have a Transform, they have a RectTransform, and the default conversion system (ConvertGameObjectToEntitySystem.cs) iterates over Transform

    Code (CSharp):
    1.     class ConvertGameObjectToEntitySystem : GameObjectConversionSystem
    2.     {
    3.         protected override void OnUpdate()
    4.         {
    5.             var convertibles = new List<IConvertGameObjectToEntity>();
    6.  
    7.             Entities.ForEach((Transform transform) =>
    8.             {
    9.                 transform.GetComponents(convertibles);
    10.  
    11.                 foreach (var c in convertibles)
    12.                 {
    13.                     var entity = GetPrimaryEntity((Component) c);
    14.                     c.Convert(entity, DstEntityManager, this);
    15.                 }
    16.             });
    17.         }
    18.     }
    The fix is fairly straight-forward, we created our own subclass of GameObjectConversionSystem in the project to handle RectTransform, and just do the same thing.

    Code (CSharp):
    1.     class ConvertUIGameObjectToEntitySystem : GameObjectConversionSystem
    2.     {
    3.         protected override void OnUpdate()
    4.         {
    5.             var convertibles = new List<IConvertGameObjectToEntity>();
    6.  
    7.             Entities.ForEach((RectTransform transform) =>
    8.             {
    9.                 transform.GetComponents(convertibles);
    10.  
    11.                 foreach (var c in convertibles)
    12.                 {
    13.                     var entity = GetPrimaryEntity((Component) c);
    14.                     c.Convert(entity, DstEntityManager, this);
    15.                 }
    16.             });
    17.         }
    18.     }

    So the main take-away is that extending and writing your own conversion systems is really simple. Or at least it feels that way, unless I'm missing something further down.

    Not rocket science, just wanted to put it out there in case someone runs into it.

    A question in relation to this: Is this a bug, or just something that hasn't been taken care of yet? Or is the intention to leave it at that and have the developers fix these holes with their own converters? I feel like the the Unity API should be covered natively. I understand that we're in early stages of course and have no real problem with the support not being in there currently, just wondering what the thoughts are regarding cases like this.