Zhonghui

每个不曾起舞的日子,都是对生命的辜负

User Tools

Site Tools


软件:unity:在editmode中保持update

在EditMode中保持Update


摘自: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
/var/www/DokuWikiStick/dokuwiki/data/pages/软件/unity/在editmode中保持update.txt · Last modified: 2022/12/14 07:02 by zh