【发布时间】:2019-10-13 08:56:31
【问题描述】:
这个页面的“消息”是什么? (开始、更新、唤醒...等)
是类似于虚拟方法或事件的东西吗?
或者“消息”是 C# 语法之一?
https://docs.unity3d.com/ScriptReference/MonoBehaviour.html
【问题讨论】:
标签: c# unity3d game-engine
这个页面的“消息”是什么? (开始、更新、唤醒...等)
是类似于虚拟方法或事件的东西吗?
或者“消息”是 C# 语法之一?
https://docs.unity3d.com/ScriptReference/MonoBehaviour.html
【问题讨论】:
标签: c# unity3d game-engine
Unity 引擎基本上会在 MonoBehaviours 上调用这些方法(如果已定义),具体取决于引擎事件。
例如:
Awake 在加载脚本实例时调用。Start 在脚本启用时的第一帧、每个 Update 方法之前和 Awake 之后调用Update 在每一帧中都被调用您可以在 DOC 中看到许多消息,并且根据引擎事件调用它们。
您不能将这些事件称为引擎正在处理的事件!
最常见的是:
但请注意,在它们为空时使用这些方法(消息)会产生很小的开销,因为引擎会调用它们,即使它们是空的。
另一个高级的事情是这些消息中的一些可以是协程。您还可以为它们提供一些高级功能。
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();
}
【讨论】:
这里的“消息”是函数/方法的同义词,对于从 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都有它的用例和时间,当你开始使用它们时你就会掌握它,它们是引擎的核心。
【讨论】: