摘自:https://forum.unity.com/threads/how-to-use-addressable-system-in-editor-script.715163/#post-7906237
#if UNITY_EDITOR using UnityEngine; using UnityEditor; using System.Threading.Tasks; /// <summary> /// This script is used to automatically refresh the time in edit mode to support some time-dependent logic /// </summary> [InitializeOnLoad] public static class TimeInEditMode { static int cnt_running = -1; static TimeInEditMode() { EditorApplication.update += Update; EditorApplication.playModeStateChanged += PlayModeStateChanged; } static void Update() { if (cnt_running > 0) { EditorApplication.QueuePlayerLoopUpdate(); } } static void PlayModeStateChanged(PlayModeStateChange state) { switch (state) { case PlayModeStateChange.ExitingEditMode: Clear(); break; } } [MenuItem("Tools/TimeInEditMode/Start", false, 100)] /// <summary> /// Start it where you need it, or keep it on all the time /// <summary> public static bool Start() { if (Application.isPlaying) { Debug.Log("cannot call TimeInEditMode/Start in play mode"); return false; } if (cnt_running < 0) cnt_running = 1; else ++cnt_running; return true; } [MenuItem("Tools/TimeInEditMode/Stop", false, 101)] public static void Stop() { if (cnt_running < 0) return; --cnt_running; } [MenuItem("Tools/TimeInEditMode/Clear", false, 102)] public static void Clear() { cnt_running = -1; } /// <summary> /// You can use it with tasks /// <summary> public async static void StartWithTask(Task task) { if (Start()) { await task; Stop(); } } } #endif