为什么未实现用于覆盖的MonoBehaviour方法? [英] Why are the MonoBehaviour methods not implemented for overriding?

查看:109
本文介绍了为什么未实现用于覆盖的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,因此人们可以轻松地override;实际上,它们根本没有实现.当时写的方法是该类事先没有的新方法.

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 a messages.

现在,MonoBehaviour继承自行为,该继承自

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.

像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.

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

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