使脚本的实例在播放模式期间以及编辑时始终执行。
默认情况下,MonoBehaviours 仅在播放模式下并且仅当它们位于包含用户场景的主阶段中的游戏对象上时才执行。它们不会在编辑模式下执行,也不在预制件模式下编辑的对象上执行,即使此时在播放模式下也是如此。通过添加此属性,MonoBehaviour 的任何实例都将始终执行其回调函数。
You can use the [ExecuteAlways] attribute when you want your script to perform certain things as part of Editor tooling, which is not necessarily related to the Play logic that happens in built Players and in Play Mode. Sometimes the Play functionality of such a script is identical to its Edit Mode functionality, while other times they differ greatly.
使用此属性的 MonoBehaviour 必须确保它们在编辑模式下或者在对象不是游戏世界的一部分时不会运行可能导致对象被错误修改的播放逻辑。为达到此目的,可以使用 Application.IsPlaying,在其中,脚本可以传入自己的游戏对象以检查它是否是游戏世界的一部分。
如果 MonoBehaviour 在播放模式下运行播放逻辑并且无法检查其游戏对象是否是游戏世界的一部分,则在预制件模式下编辑的预制件可能会被仅用作游戏世界一部分的逻辑错误地修改和保存。
如果您的脚本使用静态变量或单例模式,则应确保属于游戏世界的脚本实例和不属于游戏世界的实例不会通过这些变量或单例意外地相互影响。
在不属于游戏世界的对象上,不会像其他情况那样不断地调用以下函数。
Update is only called when something in the Scene changed.
OnGUI is called when the Game view receives a non-editor-only Event that it does not use (e.g., EventType.ScrollWheel) and does not forward to the Editor's keyboard shortcut system (e.g., EventType.KeyDown, EventType.KeyUp). Events forwarded to the Game view are enqueued and are not guaranteed to be processed immediately.
OnRenderObject and the other rendering callback functions are called on every repaint of the Scene view or Game view.
另请参阅:Application.IsPlaying、runInEditMode、EditorApplication.QueuePlayerLoopUpdate。
using UnityEngine;
[ExecuteAlways] public class ExampleClass : MonoBehaviour { void Start() { if (Application.IsPlaying(gameObject)) { // Play logic } else { // Editor logic } } }
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.