I am looking of a way to disable the possibility of attaching my MonoBehaviour component in the Unity Editor. So I don't want my component to appear anywhere visible by the user.
The reason is that I attach this component manually from a script with AddComponent<T>()
. So this is more a convenience question, I just don't want the user to think he has to add my component manually in order for my plugin to work. (if he does anyway, that doesn't cause any trouble. Again, this is just convenience)
I am writing a library (dll plugin), so I cannot use the trick of naming the file differently from my component, but that is exactly the effect I'm looking for.
I also tried to simply put my class as internal because that's really what it is, the assembly should be the only one to have access to this component. However, I'm afraid inheriting from MonoBehaviour even with an internal class makes the component available in the editor...
The HideInInspector
attribute doesn't seem to work on a class.
I am calling AddComponent
from a static method with RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)
so I won't have any other behaviour to rely on.
Quite a challenge, if we find an answer, that would be luxury. Any idea? Thank you very much
A very simple solution is to make your class an inner class of some owner:
class NotAMonoBehaviour {
class MyMonoBehaviour : MonoBehaviour {
//etc.
}
}
You can add the class easily enough:
gameObject.AddComponent<NotAMonoBehaviour.MyMonoBehaviour>()
And it will not show up in the inspector's Add Component list. Setting using the hide flags as suggested will also stop it from showing up in the inspector itself, although that can't be relied upon entirely, as you can open the Debug view in the inspector and still see hidden components.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With