本文介绍了防止在编辑器中添加组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来禁用在Unity Editor中附加我的MonoBehaviour组件的可能性.因此,我不希望我的组件出现在用户可见的任何地方.

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.

原因是我使用AddComponent<T>()从脚本手动附加了此组件.因此,这是一个更方便的问题,我只是不想让用户认为他必须手动添加我的组件才能使我的插件正常工作. (即使他这样做,也不会造成任何麻烦.再次,这只是方便)

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)

我正在编写一个库(dll插件),所以我不能使用与组件不同的方式来命名文件,但这恰恰是我想要的效果.

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.

我也试图将我的类简单地放在内部,因为这确实是事实,程序集应该是唯一可以访问此组件的类.但是,即使使用内部类,我也害怕从MonoBehaviour继承使该组件在编辑器中可用...

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

HideInInspector属性似乎不适用于类.

The HideInInspector attribute doesn't seem to work on a class.

我正在使用 RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad) AddComponent >,所以我将没有其他行为可以依靠.

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.
  }
}

您可以轻松添加类:

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.

这篇关于防止在编辑器中添加组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!