Unity如何执行其方法? [英] How does Unity execute its methods?

查看:210
本文介绍了Unity如何执行其方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Unity3D仅有很少的经验,但是我注意到从MonoBehaviour派生的类可能包含带有预定义签名的函数,这些签名将以特殊方式调用.例如,如果我写:

I only have a small experience in Unity3D, but I noticed that classes that derive from MonoBehaviour may contain functions with predefined signatures that will be called in a special way. For instance, if I write:

void Update()
{
  //some code
}

每帧都会调用此方法.

this method will be called every frame.

我想象在Unity内部有某种无限循环,它为场景中每个对象的每一帧调用Update方法.但是如何知道该对象实际上提供了Update方法的实现?很明显,如果UpdateMonoBehaviour类中某个方法的替代,但是从语法(以及可以使用任何访问修饰符实现这种方法的事实)来看,事实并非如此.那里有反射魔法吗?

I imagine that inside Unity there is some sort of an endless loop that calls the Update method every frame for each object on the scene. But how does it know that the object actually provides the Update method implementation? It would have been clear if Update was an override for a method in the MonoBehaviour class, but judging by the syntax (and the fact that you can implement such methods with any access modifier) it's not. Is there some reflection magic happening there?

推荐答案

http://blogs.unity3d.com/2015/12/23/1k-update-calls/

不,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天全站免登陆