组件,行为和MonoBehaviour有什么区别?为什么将它们分开? [英] What is the difference between Component, Behaviour and MonoBehaviour? And why these are separated?
问题描述
MonoBehaviour扩展了Behavior,而Behavior扩展了Component. 我想知道为什么这些类是分离的,以及这些类的语义. 是否有区分这些类的目的?还有任何直接扩展Behavior或Component的类吗?
MonoBehaviour extends Behaviour and Behaviour extends Component. I want to know why these classes are separated and semantic meanings of these classes. Are there any purpose to separate these classes? And are there any classes extending Behaviour or Component directly?
我知道我们必须使用MonoBehaviour在Unity中创建C#代码. 但是,我对Unity作为游戏引擎的体系结构很感兴趣.
I know we must use MonoBehaviour to create C# codes in Unity. However, I'm interesting in architecture of Unity as a game engine.
推荐答案
组件:
Component
是Behaviour
和MonoBehaviour
的基类.它是可以附加到GameObject的任何脚本的基类.
Component
is a base class for both Behaviour
and MonoBehaviour
. It is a base class of any script that can be attached to a GameObject.
这几乎是每个Unity 内置脚本所衍生的.其中包括Collider
和Rigidbody
.
This is what almost every Unity build-in script derives from. This includes Collider
and Rigidbody
.
行为:
关于Behaviour
的最重要信息是,可以启用或禁用任何从其继承的内容.如果您不希望脚本被启用/禁用,请不要从中继承脚本.
The most important thing to know about Behaviour
is that anything that inherits from it can be enabled or disabled. Do not inherit your script from this if you don't want it to be enabled/disabled.
例如,不能启用/禁用Rigidbody
.这就是为什么它从Component
类而不是Behaviour
继承的原因.
For example, Rigidbody
cannot be enabled/disabled. This is why it inherits from the Component
class instead of Behaviour
.
Behaviour
继承自Component
.
MonoBehaviour :
关于MonoBehaviour
的最重要的注意事项是,当您必须使用协程,调用或任何Unity回调函数(例如物理OnCollisionEnter
函数,Start
,OnEnable
,
The most important thing to note about MonoBehaviour
is that you need it when you have to use coroutines, Invoking, or any Unity callback functions such as physics OnCollisionEnter
function, Start
, OnEnable
, OnDisable
, etc.
MonoBehaviour
继承自Behaviour
,因此可以启用/禁用脚本.
MonoBehaviour
inherits from Behaviour
so that your scripts can be enabled/disabled.
是否有分离这些类的目的?
Are there any purpose to separate these classes?
是的,就像上面的答案一样,它们是分开的,因此您可以通过继承适当的组件来使用不同的功能.
Yes, like the answer above, they are separated so that you can use different features by inheriting from the appropriate component.
您要使用协程,Invoke
,InvokeRepeating
并能够启用/禁用脚本吗? Use从MonoBehaviour
继承.
You want coroutine, Invoke
, InvokeRepeating
and be able to enable/disable your script? Use inherits from MonoBehaviour
.
您希望能够启用或禁用脚本,但是不需要协程,Invoke
,InvokeRepeating
?使用Behaviour
.
You want to be able to enable or disable your script but don't need coroutine, Invoke
, InvokeRepeating
? Use Behaviour
.
当您有一个永远不能启用/禁用的脚本时,这些脚本应该从Component
继承.
When you have a scripts that should never be enabled/disabled, that should inherit from Component
.
请注意,Behaviour
和Component
被Unity用作内部内容.您不应该尝试从这些脚本继承您的脚本.
Note that Behaviour
and Component
are used by Unity for internal stuff. You should not try to inherit your script from these.
将它们分开的主要原因是节省内存,并使每个组件中的代码易于维护.这比为一个人很少使用的功能编写数千行代码更好.通过分离它们并将它们加载到内存中,它们实际上已被使用,而不会浪费内存中的空间.
有没有直接扩展Behavior或Component的类?
And are there any classes extending Behaviour or Component directly?
是的
-
MonoBehaviour
直接从Behaviour
继承,然后 从Component
继承.
MonoBehaviour
inherits directly fromBehaviour
which then inherits fromComponent
.
Collider
直接从Component
继承.然后是非基础对撞机
例如BoxCollider
和SphereCollider
就是从Collider
继承的.
Collider
inherits from Component
directly. Then non base colliders
such as BoxCollider
and SphereCollider
inherits from that Collider
.
这篇关于组件,行为和MonoBehaviour有什么区别?为什么将它们分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!