Unity 私有唤醒更新和启动方法如何工作? [英] How do the Unity private awake update and start methods work?

查看:37
本文介绍了Unity 私有唤醒更新和启动方法如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Unity 如何在后台调用 Awake、Update 和 Start 方法?它们对我没有访问修饰符表明它们是私有方法,并且它们不使用诸如 new 或 override 之类的任何东西,那么 Unity 框架如何找到调用它们的方法?

How does Unity call the Awake, Update, and Start methods behind the scene? That they have no access modifier to me indicates they are private methods, and they don't use anything like new or override, so how does the Unity framework find the methods to call them?

在一个相关的问题上,是否有任何特殊原因没有使用虚拟方法?

On a related question, is there any particular reason virtual methods were not used?

对于那些不熟悉 Unity 脚本的人,它们通常是这样显示的:

For those unfamiliar with Unity scripts, this is how they generally appear:

public class MyClass : MonoBehaviour{
   void Start(){
   }

   void Awake(){
   }

   void Update(){
   }
}

我不明白的是框架如何为每个脚本查找并自动调用这些方法,而从表面上看,它们看起来只是私有方法

What I don't understand is how the framework finds and automatically calls those methods for each script when, by all appearances, they look to just be private methods

推荐答案

这里是 Unity 博客文章

如何调用更新

不,Unity 不会在每次需要调用时使用 System.Reflection 来查找魔术方法.

No, Unity doesn’t use System.Reflection to find a magic method every time it needs to call one.

相反,第一次访问给定类型的 MonoBehaviour 时,通过脚本运行时(Mono 或 IL2CPP)检查底层脚本是否定义了任何魔术方法并缓存了此信息.如果 MonoBehaviour 具有特定方法,则将其添加到适当的列表中,例如,如果脚本定义了 Update 方法,则将其添加到需要每帧更新的脚本列表中.

Instead, the first time a MonoBehaviour of a given type is accessed the underlying script is inspected through scripting runtime (either Mono or IL2CPP) whether it has any magic methods defined and this information is cached. If a MonoBehaviour has a specific method it is added to a proper list, for example if a script has Update method defined it is added to a list of scripts which need to be updated every frame.

在游戏过程中,Unity 只是遍历这些列表并从中执行方法——就这么简单.此外,这就是为什么您的 Update 方法是公共的还是私有的并不重要.

During the game Unity just iterates through these lists and executes methods from it — that simple. Also, this is why it doesn’t matter if your Update method is public or private.

这篇关于Unity 私有唤醒更新和启动方法如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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