为什么 MonoBehaviour 方法没有实现覆盖? [英] Why are the MonoBehaviour methods not implemented for overriding?

查看:28
本文介绍了为什么 MonoBehaviour 方法没有实现覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Unity3d 中,您有 MonoBehaviour 类,它是所有脚本的普通基类.在实现脚本时,必须实现诸如 Awake()Start()Update() 等方法.

In Unity3d you have the MonoBehaviour class, which is the normal base class for all scripts. When implementing a script, one has to implement the methods such as Awake() or Start() or Update().

然而,这些方法在 MonoBehaviour 类中并未作为 virtualabstract 实现,因此人们可以轻松地覆盖代码>他们;事实上,它们根本没有实施.目前编写的方法是该类之前没有的新方法.

However, These methods are not implemented as virtual or abstract in the MonoBehaviour class so that one could easily override them; in fact they are not implemented at all. The method one writes at the Moment is a new method that the class did not have beforehand.

为什么这些方法没有在基类 (MonoBehaviour) 中实现为 virtualabstract ?

Why are these methods not implemented as virtual or abstract in the base class (MonoBehaviour)?

推荐答案

如果您查看文档,您将看到所有这些功能"都列在消息"部分;MonoBehaviour 文档.

If you check the documentation you will see that all those 'functions' are listed in the 'Messages' section; MonoBehaviour Doc.

这意味着函数"(如 Start、OnEnable 或 Awake)不是作为方法实现的,而是作为消息实现的.

So that means that 'functions' (like Start, OnEnable or Awake), they are not implemented as methods but as messages.

现在,MonoBehaviour 继承自 Behaviour,后者继承自 Component 具有 SendMessage 方法.该方法基本上调用消息/方法,并选择在消息/方法存在与否时报告错误.名称消息/方法的参数是一个字符串,/~~所以他们在那里使用反射.~~/检查更新!!

Now, MonoBehaviour inherits from Behaviour, which inherits from Component which has the SendMessage method. That method basically calls a message/method with the option to report an error if the message/method exists or not. The parameter of the name message/method is a string, /~~so they are using reflection there.~~/ Check Update!!

似乎 Unity 在幕后确定是否实现了这些消息,以查看是否必须调用它们.根据这个 UnityAnswer:

Seems like Unity behind the scenes determines if those messages were implemented to see if it has to call them or not. According to this UnityAnswer:

Unity 主要是用 C++ 编写的,因此大多数魔法发生在本机代码.Unity 使用反射来确定,在您的脚本之后已经编译,那些事件"是什么?你已经实施和记住这一点.Unity 只调用 Update/LateUpdate/OnGUI 实施后.

Unity is mainly written in c++ and therefore most magic happens in native code. Unity uses reflection to determine, after your scripts have been compiled, what of those "events" you've implemented and remember that for this class. Unity only calls Update / LateUpdate / OnGUI when it has been implemented.

所以,简短的回答是,它们不是您必须重写的真正"方法,它们是只有在实现时才会调用的消息.

So, the short answer is, they are not 'real' methods that you have to override, they are messages that are called only if they were implemented.

更新;正如@Raining 所指出的,我在 Unity 获取要调用的消息的方式上是错误的.Unity 不使用反射来做到这一点.根据这个1k-update-calls(也由@Rainin) 脚本被检查以检查它们是否包含一些魔法方法".如果是,则将它们添加到将相应执行的列表中.

Update; As @Raining noted, I was wrong in the way that Unity obtains the messages to call. Unity doesn't use reflection to do this. According this 1k-update-calls (also provided by @Rainin) scripts are inspected to check if they contain some of those 'magic methods'. If so, they are added to lists that will be executed accordingly.

这篇关于为什么 MonoBehaviour 方法没有实现覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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