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

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?

1 Like

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

const string baseItem = "Component/Scripts/"; // how the menu works
var item = "BovineLabs.Movement.Data/Agent Evade"; // namespace/class name in unity's pretty format

var method = typeof(EditorApplication).GetMethod("ExecuteMenuItemOnGameObjects", BindingFlags.NonPublic | BindingFlags.Static);
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.

From the DOTS compiler inspector it looks like it:
class

using Unity.Entities;
using UnityEngine;

[Unity.Entities.DOTSCompilerGenerated]
[DisallowMultipleComponent]
internal class SomethingAuthoring : MonoBehaviour, IConvertGameObjectToEntity
{
    public float something;

    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        Something componentData = default(Something);
        componentData.something = something;
        dstManager.AddComponentData(entity, componentData);
    }
}

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 :slight_smile:

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?

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.

1 Like

Ah thanks. I’m not sure we’re talking about the same thing though. I’m asking, when using a library where somebody wrote:

[GenerateAuthoringComponent]
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.

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

Ah, I should have been more specific, sorry. I mean downloaded packages and 3rd party assets.