Unity - Run coroutines correctly in edit mode.在编辑模式正确运行Coroutines.

本文介绍了如何在Unity 2019.4的编辑模式下解决Coroutine在IEnumerator包含循环时卡顿问题,通过EditorApplication.update回调和QueuePlayerLoopUpdate实现非Play模式下的Coroutine更新,确保Coroutine在EditorMode下的流畅执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Unity - 在编辑模式正确运行Coroutines.

环境

Unity2019.4, Windows 10

问题

在编辑模式下,某些Coroutine可以正确运行,但是如果IEnumerator中含有while或for循环,那么Coroutine会卡在循环中,此时只有手动更新Editor(例如在Inspector中输入,或在Scene中移动某个GameObject),Coroutine才会继续迭代。

推测原因

IEnumerator所依赖的某些刷新机制在Edit Mode下没有被自动执行。

解决

在Monobehaviour或Editor类中使用EditorApplication.update callback 配合EditorApplication.QueuePlayerLoopUpdate 强制更新。

实现:

MonoBehaviour version:

[ExecuteInEditMode]
public class EditModeUpdateHandler : MonoBehaviour
{
    ...
    void OnEnable()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.update += ConstantLoopUpdate;
#endif
    }

#if UNITY_EDITOR
    void ConstantLoopUpdate()
    {
        if (!Application.isPlaying) {
            
            //在此加入其它判定条件.Add other condition statements here.
            
            UnityEditor.EditorApplication.QueuePlayerLoopUpdate();
            Debug.Log("Loop Updated from " + this.name);
        }
    }
#endif
    ...
}

或者采用Editor version:

...
using UnityEditor;

public class LoopUpdateEditor : Editor
{
    void OnEnable()
    {
        EditorApplication.update += ConstantLoopUpdate;
    }
    void ConstantLoopUpdate()
    {
        if (!Application.isPlaying) {
        
            //在此加入其他判定条件.Add other condition statements here.

            EditorApplication.QueuePlayerLoopUpdate();
            Debug.Log("Loop Updated from Editor!");
        }
    }
    ...
}

总结:

此方法可以在没有进入Play模式的情况下正常运行MonoBehaviour.StartCoroutine(),即使IEnumerator中含有while或for循环,即便在Unity Editor处于后台时也可运行。

推论:

1.Coroutine底层其实仍依赖Update()方法;

2.带有[ExecuteInEditMode] Attribute的MonoBehaviour中的Update()函数,只有在Scene更新或EditorGUI更新时才会执行;

3.在非Play模式下,UnityEditor.EditorApplication.update 效果等同于Play模式下的MonoBehaviour.Update()

4.UnityEditor.EditorApplication.QueuePlayerLoopUpdate()会调用所有带有[ExecuteInEditMode] Attribuete 或runInEditMode = true 的MonoBehaviour.Update()

参考:

[SOLVED] How to force update in edit mode.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值