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