Search Unity

Creating one IConvertGameObjectToEntity Monobehaviour for every component.

Discussion in 'Entity Component System' started by zanius, Mar 6, 2019.

  1. zanius

    zanius

    Joined:
    Feb 5, 2018
    Posts:
    24
    Currently every component I have has an equivalent proxy so I can add them to game object prefabs. My understanding is that now I should instead have an IConvertGameObjectToEntity Monobehaviour for every component (that I want accessible in the editor at least). I wanted to make sure this was the intended workflow before going through and creating all these scripts.
     
    learc83 likes this.
  2. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    It is the preferred workflow. See here: https://forum.unity.com/threads/new-subscene-converttoentity-workflows.638785/#post-4285792

    I'd recommend testing out the IConvertGameObjectToEntity workflow in an empty project and roughly understand how it works under the hood first before committing in a real project. Depending on your old workflow, the refactoring process may or may not be straightforward. For example, in my case, my project had pooling logic on top of GameObjectEntity, so getting rid of GameObjectEntity entirely required some planning. All in all though, the IConvertGameObjectToEntity workflow feels much better to use.
     
    camillo777 likes this.
  3. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    On occasion, I'll use IConvertGameObjectToEntity, but most of the time, making a component with [GenerateAuthoringComponent] and adding the resulting authoring component to a prefab or game object that has an attached ConvertToEntity script does the trick. You'll have to create IComponents - no way around that, but making an IConvertGameObjectToEntity monobehavior for every single IComponent is unnecessary effort. Once I discovered [GenerateAuthoringComponent], life got much easier.

    I really only use IConvertGameObjectToEntity in three cases:
    1. When [GenerateAuthoringComponent] can't properly display the data in the inspector (for example: your component contains a FixedList32<float3>).

    2. When a monobehavior object needs to synchronize with an entity using AddHybridComponent(). This is mostly for things that have no ECS equivalents (particle effects or an audio source following a projectile entity for example).

    3. When your inspector user-input needs some pre-processing first (for example: your component contains an angle in radians, but you want the user input field to be in degrees).
    There are probably a few more cases for IConvertGameObjectToEntity, but I haven't run into them yet.
     
    camillo777 likes this.
  4. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,709
    This is why I love sub scenes