美文网首页unityUnityEditor
[Unity 3d] Unity Editor Coroutin

[Unity 3d] Unity Editor Coroutin

作者: 雨落随风 | 来源:发表于2019-06-03 23:03 被阅读0次

在简书上整理一个与 Unity 有关的一系列 GitHub 仓库,希望能帮助到有心人。
笔者计划以一篇文章一个功能点的方式展开,拾取超方便!

简介:

笔者今天推荐的仓库叫 Unity Editor Coroutine,编辑器协程 。
顾名思义,这个组件就是为 编辑器脚本编程提供“异步”操作能力的,类似常规协程作用的东东啦
有了它,就可以使用协程方便的在编辑器脚本中写定时器啊,网络异步加载等等。

功能:

就像常规操作协程一样:

  • 支持 迭代器 开协程
  • 支持 字符串 开协程
  • 支持 WaitForSeconds,WWW ,WaitUntil,WaitWhile
  • 支持 协程嵌套开启
  • 绘制的窗口关闭,该窗口上运行中的协程自动消亡(原仓库未加入该体验)

实战:

   EditorCoroutine coroutine;
        void OnGUI()
        {
           if (GUILayout.Button("StartByString")) //演示字符串开协程
            {
                this.StartCoroutine("Example");
            }
            if (GUILayout.Button("StartByFunc")) //演示迭代器开协程
            {
               //持有协程引用便于定点关闭  
                coroutine= this.StartCoroutine(Example());
            }
            
            if (GUILayout.Button("StopByString"))
            {
                //演示字符串关闭协程,该字符串将关闭该脚本下的所有同名协程
                this.StopCoroutine("Example");
            }
            if (GUILayout.Button("StopByCoroutine")) 
            {
                this.StopCoroutine(coroutine); //演示利用协程引用精准关闭协程
            }
}
  • 节选自仓库的实例脚本:EditorCoroutineExtensions.cs
  • 该脚本演示了 EditorCoroutine 的几乎全部的 API 。
  • 测试窗口的路径在: Window / Coroutine Example

演示:

EditorCoroutine
Tips : 点了 Start WWW 没啥反应是因为笔者用的手机热点,网速慢导致的哈。。

链接

Unity-Editor-Coroutine

结语:

常常因为 EditorWindow Notification 自动退却的太慢了而苦恼,现在有了这个组件,再也不用等待 Notification 消失了,简直是处女座福音啊(手动滑稽)。

扩展阅读

marijnz/unity-editor-coroutines: Coroutines for Editor scripts, just like regular coroutines.
Tips:主要逻辑来自于该仓库,重构了代码,重新选择了数据结构,对API进行了增删。

本文集持续更新ing

相关文章

网友评论

    本文标题:[Unity 3d] Unity Editor Coroutin

    本文链接:https://www.haomeiwen.com/subject/ahhptctx.html