/// <summary> /// Repeat call. /// </summary> /// <param name="delaySeconds">Delay(seconds).</param> /// <param name="repeatTimes">Repeat times, set <c>1</c> for no repeat .</param> /// <param name="callBack">Call back.</param> public static vp_Timer.Handle RepeatCall(float delaySeconds, int repeatTimes, Action callback) { vp_Timer.Handle handle = new vp_Timer.Handle(); vp_Timer.In(delaySeconds, () => { handle.Cancel(); callback(); }, 1, handle); return(handle); }
private static void Cancel(vp_Timer.Handle handle) { if (handle == null) { return; } if (handle.Active) { handle.Id = 0; return; } }
private void OnEnable() { if (!NetworkServer.active) // server only, isServer doesn't work in onEnable yet for some reason return; fireTimer = new vp_Timer.Handle(); turnTimer = new vp_Timer.Handle(); float rand = Random.value; vp_Timer.In(3 + rand, arty.CmdFireBullet, 999999999, 3 + rand, fireTimer); vp_Timer.In(2 + rand, delegate() { arty.CmdTurn(Random.Range(-45f, 45f)); }, 999999999, 3 + rand, turnTimer); }
/// <summary> /// cancels a timer if the passed timer handle is still active /// </summary> private static void Cancel(vp_Timer.Handle handle) { if (handle == null) { return; } // NOTE: the below Active check is super-important for verifying timer // handle integrity. recycling 'handle.Event' if 'handle.Active' is false // will cancel the wrong event and lead to some other timer never firing // (this is because timer events are recycled but not their timer handles). if (handle.Active) { // setting the 'Id' property to zero will result in DueTime also // becoming zero, sending the event to 'Execute' in the next frame // where it will be recycled instead of executed handle.Id = 0; return; } }
/// <summary> /// this method contains all timer example code. NOTE: timer code /// does not have to be located in 'OnGUI'. it can exist anywhere /// </summary> void OnGUI() { // make the status text fade back to invisible if made yellow m_StatusColor = Color.Lerp(m_StatusColor, new Color(1, 1, 0, 0), Time.deltaTime * 0.5f); // draw the header text GUI.color = Color.white; GUILayout.Space(50); GUILayout.BeginHorizontal(); GUILayout.Space(50); GUILayout.Label("SCHEDULING EXAMPLE\n - Each of these examples will schedule some functionality in one (1)\n second using different options.\n - Please study the source code in 'Examples/Scheduling/Scheduling.cs' ..."); GUILayout.EndHorizontal(); // create an area for all the example buttons GUILayout.BeginArea(new Rect(100, 150, 400, 600)); GUI.color = Color.white; GUILayout.Label("Methods, Arguments, Delegates, Iterations, Intervals & Canceling"); // --- Example 1 --- if (DoButton("A simple method")) { vp_Timer.In(1.0f, DoMethod); } // --- Example 2 --- if (DoButton("A method with a single argument")) { vp_Timer.In(1.0f, DoMethodWithSingleArgument, 242); } // --- Example 3 --- if (DoButton("A method with multiple arguments")) { object[] arg = new object[3]; arg[0] = "December"; arg[1] = 31; arg[2] = 2012; vp_Timer.In(1.0f, DoMethodWithMultipleArguments, arg); // TIP: you can also create the object array in-line like this: //vp_Timer.In(1.0f, DoMethodWithMultipleArguments, new object[] { "December", 31, 2012 }); } // --- Example 4 --- if (DoButton("A delegate")) { vp_Timer.In(1.0f, delegate() { SmackCube(); }); } // --- Example 5 --- if (DoButton("A delegate with a single argument")) { vp_Timer.In(1.0f, delegate(object o) { int i = (int)o; SmackCube(); SetStatus("A delegate with a single argument ... \"" + i + "\""); }, 242); } // --- Example 6 --- if (DoButton("A delegate with multiple arguments")) { vp_Timer.In(1.0f, delegate(object o) { object[] argument = (object[])o; // 'unpack' object into an array string month = (string)argument[0]; // convert the first argument to a string int day = (int)argument[1]; // convert the second argument to an integer int year = (int)argument[2]; // convert the third argument to an integer SmackCube(); SetStatus("A delegate with multiple arguments ... \"" + month + " " + day + ", " + year + "\""); }, new object[] { "December", 31, 2012 }); } // --- Example 7 --- if (DoButton("5 iterations of a method")) { vp_Timer.In(1.0f, SmackCube, 5); } // --- Example 8 --- if (DoButton("5 iterations of a method, with 0.2 sec intervals")) { vp_Timer.In(1.0f, SmackCube, 5, 0.2f); } // --- Example 9 --- if (DoButton("5 iterations of a delegate, canceled after 3 seconds")) { vp_Timer.Handle timer = new vp_Timer.Handle(); vp_Timer.In(0.0f, delegate() { SmackCube(); }, 5, 1, timer); vp_Timer.In(3, delegate() { timer.Cancel(); }); } GUILayout.Label("\nMethod & object accessibility:"); // --- Example 10 --- if (DoButton("Running a method from a non-monobehaviour class", false)) { vp_Timer.In(1, delegate() { NonMonoBehaviour test = new NonMonoBehaviour(); test.Test(); }); } // --- Example 11 --- if (DoButton("Running a method from a specific external gameobject", false)) { vp_Timer.In(1, delegate() { TestComponent tc = (TestComponent)GameObject.Find("ExternalGameObject").GetComponent("TestComponent"); tc.Test("Hello World!"); }); } // --- Example 12 --- if (DoButton("Running a method from the first component of a certain type\nin current transform or any of its children", false)) { vp_Timer.In(1, delegate() { TestComponent tc = transform.root.GetComponentInChildren <TestComponent>(); tc.Test("Hello World!"); }); } // --- Example 13 --- if (DoButton("Running a method from the first component of a certain type\nin the whole Hierarchy", false)) { vp_Timer.In(1, delegate() { TestComponent tc = (TestComponent)FindObjectOfType(typeof(TestComponent)); tc.Test("Hello World!"); }); } GUILayout.EndArea(); GUI.color = m_StatusColor; GUILayout.BeginArea(new Rect(Screen.width - 255, 205, 240, 600)); GUILayout.Label(m_StatusString); GUILayout.EndArea(); }
/// <summary> /// this method contains all timer example code. NOTE: timer code /// does not have to be located in 'OnGUI'. it can exist anywhere /// </summary> void OnGUI() { // make the status text fade back to invisible if made yellow m_StatusColor = Color.Lerp(m_StatusColor, new Color(1, 1, 0, 0), Time.deltaTime * 0.5f); // draw the header text GUI.color = Color.white; GUILayout.Space(50); GUILayout.BeginHorizontal(); GUILayout.Space(50); GUILayout.Label("SCHEDULING EXAMPLE\n - Each of these examples will schedule some functionality in one (1)\n second using different options.\n - Please study the source code in 'Examples/Scheduling/Scheduling.cs' ..."); GUILayout.EndHorizontal(); // create an area for all the example buttons GUILayout.BeginArea(new Rect(100, 150, 400, 600)); GUI.color = Color.white; GUILayout.Label("Methods, Arguments, Delegates, Iterations, Intervals & Canceling"); // --- Example 1 --- if (DoButton("A simple method")) { vp_Timer.In(1.0f, DoMethod); } // --- Example 2 --- if (DoButton("A method with a single argument")) { vp_Timer.In(1.0f, DoMethodWithSingleArgument, 242); } // --- Example 3 --- if (DoButton("A method with multiple arguments")) { object[] arg = new object[3]; arg[0] = "December"; arg[1] = 31; arg[2] = 2012; vp_Timer.In(1.0f, DoMethodWithMultipleArguments, arg); // TIP: you can also create the object array in-line like this: //vp_Timer.In(1.0f, DoMethodWithMultipleArguments, new object[] { "December", 31, 2012 }); } // --- Example 4 --- if (DoButton("A delegate")) { vp_Timer.In(1.0f, delegate() { SmackCube(); }); } // --- Example 5 --- if (DoButton("A delegate with a single argument")) { vp_Timer.In(1.0f, delegate(object o) { int i = (int)o; SmackCube(); SetStatus("A delegate with a single argument ... \"" + i + "\""); }, 242); } // --- Example 6 --- if (DoButton("A delegate with multiple arguments")) { vp_Timer.In(1.0f, delegate(object o) { object[] argument = (object[])o; // 'unpack' object into an array string month = (string)argument[0]; // convert the first argument to a string int day = (int)argument[1]; // convert the second argument to an integer int year = (int)argument[2]; // convert the third argument to an integer SmackCube(); SetStatus("A delegate with multiple arguments ... \"" + month + " " + day + ", " + year + "\""); }, new object[] { "December", 31, 2012 }); } // --- Example 7 --- if (DoButton("5 iterations of a method")) { vp_Timer.In(1.0f, SmackCube, 5); } // --- Example 8 --- if (DoButton("5 iterations of a method, with 0.2 sec intervals")) { vp_Timer.In(1.0f, SmackCube, 5, 0.2f); } // --- Example 9 --- if (DoButton("5 iterations of a delegate, canceled after 3 seconds")) { vp_Timer.Handle timer = new vp_Timer.Handle(); vp_Timer.In(0.0f, delegate() { SmackCube(); }, 5, 1, timer); vp_Timer.In(3, delegate() { timer.Cancel(); }); } GUILayout.Label("\nMethod & object accessibility:"); // --- Example 10 --- if (DoButton("Running a method from a non-monobehaviour class", false)) { vp_Timer.In(1, delegate() { NonMonoBehaviour test = new NonMonoBehaviour(); test.Test(); }); } // --- Example 11 --- if (DoButton("Running a method from a specific external gameobject", false)) { vp_Timer.In(1, delegate() { TestComponent tc = (TestComponent)GameObject.Find("ExternalGameObject").GetComponent("TestComponent"); tc.Test("Hello World!"); }); } // --- Example 12 --- if (DoButton("Running a method from the first component of a certain type\nin current transform or any of its children", false)) { vp_Timer.In(1, delegate() { TestComponent tc = transform.root.GetComponentInChildren<TestComponent>(); tc.Test("Hello World!"); }); } // --- Example 13 --- if (DoButton("Running a method from the first component of a certain type\nin the whole Hierarchy", false)) { vp_Timer.In(1, delegate() { TestComponent tc = (TestComponent)FindObjectOfType(typeof(TestComponent)); tc.Test("Hello World!"); }); } GUILayout.EndArea(); GUI.color = m_StatusColor; GUILayout.BeginArea(new Rect(Screen.width - 255, 205, 240, 600)); GUILayout.Label(m_StatusString); GUILayout.EndArea(); }
private static void Schedule(float time, vp_Timer.Callback func, vp_Timer.ArgCallback argFunc, object args, vp_Timer.Handle timerHandle, int iterations, float interval) { if (func == null && argFunc == null) { Debug.LogError("Error: (vp_Timer) Aborted event because function is null."); return; } if (vp_Timer.m_MainObject == null) { vp_Timer.m_MainObject = new GameObject("Timers"); vp_Timer.m_MainObject.AddComponent <vp_Timer>(); UnityEngine.Object.DontDestroyOnLoad(vp_Timer.m_MainObject); } time = Mathf.Max(0f, time); iterations = Mathf.Max(0, iterations); interval = ((interval != -1f) ? Mathf.Max(0f, interval) : time); vp_Timer.m_NewEvent = null; if (vp_Timer.m_Pool.Count > 0) { vp_Timer.m_NewEvent = vp_Timer.m_Pool[0]; vp_Timer.m_Pool.Remove(vp_Timer.m_NewEvent); } else { vp_Timer.m_NewEvent = new vp_Timer.Event(); } vp_Timer.m_EventCount++; vp_Timer.m_NewEvent.Id = vp_Timer.m_EventCount; if (func != null) { vp_Timer.m_NewEvent.Function = func; } else if (argFunc != null) { vp_Timer.m_NewEvent.ArgFunction = argFunc; vp_Timer.m_NewEvent.Arguments = args; } vp_Timer.m_NewEvent.StartTime = Time.time; vp_Timer.m_NewEvent.DueTime = Time.time + time; vp_Timer.m_NewEvent.Iterations = iterations; vp_Timer.m_NewEvent.Interval = interval; vp_Timer.m_NewEvent.LifeTime = 0f; vp_Timer.m_NewEvent.Paused = false; vp_Timer.m_Active.Add(vp_Timer.m_NewEvent); if (timerHandle != null) { if (timerHandle.Active) { timerHandle.Cancel(); } timerHandle.Id = vp_Timer.m_NewEvent.Id; } }
public static void Start(vp_Timer.Handle timerHandle) { vp_Timer.Schedule(3.1536E+08f, delegate { }, null, null, timerHandle, 1, -1f); }
public static void In(float delay, vp_Timer.ArgCallback callback, object arguments, int iterations, float interval, vp_Timer.Handle timerHandle = null) { vp_Timer.Schedule(delay, null, callback, arguments, timerHandle, iterations, interval); }
public static void In(float delay, vp_Timer.ArgCallback callback, object arguments, vp_Timer.Handle timerHandle = null) { vp_Timer.Schedule(delay, null, callback, arguments, timerHandle, 1, -1f); }
public static void In(float delay, vp_Timer.Callback callback, int iterations, float interval, vp_Timer.Handle timerHandle = null) { vp_Timer.Schedule(delay, callback, null, null, timerHandle, iterations, interval); }
public static void In(float delay, vp_Timer.Callback callback, vp_Timer.Handle timerHandle = null) { vp_Timer.Schedule(delay, callback, null, null, timerHandle, 1, -1f); }