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
The snippet below should hide your script from the Editor (Inspector and Hierarchy).
As long as your script inherits from
MonoBehaviour
, you can't prevent people attaching it to a GameObject.Although, you can make it so that when they attach it their GameObject, you throw an error and then destroy that component. Have a variable you set to true. You can can a function from your plugin to set this variable to true from your plugin so that your script will not destroy.
Example code:
When people call it like this:
They will get the error below and the script will destroy itself:
Now, if you call create it from your plugin and call the
keepAlive
function, the script should stay:Note:
This is not an Editor script and it made to work when you click the "Play" button but you can use
[ExecuteInEditMode]
to make it execute in the Editor.A very simple solution is to make your class an inner class of some owner:
You can add the class easily enough:
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.
Since you are adding it with AddComponent, maybe you want to remove the MonoBehaviour part of your component.
Consider you have that main component that creates the sub component in Start:
turn that into:
The problem is that you most likely wanted to use all the Unity callbacks. Add them to the TopComponent and call the SubComponent explicitly.
For my case, I use Monobehaviour.Reset() method to prevent addcomponent in Editor.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Reset.html