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

查看:170
本文介绍了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每次需要调用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天全站免登陆