在继承MonoBehavior的脚本中,我们通常会使用协程来延迟执行某块代码,或者做个计时器之类的功能
那我们能不能把这块代码公共起来呢?
今天我给分享个CoroutineUtil类,在项目创建即挂载,且永不销毁
下面为代码内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
/********************* * 姓名:王冲 * 功能:延迟指定时间后调用回调方法,可用于解决部分时序问题。 * 日期:2017/5/27 **********************/ using UnityEngine; using System.Collections; using System; public class CoroutineUtil : MonoBehaviour { private static CoroutineUtil instance; public static CoroutineUtil Instance {get{return instance;}} void Awake() { instance = this; } /// <summary> /// 等待指定时间后,执行回调方法 /// </summary> /// <param name="time">等待时间 秒,0为1帧</param> /// <param name="ignoreTimeScale">是否忽略TimeScale影响</param> /// <param name="callBack">回调方法</param> /// <param name="objects">回调参数</param> /// <returns></returns> public Coroutine WaitTime(float time = 0, bool ignoreTimeScale = false, Action<object[]> callBack = null, params object[] objects) { return StartCoroutine(ICoroutine(time, ignoreTimeScale, callBack, objects)); } /// <summary> /// 倒计时或正计时 每隔指定时间回调一次 /// </summary> /// <param name="startTime">开始时间</param> /// <param name="endTime">结束时间</param> /// <param name="perTime">回调间隔</param> /// <param name="ignoreTimeScale">是否忽略TimeScale影响</param> /// <param name="callBack">回调方法</param> /// <returns></returns> public Coroutine WaitPerSecond(float startTime = 10, float endTime = 0, float perTime = 1, bool ignoreTimeScale = false, Action<float> callBack = null) { return StartCoroutine(ICoroutine(startTime, endTime, perTime, ignoreTimeScale, callBack)); } /// <summary> /// 等待FixedUpdate时间后执行回调 /// </summary> /// <param name="time">等待时间 秒,0为1帧</param> /// <param name="callBack">回调方法</param> /// <param name="objects">回调参数</param> /// <returns></returns> public Coroutine WaitFixedUpdate(float time = 0, Action<object[]> callBack = null, params object[] objects) { return StartCoroutine(ICoroutine(time, callBack, objects)); } /// <summary> /// 主动停止指定协程 /// </summary> /// <param name="coroutine"></param> public void Stop(Coroutine coroutine) { if (coroutine != null) { StopCoroutine(coroutine); } } IEnumerator ICoroutine( float time = 0, bool ignoreTimeScale = false, Action<object[]> callBack = null, params object[] objects) { if (time == 0) { yield return null; } else if (time > 0) { if (ignoreTimeScale) { float start = Time.realtimeSinceStartup; while (Time.realtimeSinceStartup < start + time) { yield return null; } } else yield return new WaitForSeconds(time); } if (callBack != null) { callBack(objects); } } IEnumerator ICoroutine(float beginTime = 10, float endTime = 0, float perTime = 1, bool ignoreTimeScale = false, Action<float> callBack = null) { if (beginTime > endTime) { while (beginTime > endTime) { if (ignoreTimeScale) { float start = Time.realtimeSinceStartup; while (Time.realtimeSinceStartup < start + perTime) { yield return null; } } else yield return new WaitForSeconds(perTime); beginTime -= perTime; if (callBack != null) { callBack(beginTime); } } } else if (beginTime < endTime) { while (beginTime < endTime) { if (ignoreTimeScale) { float start = Time.realtimeSinceStartup; while (Time.realtimeSinceStartup < start + perTime) { yield return null; } } else yield return new WaitForSeconds(perTime); beginTime += perTime; if (callBack != null) { callBack(beginTime); } } } } IEnumerator ICoroutine(float waitTime, Action<object[]> callBack = null, params object[] objects) { if (waitTime == 0) { yield return new WaitForFixedUpdate(); } else if(waitTime > 0) { float time = 0; while (time < waitTime) { yield return new WaitForFixedUpdate(); time += Time.fixedDeltaTime; } } if (callBack != null) { callBack(objects); } } } |
- 本文固定链接: http://www.u3d8.com/?p=1170
- 转载请注明: 网虫虫 在 u3d8.com 发表过