Unity3D不繼承MonoBehaviour的類需要Invoke、StartCoroutine的運用


using UnityEngine;
using System.Collections; using System.Collections.Generic; /// <summary> /// 方法執行工具 /// </summary> public static class MethodExeTool 
{ static private ToolScript _script; /// <summary> /// 工具腳本 /// </summary> /// <value>The coroutine object.</value> static private ToolScript script{ get{ if(_script==null){ GameObject go=new GameObject("toolObj"); _script=go.AddComponent<ToolScript>(); } return _script; } } /// <summary> /// 啟動協程 /// </summary> /// <param name="enumerator">Enumerator.</param> static public void StartCoroutine(IEnumerator enumerator){ script.StartCoroutine (enumerator); } /// <summary> /// 循環調用 /// </summary> /// <param name="method">Method.</param> /// <param name="time">Time.</param> /// <param name="repeat">Repeat.</param> static public void Invoke(InvokeHandler.Handler method,float time,int repeat=1){ InvokeHandler temp = new InvokeHandler (); temp.method = method; temp.repeatTime = time; temp.repeat = repeat; script.AddHandler(temp); } /// <summary> /// 取消循環調用 /// </summary> /// <returns><c>true</c> if cancel invoke the specified method; otherwise, <c>false</c>.</returns> /// <param name="method">Method.</param> static public void CancelInvoke(InvokeHandler.Handler method){ script.RemoveHandler (method); } /// <summary> /// 循環調用結構 /// </summary> public class InvokeHandler{ /// <summary> /// 委托 /// </summary> public delegate void Handler(); public Handler method; /// <summary> /// 間隔時間 /// </summary> public float repeatTime=1f; /// <summary> /// 調用次數 /// </summary> public int repeat=1; /// <summary> /// 當前時間 /// </summary> public float curTime=0f; /// <summary> /// 是否完成 /// </summary> /// <value><c>true</c> if this instance is complete; otherwise, <c>false</c>.</value> public bool IsComplete{ get{ return repeat<1; } } /// <summary> /// 更新時間 /// </summary> /// <param name="time">Time.</param> public void AddTime(float time){ curTime += time; if (curTime >= repeatTime) { curTime-=repeatTime; execute(); } } /// <summary> /// 執行 /// </summary> void execute(){ if (repeat > 0) { if(method!=null){ method(); } } repeat--; } } } /// <summary> /// 工具腳本類 /// </summary> class ToolScript :MonoBehaviour{ /// <summary> /// 循環調用列表 /// </summary> private Dictionary<MethodExeTool.InvokeHandler.Handler,MethodExeTool.InvokeHandler> handlerList = new Dictionary<MethodExeTool.InvokeHandler.Handler, MethodExeTool.InvokeHandler> (); /// <summary> /// 更新時間 /// </summary> void Update(){ foreach (MethodExeTool.InvokeHandler temp in handlerList.Values) { temp.AddTime(Time.deltaTime); } DeleteComplete (); } /// <summary> /// 刪除已完成 /// </summary> void DeleteComplete(){ List<MethodExeTool.InvokeHandler> list = new List<MethodExeTool.InvokeHandler> (); foreach (MethodExeTool.InvokeHandler temp in handlerList.Values) { if(temp.IsComplete){ list.Add(temp); } } foreach (MethodExeTool.InvokeHandler del in list) { handlerList.Remove(del.method); } } /// <summary> /// 添加調用 /// </summary> /// <param name="temp">Temp.</param> public void AddHandler(MethodExeTool.InvokeHandler temp){ RemoveHandler (temp.method); handlerList.Add (temp.method, temp); } /// <summary> /// 刪除調用 /// </summary> /// <param name="method">Method.</param> public void RemoveHandler(MethodExeTool.InvokeHandler.Handler method){ if (handlerList.ContainsKey (method)) { handlerList.Remove(method); } } }

http://www.unitymanual.com/thread-21718-1-1.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM