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 Prevent unity transforms from being added to Entity prefabs?

Discussion in 'Entity Component System' started by Ryetoast, Sep 11, 2022.

  1. Ryetoast

    Ryetoast

    Joined:
    Mar 8, 2015
    Posts:
    48
    I have some prefabs for entities that don't need transforms on them at all. I don't see a way to prevent a prefab set up in the editor from including transforms, and so my prefabs all end up with Translation/Rotation/Scale/LocalToWorld. This results in the TRS to Local To World system doing a lot of work every frame that is completely unnecessary (I'm instantiating a lot of some of these positionless entities).

    Is there something I'm missing, or do I need to do something really weird like manually removing the transform components from the prefab before instantiating copies of it?
     
  2. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    You can create an authoring component and attach it to the prefabs you don't want transform components on.
    Code (CSharp):
    1. public class RemoveTransformAuthoring : MonoBehaviour, IConvertGameObjectToEntity
    2. {
    3.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    4.     {
    5.         dstManager.RemoveComponent<Translation>(entity);
    6.         dstManager.RemoveComponent<Rotation>(entity);
    7.         dstManager.RemoveComponent<NonUniformScale>(entity);
    8.         dstManager.RemoveComponent<LocalToWorld>(entity);
    9.     }
    10. }
     
  3. DreamersINC

    DreamersINC

    Joined:
    Mar 4, 2015
    Posts:
    130
    Correct me if I am wrong, but I believe Local to World is required if you are using the Hybrid Render system. You would need to write you own Entity Data authoring component and don't add the component object link GO transform to the data entity.
     
  4. Ryetoast

    Ryetoast

    Joined:
    Mar 8, 2015
    Posts:
    48
    I ended up writing an initialization system that removes Translation, Rotation, RotationEulerXYZ, Scale, and NonUniformScale from both active and prefab entities that don't have SpriteRenderer. Since it's stripping off the prefabs as well, basically doesn't need to run more than once so it's not thrashing entities across chunks constantly.

    Works well for my use case, since I'm doing a 2D game and have my own simpler transform components and in a lot of cases I'm doing my own rendering for better optimized batching.

    Would be nice if you could opt out of transforms as part of the default workflow in the future though.