进入播放模式并禁用域重新加载
了解ON进入运行播放模式时禁用域重新加载如何影响应用程序程序状态,以及如何在代码中弥补这些效果。
阅读时间11 分钟最后更新于 5 天前
脚本域也称为应用程序域,简称域,是 Unity Managed 脚本 Environment 的核心功能。域是专用于特定应用程序的孤立内存部分,其中包含应用程序所需的编译类型,这些类型分组为称为程序集的逻辑单位。它还包含表示当前应用程序状态的数据,例如变量的值和托管内存各个部分中的Object引用。
默认情况下,Unity 会ON进入播放模式时重新加载域以Reset应用程序程序状态。在进入播放模式之前重置状态通常是可取的,因此应用程序程序会像在版本开始时一样启动。例如,在上一个播放模式会话中递增的Static计数器应在Next一个会话中再次从零开始。但是,当经常在编辑和播放模式之间切换时,域重新加载也是一种耗时的操作,会对迭代时间产生负面影响。为了加快迭代时间,您可以 ON 进入播放模式禁用域重新加载,但随后必须在代码中手动重置状态。
进入播放模式When用域重新加载的特效
禁用域重新加载When:
- 非序列化字段在返回到编辑模式时保持播放模式期间分配给它们的值。这适用于所有脚本类型的字段,包括 MonoBehaviours(包括 ON 预制件 Assets)、ScriptableObjects 和您自己的自定义 C# 类型。有关在不同上下文中什么是 ON 和什么是 UN 序列化的 Detailed 信息,请参阅 Serialization Rules。
- Static 变量在播放模式 Sessions 之间保持其值。
- Static 事件在播放模式 Sessions 之间保持其注册用户。
- 对于标有 或
[ExecuteInEditMode]的脚本,没有其他[ExecuteAlways]或OnDisableCalls。OnEnable
为了补偿播放模式 Sessions 之间的这种持久性并以新的应用程序状态进入播放模式,必须在代码中重置状态。
ON 禁用域和重新加载 Scene 的效果的更多信息,请参阅禁用域和重新加载 Scene 的细节。
从代码重置状态
当禁用域重新加载时,Static 字段的值和分配给 Static 事件的处理程序在播放模式运行之间持续存在。以下代码示例具有一个 Static 计数器,按下 Any keyboard Key 时会递增。该代码还注册了用于手柄 Static 事件的方法。
EditorApplication.playModeStateChanged已启用域重新加载后,Unity 会在进入播放模式 ON 时重新初始化此代码,擦除先前播放模式运行中的状态,包括计数器值和事件处理程序的注册。禁用域重新加载后,计数器值和事件处理程序注册都将保留上次运行的结果。Next运行播放模式ON,计数器从上次运行结束时的值开始,并ON一次事件程序程方法,从而在控制台台中生成多个“Exiting 播放模式”消息。
// Copy-paste this code into a MonoBehaviour script attached to a GameObject in your project.// Run it with domain reload enabled and then with domain reload disabled and note the different behavior.using UnityEngine;#if UNITY_EDITORusing UnityEditor;#endifpublic class StateResetExample :MonoBehaviour{ // With domain reload disabled this counter won't reset to zero on exiting Play mode Static 整型计数器 = 0; void Start() { // Register handler#if UNITY_EDITOR EditorApplication.playModeStateChanged += OnExitPlayMode;#endif } void Update() { if (Input.anyKeyDown) { counter++; Debug.Log("Counter: " + counter); } }#if UNITY_EDITOR private static void OnExitPlayMode(PlayModeStateChange 状态) { if(state == PlayModeStateChange.ExitingPlayMode) { // With domain reload disabled this message prints multiple times after the first Play mode run Debug.Log("Exiting Play mode!"); } }#endif}
您可以使用在播放模式运行之间显式重置计数器和取消注册事件处理程序的代码来修复问题行为。您可以 ON 进入播放模式或 ON 退出播放模式。
ON 退出播放模式重置状态
ON退出播放模式时Reset状态通常最有效,而不是ON进入时重置状态。您可以使用 事件及其 enum 值捕获播放模式退出事件并在该点重置状态。在以下示例中,用于播放模式退出的事件处理程序用于自行取消注册,但此时也可以使用此方法取消注册 Any Other Static 事件处理程序:
EditorApplication.playModeStateChangedExitingPlayModeusing UnityEngine;#if UNITY_EDITORusing UnityEditor;#endifpublic class StateResetOnExit :MonoBehaviour{ Static 整型计数器 = 0; void Start() {#if UNITY_EDITOR EditorApplication.playModeStateChanged += OnExitPlayMode;#endif } // Update is called once per frame void Update() { if (Input.anyKeyDown) { counter++; Debug.Log("Counter: " + counter); } }#if UNITY_EDITOR private static void OnExitPlayMode(PlayModeStateChange 状态) { if(state == PlayModeStateChange.ExitingPlayMode) { Debug.Log("Exiting Play mode!"); Debug.Log("Unregistering handler."); // Unregister the handler so it doesn't affect the next Play mode run EditorApplication.playModeStateChanged -= OnExitPlayMode; Debug.Log("Resetting counter."); // Reset the counter so it starts from 0 on the next Play mode run counter = 0; } }#endif}
EditorApplication.playModeStateChangedUnityEditor如果代码在播放模式之外以编辑模式执行,则退出播放模式ON不能依赖ON重置状态。代码在编辑模式下可能会修改 Static 变量,因此在进入播放模式时必须重置该变量。
进入播放模式时重置 ON 状态
还可以ON进入播放模式时Reset状态。如果在捕获退出的播放模式事件时遇到 Any Platform 指定的问题,或者如果您的代码也在编辑模式下执行,这可能会是一个有用的替代方法。以下示例演示了ON进入播放模式时重置Static变量和取消注册Static事件处理程序。最佳做法是始终在退出播放模式时取消注册 Static 事件处理程序。等到Next一个播放模式会话时,可能会导致Object引用问题,从而导致取消处理程序错误的处理程序。
Unity 在 和 Namespaces 中都有自定义初始化Attributes,您可以使用它们执行初始化工作,包括用户手册状态重置。应该使用哪些Attributes取决ON代码是在编辑模式还是播放模式。要重置 Player(运行时)脚本进入播放模式的 ON 状态,可将 属性与 参数结合使用:
UnityEngineUnityEditor[RuntimeInitializeOnLoadMethod]RuntimeInitializeLoadType.SubsystemRegistrationusing UnityEngine;#if UNITY_EDITORusing UnityEditor;#endifpublic class StateResetOnEnter :MonoBehaviour{ Static 整型计数器 = 0; [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] Static void Init() { Debug.Log("Unregistering handler."); // Unregister the handler so it doesn't affect the next Play mode run EditorApplication.playModeStateChanged -= OnExitPlayMode; Debug.Log("Resetting counter."); // Reset the counter so it starts from 0 on the next Play mode run counter = 0; } void Start() {#if UNITY_EDITOR EditorApplication.playModeStateChanged += OnExitPlayMode;#endif } // Update is called once per frame void Update() { if (Input.anyKeyDown) { counter++; Debug.Log("Counter: " + counter); } }#if UNITY_EDITOR private static void OnExitPlayMode(PlayModeStateChange 状态) { if(state == PlayModeStateChange.ExitingPlayMode) { Debug.Log("Exiting Play mode!"); } }#endif}
[RuntimeInitializeOnLoadMethod]UnityEngine- 使用 为
[InitializeOnLoadAttribute]注册处理程序,然后在进入或退出播放模式时 ON 执行状态清理。请参阅EditorApplication.playModeStateChangedAPI 参考以查看示例。EditorApplication.playModeStateChanged - 使用 属性可以专门 ON 进入播放模式执行清理。
[InitializeOnEnterPlayMode]