Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent component from being added in the Editor

Tags:

c#

mono

unity3d

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)

Precisions

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

like image 773
MadStark Avatar asked Dec 18 '22 04:12

MadStark


1 Answers

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.

like image 55
Ed Marty Avatar answered Dec 30 '22 14:12

Ed Marty