Unity 的 MonoBehaviour class 中的 "Messages" 是什么? (开始、更新、唤醒...等)

What is "Messages" in Unity's MonoBehaviour class? (Start,Update,Awake...etc)

这个页面的"Messages"是什么? (启动、更新、唤醒...等)
是虚方法还是事件之类的东西?
或者 "Messages" 是 C# 语法之一?
https://docs.unity3d.com/ScriptReference/MonoBehaviour.html

如果定义了 MonoBehaviours,Unity 引擎基本上会调用这些方法,具体取决于引擎事件。

例如:

  • Awake 在加载脚本实例时调用。
  • Start 在启用脚本时的第一帧调用,在每个 Update 方法之前和 Awake
  • 之后
  • Update 每一帧都被调用

您可以在 DOC 中看到许多消息,并且根据引擎事件调用它们。

您不能调用引擎正在处理的这些事件!

最常见的是:

  • 更新
  • 开始
  • 清醒
  • OnDestroy
  • 启用

但请注意,在这些方法(消息)为空时使用这些方法(消息)的开销很小,因为即使它们为空,引擎也会调用它们。

另一个先进的事情是,其中一些消息可以是协程。你可以给他们一些高级功能。

IEnumerator Start()
{
    Debug.Log("First frame i'm being enabled! yeee");
    // After 2 seconds i'm gonna blink
    yield return new WaitForSeconds(2.0f);
    Debug.Log("I'm going to blink");
    Blink();
}

'Message' 是 Function/Method 的同义词,它们只是由 unity 引擎自动调用的函数,适用于从 MonoBehaviour 继承并附加到场景中的活动游戏对象的任何脚本.

考虑一个动物脚本

public class Animal : MonoBehaviour
{
    void Awake()
    {
        Debug.Log("Code here in awake is executed by unity the first time this object is activated, and never again in the lifetime of this object.");
    }

    void Start()
    {
        Debug.Log("Start is similar to awake but is executed after 'Awake' is executed on all game objects.");
    }

    void OnEnable()
    {
        Debug.Log("Code executed EVERYTIME your object is activated, including the first time you enter playmode, provided this object is active.");
    }

    void OnDisable()
    {
        Debug.Log("Code executed EVERYTIME your object is deactivated, does not include the first time you enter playmode if the object was disabled before playing.");
    }

}

等等,每一个Message/Function/Method都有它的用例和时间,当你开始使用它们的时候你就会掌握它的窍门,它们是引擎的核心。