MonoBehaviour 类是一个基类,所有 Unity 脚本都默认派生自该类。当您从 Unity 的项目窗口创建一个 C# 脚本时,它会自动继承 MonoBehaviour,并为您提供模板脚本。请参阅创建和使用脚本以了解更多相关信息。
MonoBehaviour 类提供了框架,允许您将脚本附加到编辑器中的游戏对象,并提供诸如 Start 和 Update 等常用事件的挂钩。
For a complete reference of every member of the MonoBehaviour class, and its technical details, see the MonoBehaviour script reference.
MonoBehaviour 类允许您启动、停止和管理协程,这是一种编写异步代码的方法,其中包括等待一定时间或某些操作完成,同时允许其他代码继续执行。
有关协程的更多信息,请参阅协程手册页和 StartCoroutine 方法脚本参考。
MonoBehaviour 类提供对大量事件消息的访问,允许您根据项目中当前发生的情况执行代码。下面是一些比较常见的例子。有关完整的列表,请参阅 MonoBehaviour 脚本参考页面 上的消息部分
Start
- 在游戏对象开始存在时(加载场景或实例化游戏对象时)调用。
Update
- 每帧都会被调用。
FixedUpdate
- 每个物理时间步进调用。
OnBecameVisible
和 OnBecameInvisible
- 当游戏对象的渲染器进入或离开摄像机的视图时调用。
OnCollisionEnter
和 OnTriggerEnter
- 在发生物理碰撞或触发时调用。
OnDestroy
- 在销毁游戏对象时调用。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.