Version: 2021.1

ExecuteAlways

class in UnityEngine

切换到手册

描述

使脚本的实例在播放模式期间以及编辑时始终执行。

默认情况下,MonoBehaviours 仅在播放模式下并且仅当它们位于包含用户场景的主阶段中的游戏对象上时才执行。它们不会在编辑模式下执行,也不在预制件模式下编辑的对象上执行,即使此时在播放模式下也是如此。通过添加此属性,MonoBehaviour 的任何实例都将始终执行其回调函数。

当您希望脚本作为编辑器工具的一部分执行某些操作时,即可使用 [ExecuteAlways] 属性,不一定要与构建播放器和播放模式中发生的播放逻辑相关。有时,此类脚本的“播放”功能与其“编辑模式”功能相同,而其他情况下则相差很大。

使用此属性的 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.IsPlayingrunInEditModeEditorApplication.QueuePlayerLoopUpdate

using UnityEngine;

[ExecuteAlways] public class ExampleClass : MonoBehaviour { void Start() { if (Application.IsPlaying(gameObject)) { // Play logic } else { // Editor logic } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961