Search Unity

What's the reason [GenerateAuthoringComponent] generates an internal instead of a public class?

Discussion in 'Entity Component System' started by florianhanke, Aug 11, 2020.

  1. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    I often completely regenerate my prefabs from code, via an AssetPostprocessor, from blender and metadata files. In this process, I add GO components to the prefabs, usually via
    go.AddComponent<SomeComponentAuthoring>();
    .
    When
    [GenerateAuthoringComponent]
    is used, this is not possible, especially when it is used in 3rd party code.

    What is the reason
    [GenerateAuthoringComponent]
    generates an internal class?
     
    JesOb likes this.
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Does it even generate a class anymore?

    Anyway you can kind of hack this in using the existing menu implementation with a little reflection. For example

    Code (CSharp):
    1. const string baseItem = "Component/Scripts/"; // how the menu works
    2. var item = "BovineLabs.Movement.Data/Agent Evade"; // namespace/class name in unity's pretty format
    3.  
    4. var method = typeof(EditorApplication).GetMethod("ExecuteMenuItemOnGameObjects", BindingFlags.NonPublic | BindingFlags.Static);
    5. method.Invoke(null, new object[] { $"{baseItem}{item}", Selection.gameObjects });
    Someone might have a cleaner path of implementing this. I haven't really played around with this except to confirm it works.
     
  3. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    From the DOTS compiler inspector it looks like it:
    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3.  
    4. [Unity.Entities.DOTSCompilerGenerated]
    5. [DisallowMultipleComponent]
    6. internal class SomethingAuthoring : MonoBehaviour, IConvertGameObjectToEntity
    7. {
    8.     public float something;
    9.  
    10.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    11.     {
    12.         Something componentData = default(Something);
    13.         componentData.something = something;
    14.         dstManager.AddComponentData(entity, componentData);
    15.     }
    16. }
    17.  

    I'm a bit confused by "existing menu implementation" – are you simply informing me I can use reflection to make this call
    go.AddComponent<SomeComponentAuthoring>();
    even though
    SomeComponentAuthoring
    is internal? If so: thank you :)

    I'm still wondering if there is a good reason why the generated
    SomethingAuthoring
    is not public, so it can be used in code. I can think of naming collisions, but since I probably want the IComponent
    Something
    to have an Authoring component, it shouldn't be an issue if the resulting
    SomethingAuthoring
    is public…???

    Perhaps there was once a good reason, but it could be public now?
     
    Last edited: Aug 12, 2020
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Interesting. I haven't been able to find them in the project, but i haven't looked too deep.

    As for the menu stuff. That's the internal method the add component menu calls to add them in inspector.
     
    florianhanke likes this.
  5. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    Ah thanks. I'm not sure we're talking about the same thing though. I'm asking, when using a library where somebody wrote:
    Code (CSharp):
    1. [GenerateAuthoringComponent]
    2. public struct Something : IComponentData { }
    why the Unity team chose to generate
    SomethingAuthoring
    code that is internal. Why not make it public so I can reference and use
    SomethingAuthoring
    easily in my code? Seems overly defensive.
     
  6. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    even if it were public, you can't reference the generated dll from your code because the generated code needs to reference your code dll and you can't have cyclic references
     
  7. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    Ah, I should have been more specific, sorry. I mean downloaded packages and 3rd party assets.
     
    Last edited: Aug 12, 2020