1 Star 0 Fork 3

wayfarer/rongYaoDaLuCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
vp_Timer.cs 17.18 KB
一键复制 编辑 原始数据 按行查看 历史
liyonghelpme 提交于 2015-05-19 23:14 +08:00 . rong Yao Da Lu Some Code For Game
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;
public class vp_Timer : MonoBehaviour
{
[CompilerGenerated]
private static Callback <>f__am$cache8;
private static List<Event> m_Active = new List<Event>();
private static int m_EventBatch = 0;
private static int m_EventCount = 0;
private static int m_EventIterator = 0;
private static GameObject m_MainObject = null;
private static Event m_NewEvent = null;
private static List<Event> m_Pool = new List<Event>();
public static int MaxEventsPerFrame = 500;
private void Awake()
{
if (!this.WasAddedCorrectly)
{
UnityEngine.Object.Destroy(this);
}
}
private static void Cancel(Handle handle)
{
if ((handle != null) && handle.Active)
{
handle.Id = 0;
}
}
public static void CancelAll()
{
for (int i = m_Active.Count - 1; i > -1; i--)
{
m_Active[i].Id = 0;
}
}
public static void CancelAll(string methodName)
{
for (int i = m_Active.Count - 1; i > -1; i--)
{
if (m_Active[i].MethodName == methodName)
{
m_Active[i].Id = 0;
}
}
}
public static void CancelTimerByHandle(Handle handle)
{
if (handle != null)
{
for (int i = m_Active.Count - 1; i > -1; i--)
{
if (m_Active[i].Id == handle.Id)
{
m_Active[i].Id = 0;
}
}
handle = new Handle();
}
}
public static void Delay(float fTime)
{
In(0.08f, new Callback(vp_Timer.DoNothing), 1, fTime, null);
}
public static void DestroyAll()
{
m_Active.Clear();
m_Pool.Clear();
}
public static void DoNothing()
{
}
public static int EditorGetMethodId(int eventIndex)
{
if ((eventIndex >= 0) && (eventIndex <= (m_Active.Count - 1)))
{
return m_Active[eventIndex].Id;
}
return 0;
}
public static string EditorGetMethodInfo(int eventIndex)
{
if ((eventIndex >= 0) && (eventIndex <= (m_Active.Count - 1)))
{
return m_Active[eventIndex].MethodInfo;
}
return "Argument out of range.";
}
public static Stats EditorGetStats()
{
Stats stats;
stats.Created = m_Active.Count + m_Pool.Count;
stats.Inactive = m_Pool.Count;
stats.Active = m_Active.Count;
return stats;
}
public static void In(float delay, Callback callback, Handle timerHandle = null)
{
Schedule(delay, callback, null, null, timerHandle, 1, -1f);
}
public static void In(float delay, ArgCallback callback, object arguments, Handle timerHandle = null)
{
Schedule(delay, null, callback, arguments, timerHandle, 1, -1f);
}
public static void In(float delay, Callback callback, int iterations, Handle timerHandle = null)
{
Schedule(delay, callback, null, null, timerHandle, iterations, -1f);
}
public static void In(float delay, ArgCallback callback, object arguments, int iterations, Handle timerHandle = null)
{
Schedule(delay, null, callback, arguments, timerHandle, iterations, -1f);
}
public static void In(float delay, Callback callback, int iterations, float interval, Handle timerHandle = null)
{
Schedule(delay, callback, null, null, timerHandle, iterations, interval);
}
public static void In(float delay, ArgCallback callback, object arguments, int iterations, float interval, Handle timerHandle = null)
{
Schedule(delay, null, callback, arguments, timerHandle, iterations, interval);
}
private static void Schedule(float time, Callback func, ArgCallback argFunc, object args, Handle timerHandle, int iterations, float interval)
{
if ((func == null) && (argFunc == null))
{
Debug.LogError("Error: (vp_Timer) Aborted event because function is null.");
}
else
{
if (m_MainObject == null)
{
m_MainObject = new GameObject("Timers");
m_MainObject.AddComponent<vp_Timer>();
UnityEngine.Object.DontDestroyOnLoad(m_MainObject);
}
time = Mathf.Max(0f, time);
iterations = Mathf.Max(0, iterations);
interval = (interval != -1f) ? Mathf.Max(0f, interval) : time;
m_NewEvent = null;
if (m_Pool.Count > 0)
{
m_NewEvent = m_Pool[0];
m_Pool.Remove(m_NewEvent);
}
else
{
m_NewEvent = new Event();
}
m_EventCount++;
m_NewEvent.Id = m_EventCount;
if (func != null)
{
m_NewEvent.Function = func;
}
else if (argFunc != null)
{
m_NewEvent.ArgFunction = argFunc;
m_NewEvent.Arguments = args;
}
m_NewEvent.StartTime = Time.time;
m_NewEvent.DueTime = Time.time + time;
m_NewEvent.Iterations = iterations;
m_NewEvent.Interval = interval;
m_NewEvent.LifeTime = 0f;
m_NewEvent.Paused = false;
m_Active.Add(m_NewEvent);
if (timerHandle != null)
{
if (timerHandle.Active)
{
timerHandle.Cancel();
}
timerHandle.Id = m_NewEvent.Id;
}
}
}
public static void Start(Handle timerHandle)
{
if (<>f__am$cache8 == null)
{
<>f__am$cache8 = delegate {
};
}
Schedule(3.1536E+08f, <>f__am$cache8, null, null, timerHandle, 1, -1f);
}
private void Update()
{
m_EventBatch = 0;
while ((m_Active.Count > 0) && (m_EventBatch < MaxEventsPerFrame))
{
if (m_EventIterator < 0)
{
m_EventIterator = m_Active.Count - 1;
break;
}
if (m_EventIterator > (m_Active.Count - 1))
{
m_EventIterator = m_Active.Count - 1;
}
if ((Time.time >= m_Active[m_EventIterator].DueTime) || (m_Active[m_EventIterator].Id == 0))
{
m_Active[m_EventIterator].Execute();
}
else if (m_Active[m_EventIterator].Paused)
{
Event local1 = m_Active[m_EventIterator];
local1.DueTime += Time.deltaTime;
}
else
{
Event local2 = m_Active[m_EventIterator];
local2.LifeTime += Time.deltaTime;
}
m_EventIterator--;
m_EventBatch++;
}
}
public bool WasAddedCorrectly
{
get
{
if (!Application.isPlaying)
{
return false;
}
if (base.gameObject != m_MainObject)
{
return false;
}
return true;
}
}
public delegate void ArgCallback(object args);
public delegate void Callback();
private class Event
{
public vp_Timer.ArgCallback ArgFunction;
public object Arguments;
public float DueTime;
public vp_Timer.Callback Function;
public int Id;
public float Interval = -1f;
public int Iterations = 1;
public float LifeTime;
public bool Paused;
public float StartTime;
private void Destroy()
{
vp_Timer.m_Active.Remove(this);
vp_Timer.m_Pool.Remove(this);
}
private void Error(string message)
{
Debug.LogError("Error: (vp_Timer.Event) " + message);
}
public void Execute()
{
if ((this.Id == 0) || (this.DueTime == 0f))
{
this.Recycle();
}
else
{
if (this.Function != null)
{
this.Function();
}
else if (this.ArgFunction != null)
{
this.ArgFunction(this.Arguments);
}
else
{
this.Error("Aborted event because function is null.");
this.Recycle();
return;
}
if (this.Iterations > 0)
{
this.Iterations--;
if (this.Iterations < 1)
{
this.Recycle();
return;
}
}
this.DueTime = Time.time + this.Interval;
}
}
private void Recycle()
{
this.Id = 0;
this.DueTime = 0f;
this.StartTime = 0f;
this.Function = null;
this.ArgFunction = null;
this.Arguments = null;
if (vp_Timer.m_Active.Remove(this))
{
vp_Timer.m_Pool.Add(this);
}
}
public string MethodInfo
{
get
{
string methodName = this.MethodName;
if (string.IsNullOrEmpty(methodName))
{
return "(function = null)";
}
methodName = methodName + "(";
if (this.Arguments != null)
{
if (this.Arguments.GetType().IsArray)
{
object[] arguments = (object[]) this.Arguments;
foreach (object obj2 in arguments)
{
methodName = methodName + obj2.ToString();
if (Array.IndexOf<object>(arguments, obj2) < (arguments.Length - 1))
{
methodName = methodName + ", ";
}
}
}
else
{
methodName = methodName + this.Arguments;
}
}
return (methodName + ")");
}
}
public string MethodName
{
get
{
if (this.Function != null)
{
if (this.Function.Method != null)
{
if (this.Function.Method.Name[0] == '<')
{
return "delegate";
}
return this.Function.Method.Name;
}
}
else if ((this.ArgFunction != null) && (this.ArgFunction.Method != null))
{
if (this.ArgFunction.Method.Name[0] == '<')
{
return "delegate";
}
return this.ArgFunction.Method.Name;
}
return null;
}
}
}
public class Handle
{
private vp_Timer.Event m_Event;
private float m_FirstDueTime;
private int m_Id;
private int m_StartIterations = 1;
public void Cancel()
{
vp_Timer.Cancel(this);
}
public void Execute()
{
this.m_Event.DueTime = Time.time;
}
public bool Active
{
get
{
return ((((this.m_Event != null) && (this.Id != 0)) && (this.m_Event.Id != 0)) && (this.m_Event.Id == this.Id));
}
}
public float Delay
{
get
{
return (Mathf.Round((this.m_FirstDueTime - this.TimeOfInitiation) * 1000f) / 1000f);
}
}
public float Duration
{
get
{
if (this.Active)
{
return this.m_Event.LifeTime;
}
return 0f;
}
}
public float DurationLeft
{
get
{
if (this.Active)
{
return (this.TimeUntilNextIteration + ((this.m_Event.Iterations - 1) * this.m_Event.Interval));
}
return 0f;
}
}
public float DurationTotal
{
get
{
if (this.Active)
{
return (this.Delay + (this.m_StartIterations * ((this.m_StartIterations <= 1) ? 0f : this.Interval)));
}
return 0f;
}
}
public int Id
{
get
{
return this.m_Id;
}
set
{
this.m_Id = value;
if (this.m_Id == 0)
{
this.m_Event.DueTime = 0f;
}
else
{
this.m_Event = null;
for (int i = vp_Timer.m_Active.Count - 1; i > -1; i--)
{
if (vp_Timer.m_Active[i].Id == this.m_Id)
{
this.m_Event = vp_Timer.m_Active[i];
break;
}
}
if (this.m_Event == null)
{
Debug.LogError("Error: (vp_Timer.Handle) Failed to assign event with Id '" + this.m_Id + "'.");
}
this.m_StartIterations = this.m_Event.Iterations;
this.m_FirstDueTime = this.m_Event.DueTime;
}
}
}
public float Interval
{
get
{
if (this.Active)
{
return this.m_Event.Interval;
}
return 0f;
}
}
public int IterationsLeft
{
get
{
if (this.Active)
{
return this.m_Event.Iterations;
}
return 0;
}
}
public int IterationsTotal
{
get
{
return this.m_StartIterations;
}
}
public string MethodInfo
{
get
{
return this.m_Event.MethodInfo;
}
}
public string MethodName
{
get
{
return this.m_Event.MethodName;
}
}
public bool Paused
{
get
{
return (this.Active && this.m_Event.Paused);
}
set
{
if (this.Active)
{
this.m_Event.Paused = value;
}
}
}
public float TimeOfFirstIteration
{
get
{
if (this.Active)
{
return this.m_FirstDueTime;
}
return 0f;
}
}
public float TimeOfInitiation
{
get
{
if (this.Active)
{
return this.m_Event.StartTime;
}
return 0f;
}
}
public float TimeOfLastIteration
{
get
{
if (this.Active)
{
return (Time.time + this.DurationLeft);
}
return 0f;
}
}
public float TimeOfNextIteration
{
get
{
if (this.Active)
{
return this.m_Event.DueTime;
}
return 0f;
}
}
public float TimeUntilNextIteration
{
get
{
if (this.Active)
{
return (this.m_Event.DueTime - Time.time);
}
return 0f;
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct Stats
{
public int Created;
public int Inactive;
public int Active;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wayfarer/rongYaoDaLuCode.git
git@gitee.com:wayfarer/rongYaoDaLuCode.git
wayfarer
rongYaoDaLuCode
rongYaoDaLuCode
master

搜索帮助