组件、行为和 MonoBehaviour 之间有什么区别?为什么这些是分开的? [英] What is the difference between Component, Behaviour and MonoBehaviour? And why these are separated?

查看:34
本文介绍了组件、行为和 MonoBehaviour 之间有什么区别?为什么这些是分开的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

组件:

ComponentBehaviourMonoBehaviour 的基类.它是可以附加到游戏对象的任何脚本的基类.

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 内置 脚本的派生来源.这包括 ColliderRigidbody.

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 最需要注意的一点是,当你必须使用协程、调用或任何 Unity 回调函数(如物理 OnCollisionEnter 函数)时,你需要它,StartOnEnableOnDisable

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.

您想要协程、InvokeInvokeRepeating 并且能够启用/禁用您的脚本?使用继承自 MonoBehaviour.

You want coroutine, Invoke, InvokeRepeating and be able to enable/disable your script? Use inherits from MonoBehaviour.

您希望能够启用或禁用您的脚本但不需要协程、InvokeInvokeRepeating?使用 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.

请注意,BehaviourComponent 被 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 from Behaviour which then inherits from Component.

Collider 直接继承自 Component.然后非基础对撞机例如 BoxColliderSphereCollider 继承自 Collider.

Collider inherits from Component directly. Then non base colliders such as BoxCollider and SphereCollider inherits from that Collider.

这篇关于组件、行为和 MonoBehaviour 之间有什么区别?为什么这些是分开的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆