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?
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
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?
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