DelayedDelegates

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Description)
(Code)
Line 67: Line 67:
 
public delegate void DelayedDelegateVector2(Vector3 param);
 
public delegate void DelayedDelegateVector2(Vector3 param);
 
public delegate void DelayedDelegateGameObject(GameObject param);
 
public delegate void DelayedDelegateGameObject(GameObject param);
 +
public delegate void DelayedDelegateCollider(Collider param);
  
  
Line 75: Line 76:
  
 
static List<DelayedDelegate> delayedDelegates = new List<DelayedDelegate>();
 
static List<DelayedDelegate> delayedDelegates = new List<DelayedDelegate>();
static List<System.DateTime> delayedDelegatesTime = new List<System.DateTime>();
+
static List<float> delayedDelegatesTime = new List<float>();
 +
static List<int> delayedDelegatesFrame = new List<int>();
 
 
 
     static List<DelayedDelegateParameters> delayedDelegatesParams = new List<DelayedDelegateParameters>();
 
     static List<DelayedDelegateParameters> delayedDelegatesParams = new List<DelayedDelegateParameters>();
 
static List<DelegateParameters> delegateParameters = new List<DelegateParameters>();
 
static List<DelegateParameters> delegateParameters = new List<DelegateParameters>();
static List<System.DateTime> delayedDelegatesTimeParams = new List<System.DateTime>();
+
static List<float> delayedDelegatesTimeParams = new List<float>();
 
 
 
static List<DelayedDelegateInt> delayedDelegatesInt = new List<DelayedDelegateInt>();
 
static List<DelayedDelegateInt> delayedDelegatesInt = new List<DelayedDelegateInt>();
 
static List<int> delegateParametersInt = new List<int>();
 
static List<int> delegateParametersInt = new List<int>();
static List<System.DateTime> delayedDelegatesTimeInt = new List<System.DateTime>();
+
static List<float> delayedDelegatesTimeInt = new List<float>();
 
 
 
static List<DelayedDelegateString> delayedDelegatesString = new List<DelayedDelegateString>();
 
static List<DelayedDelegateString> delayedDelegatesString = new List<DelayedDelegateString>();
 
static List<string> delegateParametersString = new List<string>();
 
static List<string> delegateParametersString = new List<string>();
static List<System.DateTime> delayedDelegatesTimeString = new List<System.DateTime>();
+
static List<float> delayedDelegatesTimeString = new List<float>();
 
 
 
static List<DelayedDelegateFloat> delayedDelegatesFloat = new List<DelayedDelegateFloat>();
 
static List<DelayedDelegateFloat> delayedDelegatesFloat = new List<DelayedDelegateFloat>();
 
static List<float> delegateParametersFloat = new List<float>();
 
static List<float> delegateParametersFloat = new List<float>();
static List<System.DateTime> delayedDelegatesTimeFloat = new List<System.DateTime>();
+
static List<float> delayedDelegatesTimeFloat = new List<float>();
  
 
static List<DelayedDelegateVector3> delayedDelegatesVector3 = new List<DelayedDelegateVector3>();
 
static List<DelayedDelegateVector3> delayedDelegatesVector3 = new List<DelayedDelegateVector3>();
 
static List<Vector3> delegateParametersVector3 = new List<Vector3>();
 
static List<Vector3> delegateParametersVector3 = new List<Vector3>();
static List<System.DateTime> delayedDelegatesTimeVector3 = new List<System.DateTime>();
+
static List<float> delayedDelegatesTimeVector3 = new List<float>();
 
 
 
static List<DelayedDelegateVector2> delayedDelegatesVector2 = new List<DelayedDelegateVector2>();
 
static List<DelayedDelegateVector2> delayedDelegatesVector2 = new List<DelayedDelegateVector2>();
 
static List<Vector2> delegateParametersVector2 = new List<Vector2>();
 
static List<Vector2> delegateParametersVector2 = new List<Vector2>();
static List<System.DateTime> delayedDelegatesTimeVector2 = new List<System.DateTime>();
+
static List<float> delayedDelegatesTimeVector2 = new List<float>();
 
 
 
static List<DelayedDelegateGameObject> delayedDelegatesGameObject = new List<DelayedDelegateGameObject>();
 
static List<DelayedDelegateGameObject> delayedDelegatesGameObject = new List<DelayedDelegateGameObject>();
 
static List<GameObject> delegateParametersGameObject = new List<GameObject>();
 
static List<GameObject> delegateParametersGameObject = new List<GameObject>();
static List<System.DateTime> delayedDelegatesTimeGameObject = new List<System.DateTime>();
+
static List<float> delayedDelegatesTimeGameObject = new List<float>();
 +
 +
static List<DelayedDelegateCollider> delayedDelegatesCollider = new List<DelayedDelegateCollider>();
 +
static List<Collider> delegateParametersCollider = new List<Collider>();
 +
static List<float> delayedDelegatesTimeCollider = new List<float>();
 
 
 
 
Line 109: Line 115:
 
 
 
static bool hasDelegates;
 
static bool hasDelegates;
static string lastDelegate;
+
 
+
+
public static string LastDelegate
+
{
+
get { return lastDelegate; }
+
}
+
+
 
public static int Count
 
public static int Count
 
{
 
{
Line 139: Line 138:
 
                 delayedDelegates.RemoveAt(x);
 
                 delayedDelegates.RemoveAt(x);
 
delayedDelegatesTime.RemoveAt(x);
 
delayedDelegatesTime.RemoveAt(x);
 +
delayedDelegatesFrame.RemoveAt(x);
 
             }
 
             }
  
Line 283: Line 283:
  
 
         }
 
         }
 +
 +
}
 +
 +
public static void RemoveAll(DelayedDelegateCollider method)
 +
{
 +
 +
for (int x = delayedDelegatesCollider.Count-1; x >= 0; x --)
 +
        {
 +
 +
            if (delayedDelegatesCollider[x] == method)
 +
            {
 +
 +
                delayedDelegatesCollider.RemoveAt(x);
 +
delayedDelegatesTimeCollider.RemoveAt(x);
 +
delegateParametersCollider.RemoveAt(x);
 +
           
 +
}
 +
 +
 +
        }
 +
 +
}
 +
 +
public static void RunAll()
 +
{
 +
 +
 +
for (int x = delayedDelegates.Count-1; x >= 0; x --)
 +
delayedDelegatesTime[x] = Time.realtimeSinceStartup;
 +
 +
for (int x = delayedDelegatesParams.Count-1; x >= 0; x --)
 +
delayedDelegatesTimeParams[x] = Time.realtimeSinceStartup;
 +
 +
for (int x = delayedDelegatesInt.Count-1; x >= 0; x --)
 +
delayedDelegatesTimeInt[x] = Time.realtimeSinceStartup;
 +
 +
for (int x = delayedDelegatesString.Count-1; x >= 0; x --)
 +
delayedDelegatesTimeString[x] = Time.realtimeSinceStartup;
 +
 +
for (int x = delayedDelegatesFloat.Count-1; x >= 0; x --)
 +
delayedDelegatesTimeFloat[x] = Time.realtimeSinceStartup;
 +
 +
for (int x = delayedDelegatesVector3.Count-1; x >= 0; x --)
 +
delayedDelegatesTimeVector3[x] = Time.realtimeSinceStartup;
 +
 +
for (int x = delayedDelegatesVector2.Count-1; x >= 0; x --)
 +
delayedDelegatesTimeVector2[x] = Time.realtimeSinceStartup;
 +
 +
for (int x = delayedDelegatesGameObject.Count-1; x >= 0; x --)
 +
delayedDelegatesTimeGameObject[x] = Time.realtimeSinceStartup;
 +
 +
for (int x = delayedDelegatesCollider.Count-1; x >= 0; x --)
 +
delayedDelegatesTimeCollider[x] = Time.realtimeSinceStartup;
 +
 +
DelayedDelegates.RunDelegates();
 
 
 
}
 
}
Line 296: Line 351:
 
             {
 
             {
  
delayedDelegatesTime[x] = System.DateTime.Now;
+
delayedDelegatesTime[x] = Time.realtimeSinceStartup;
 
             }
 
             }
  
 
         }
 
         }
 +
 +
DelayedDelegates.RunDelegates();
 
 
 
}
 
}
Line 313: Line 370:
 
             {
 
             {
 
 
delayedDelegatesTimeParams[x] = System.DateTime.Now;
+
delayedDelegatesTimeParams[x] = Time.realtimeSinceStartup;
 
            
 
            
 
}
 
}
Line 319: Line 376:
  
 
         }
 
         }
 +
 +
DelayedDelegates.RunDelegates();
 
 
 
}
 
}
Line 331: Line 390:
 
             {
 
             {
  
delayedDelegatesTimeInt[x] = System.DateTime.Now;
+
delayedDelegatesTimeInt[x] = Time.realtimeSinceStartup;
 
            
 
            
 
}
 
}
 
  
 
         }
 
         }
 +
 +
DelayedDelegates.RunDelegates();
 
 
 
}
 
}
Line 349: Line 409:
 
             {
 
             {
  
delayedDelegatesTimeString[x] = System.DateTime.Now;
+
delayedDelegatesTimeString[x] = Time.realtimeSinceStartup;
  
 
}
 
}
Line 355: Line 415:
  
 
         }
 
         }
 +
 +
DelayedDelegates.RunDelegates();
 
 
 
}
 
}
Line 367: Line 429:
 
             {
 
             {
  
delayedDelegatesTimeFloat[x] = System.DateTime.Now;
+
delayedDelegatesTimeFloat[x] = Time.realtimeSinceStartup;
  
 
}
 
}
Line 373: Line 435:
  
 
         }
 
         }
 +
 +
DelayedDelegates.RunDelegates();
 
 
 
}
 
}
Line 385: Line 449:
 
             {
 
             {
  
delayedDelegatesTimeVector3[x] = System.DateTime.Now;
+
delayedDelegatesTimeVector3[x] = Time.realtimeSinceStartup;
  
 
}
 
}
Line 391: Line 455:
  
 
         }
 
         }
 +
 +
DelayedDelegates.RunDelegates();
 
 
 
}
 
}
Line 403: Line 469:
 
             {
 
             {
  
delayedDelegatesTimeVector2[x] = System.DateTime.Now;
+
delayedDelegatesTimeVector2[x] = Time.realtimeSinceStartup;
  
 
}
 
}
Line 409: Line 475:
  
 
         }
 
         }
 +
 +
DelayedDelegates.RunDelegates();
 
 
 
}
 
}
Line 421: Line 489:
 
             {
 
             {
  
delayedDelegatesTimeGameObject[x] = System.DateTime.Now;
+
delayedDelegatesTimeGameObject[x] = Time.realtimeSinceStartup;
 
            
 
            
 
}
 
}
Line 427: Line 495:
  
 
         }
 
         }
 +
 +
DelayedDelegates.RunDelegates();
 
 
 
}
 
}
 +
 +
public static void RunAll(DelayedDelegateCollider method)
 +
{
  
public static void Add(DelayedDelegate method, float delay) { PreAdd(); delayedDelegates.Add(method); delayedDelegatesTime.Add(System.DateTime.Now + System.TimeSpan.FromSeconds(delay)); }
+
for (int x = delayedDelegatesCollider.Count-1; x >= 0; x --)
public static void Add(DelayedDelegateParameters method, DelegateParameters param, float delay) { PreAdd(); delayedDelegatesParams.Add(method); delayedDelegatesTimeParams.Add(System.DateTime.Now + System.TimeSpan.FromSeconds(delay)); delegateParameters.Add(param); }
+
        {
public static void Add(DelayedDelegateString method, string param,  float delay) { PreAdd(); delayedDelegatesString.Add(method); delayedDelegatesTimeString.Add(System.DateTime.Now + System.TimeSpan.FromSeconds(delay)); delegateParametersString.Add(param);  }
+
 
public static void Add(DelayedDelegateInt method, int param, float delay) { PreAdd(); delayedDelegatesInt.Add(method); delayedDelegatesTimeInt.Add(System.DateTime.Now + System.TimeSpan.FromSeconds(delay)); delegateParametersInt.Add(param); }
+
            if (delayedDelegatesCollider[x] == method)
public static void Add(DelayedDelegateFloat method, float param, float delay) { PreAdd(); delayedDelegatesFloat.Add(method); delayedDelegatesTimeFloat.Add(System.DateTime.Now + System.TimeSpan.FromSeconds(delay)); delegateParametersFloat.Add(param); }
+
            {
public static void Add(DelayedDelegateVector3 method, Vector3 param, float delay) { PreAdd(); delayedDelegatesVector3.Add(method); delayedDelegatesTimeVector3.Add(System.DateTime.Now + System.TimeSpan.FromSeconds(delay)); delegateParametersVector3.Add(param); }
+
 
public static void Add(DelayedDelegateVector2 method, Vector2 param, float delay) { PreAdd(); delayedDelegatesVector2.Add(method); delayedDelegatesTimeVector2.Add(System.DateTime.Now + System.TimeSpan.FromSeconds(delay)); delegateParametersVector2.Add(param); }
+
delayedDelegatesTimeCollider[x] = Time.realtimeSinceStartup;
public static void Add(DelayedDelegateGameObject method, GameObject param, float delay) { PreAdd(); delayedDelegatesGameObject.Add(method); delayedDelegatesTimeGameObject.Add(System.DateTime.Now + System.TimeSpan.FromSeconds(delay)); delegateParametersGameObject.Add(param); }
+
           
 +
}
 +
 +
 
 +
        }
 +
 +
DelayedDelegates.RunDelegates();
 +
 +
}
 +
 +
 +
public static void Add(DelayedDelegate method, float delay) { PreAdd(); delayedDelegates.Add(method); delayedDelegatesTime.Add(Time.realtimeSinceStartup + delay);  delayedDelegatesFrame.Add(0); }
 +
 +
/// <summary>
 +
/// Delays the function until the next frame
 +
/// </summary>
 +
/// <param name='method'>
 +
/// Method.
 +
/// </param>
 +
public static void Skip(DelayedDelegate method, int skipFrames) { PreAdd(); delayedDelegates.Add(method); delayedDelegatesTime.Add(0); delayedDelegatesFrame.Add(Time.frameCount+skipFrames); }
 +
 +
public static void Add(DelayedDelegateParameters method, DelegateParameters param, float delay) { PreAdd(); delayedDelegatesParams.Add(method); delayedDelegatesTimeParams.Add(Time.realtimeSinceStartup + delay); delegateParameters.Add(param); }
 +
public static void Add(DelayedDelegateString method, string param,  float delay) { PreAdd(); delayedDelegatesString.Add(method); delayedDelegatesTimeString.Add(Time.realtimeSinceStartup + delay); delegateParametersString.Add(param);  }
 +
public static void Add(DelayedDelegateInt method, int param, float delay) { PreAdd(); delayedDelegatesInt.Add(method); delayedDelegatesTimeInt.Add(Time.realtimeSinceStartup + delay); delegateParametersInt.Add(param); }
 +
public static void Add(DelayedDelegateFloat method, float param, float delay) { PreAdd(); delayedDelegatesFloat.Add(method); delayedDelegatesTimeFloat.Add(Time.realtimeSinceStartup + delay); delegateParametersFloat.Add(param); }
 +
public static void Add(DelayedDelegateVector3 method, Vector3 param, float delay) { PreAdd(); delayedDelegatesVector3.Add(method); delayedDelegatesTimeVector3.Add(Time.realtimeSinceStartup + delay); delegateParametersVector3.Add(param); }
 +
public static void Add(DelayedDelegateVector2 method, Vector2 param, float delay) { PreAdd(); delayedDelegatesVector2.Add(method); delayedDelegatesTimeVector2.Add(Time.realtimeSinceStartup + delay); delegateParametersVector2.Add(param); }
 +
public static void Add(DelayedDelegateGameObject method, GameObject param, float delay) { PreAdd(); delayedDelegatesGameObject.Add(method); delayedDelegatesTimeGameObject.Add(Time.realtimeSinceStartup + delay); delegateParametersGameObject.Add(param); }
 +
    public static void Add(DelayedDelegateCollider method, Collider param, float delay) { PreAdd(); delayedDelegatesCollider.Add(method); delayedDelegatesTimeCollider.Add(Time.realtimeSinceStartup + delay); delegateParametersCollider.Add(param); }
 
      
 
      
 
static void PreAdd()
 
static void PreAdd()
Line 459: Line 560:
 
if (hasDelegates)
 
if (hasDelegates)
 
{
 
{
+
 
 +
 
for (int x = delayedDelegates.Count-1; x >= 0; x --)
 
for (int x = delayedDelegates.Count-1; x >= 0; x --)
 
        {
 
        {
 
+
            if (System.DateTime.Now >= delayedDelegatesTime[x])
+
            if ((Time.realtimeSinceStartup >= delayedDelegatesTime[x] && delayedDelegatesFrame[x] == 0) ||  (Time.frameCount >= delayedDelegatesFrame[x] && delayedDelegatesTime[x] == 0))
 
            {
 
            {
  
lastDelegate = delayedDelegates[x].ToString();
 
 
 
delayedDelegates[x]();
 
delayedDelegates[x]();
 
                delayedDelegates.RemoveAt(x);
 
                delayedDelegates.RemoveAt(x);
 
delayedDelegatesTime.RemoveAt(x);
 
delayedDelegatesTime.RemoveAt(x);
 +
delayedDelegatesFrame.RemoveAt(x);
 +
 
            }
 
            }
+
        }
+
 
 
 +
        }
 +
 
for (int x = delayedDelegatesParams.Count-1; x >= 0; x --)
 
for (int x = delayedDelegatesParams.Count-1; x >= 0; x --)
 
        {
 
        {
 
 
            if (System.DateTime.Now >= delayedDelegatesTimeParams[x])
+
            if (Time.realtimeSinceStartup >= delayedDelegatesTimeParams[x])
 
            {
 
            {
+
lastDelegate = delayedDelegatesParams[x].ToString();
+
+
 
delayedDelegatesParams[x](delegateParameters[x]);
 
delayedDelegatesParams[x](delegateParameters[x]);
 
                delayedDelegatesParams.RemoveAt(x);
 
                delayedDelegatesParams.RemoveAt(x);
Line 497: Line 598:
 
        {
 
        {
 
 
            if (System.DateTime.Now >= delayedDelegatesTimeInt[x])
+
            if (Time.realtimeSinceStartup >= delayedDelegatesTimeInt[x])
 
            {
 
            {
+
 
lastDelegate = delayedDelegatesInt[x].ToString();
+
+
 
delayedDelegatesInt[x](delegateParametersInt[x]);
 
delayedDelegatesInt[x](delegateParametersInt[x]);
 
                delayedDelegatesInt.RemoveAt(x);
 
                delayedDelegatesInt.RemoveAt(x);
Line 515: Line 614:
 
        {
 
        {
 
 
            if (System.DateTime.Now >= delayedDelegatesTimeFloat[x])
+
            if (Time.realtimeSinceStartup >= delayedDelegatesTimeFloat[x])
 
            {
 
            {
+
 
lastDelegate = delayedDelegatesFloat[x].ToString();
+
+
 
delayedDelegatesFloat[x](delegateParametersFloat[x]);
 
delayedDelegatesFloat[x](delegateParametersFloat[x]);
 
                delayedDelegatesFloat.RemoveAt(x);
 
                delayedDelegatesFloat.RemoveAt(x);
Line 532: Line 629:
 
        {
 
        {
 
 
            if (System.DateTime.Now >= delayedDelegatesTimeString[x])
+
            if (Time.realtimeSinceStartup >= delayedDelegatesTimeString[x])
 
            {
 
            {
+
lastDelegate = delayedDelegatesString[x].ToString();
+
+
 
delayedDelegatesString[x](delegateParametersString[x]);
 
delayedDelegatesString[x](delegateParametersString[x]);
 
                delayedDelegatesString.RemoveAt(x);
 
                delayedDelegatesString.RemoveAt(x);
Line 549: Line 644:
 
        {
 
        {
 
 
            if (System.DateTime.Now >= delayedDelegatesTimeVector3[x])
+
            if (Time.realtimeSinceStartup >= delayedDelegatesTimeVector3[x])
 
            {
 
            {
+
lastDelegate = delayedDelegatesVector3[x].ToString();
+
+
 
delayedDelegatesVector3[x](delegateParametersVector3[x]);
 
delayedDelegatesVector3[x](delegateParametersVector3[x]);
 
                delayedDelegatesVector3.RemoveAt(x);
 
                delayedDelegatesVector3.RemoveAt(x);
Line 566: Line 659:
 
        {
 
        {
 
 
            if (System.DateTime.Now >= delayedDelegatesTimeVector2[x])
+
            if (Time.realtimeSinceStartup >= delayedDelegatesTimeVector2[x])
 
            {
 
            {
+
 
lastDelegate = delayedDelegatesVector2[x].ToString();
+
+
 
delayedDelegatesVector2[x](delegateParametersVector2[x]);
 
delayedDelegatesVector2[x](delegateParametersVector2[x]);
 
                delayedDelegatesVector2.RemoveAt(x);
 
                delayedDelegatesVector2.RemoveAt(x);
Line 583: Line 674:
 
        {
 
        {
 
 
            if (System.DateTime.Now >= delayedDelegatesTimeGameObject[x])
+
            if (Time.realtimeSinceStartup >= delayedDelegatesTimeGameObject[x])
 
            {
 
            {
+
 
lastDelegate = delayedDelegatesGameObject[x].ToString();
+
+
 
delayedDelegatesGameObject[x](delegateParametersGameObject[x]);
 
delayedDelegatesGameObject[x](delegateParametersGameObject[x]);
 
                delayedDelegatesGameObject.RemoveAt(x);
 
                delayedDelegatesGameObject.RemoveAt(x);
 
delayedDelegatesTimeGameObject.RemoveAt(x);
 
delayedDelegatesTimeGameObject.RemoveAt(x);
 
delegateParametersGameObject.RemoveAt(x);
 
delegateParametersGameObject.RemoveAt(x);
 +
           
 +
}
 +
 +
 +
        }
 +
 +
for (int x = delayedDelegatesCollider.Count-1; x >= 0; x --)
 +
        {
 +
 +
            if (Time.realtimeSinceStartup >= delayedDelegatesTimeCollider[x])
 +
            {
 +
 +
delayedDelegatesCollider[x](delegateParametersCollider[x]);
 +
                delayedDelegatesCollider.RemoveAt(x);
 +
delayedDelegatesTimeCollider.RemoveAt(x);
 +
delegateParametersCollider.RemoveAt(x);
 
           
 
           
 
}
 
}
Line 613: Line 718:
 
public class DelayedDelegateRun : MonoBehaviour  
 
public class DelayedDelegateRun : MonoBehaviour  
 
{
 
{
+
 
public void Awake()
+
void Update()
 
{
 
{
+
 
InvokeRepeating("Run",0,0.05f);
+
               
+
}
+
+
void Run()
+
{
+
+
 
DelayedDelegates.RunDelegates();
 
DelayedDelegates.RunDelegates();
 
 

Revision as of 04:45, 13 May 2012

Author: David O'Donoghue (Trooper)

Description

DelayedDelegates is a centralised way of calling a function at a specific time.

Usage

Create a function with either a int, float, string, Vector3, Vector2 or GameObject parameter and then call the following:

void MyFunction(int param)
{
 
   // Do somethign with param
 
}
 
// Replace '1' with your int, float, etc....
DelayedDelegates.Add(MyFunction, 1, 2)

You can pass back your own parameters by inheriting from DelegateParameters like this:

public class MyParams : DelegateParameters
{
   public int myParam1;
   public string myParam2;
 
   public MyParams(int myParam1, string myParam2)
   {
      this.myParam1 = myParam1;
      this.myParam2 = myParam2;
   }
}
 
void MyFunction(DelegateParameters param)
{
   MyParams myParam = (MyParams) param;
 
   Debug.Log(myParam.myParam1 + " : " + myParam.myParam2);
 
}
 
 
DelayedDelegates.Add(MyFunction, new MyParams(1,"HELLOW WORLD"), 2);

Code

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
 
public delegate void DelayedDelegate();
public delegate void DelayedDelegateParameters(DelegateParameters param);
public delegate void DelayedDelegateInt(int param);
public delegate void DelayedDelegateString(string param);
public delegate void DelayedDelegateFloat(float param);
public delegate void DelayedDelegateVector3(Vector3 param);
public delegate void DelayedDelegateVector2(Vector3 param);
public delegate void DelayedDelegateGameObject(GameObject param);
public delegate void DelayedDelegateCollider(Collider param);
 
 
public abstract class DelegateParameters { public Object param; }
 
public static class DelayedDelegates
{
 
	static List<DelayedDelegate> delayedDelegates = new List<DelayedDelegate>();
	static List<float> delayedDelegatesTime = new List<float>();
	static List<int> delayedDelegatesFrame = new List<int>();
 
    static List<DelayedDelegateParameters> delayedDelegatesParams = new List<DelayedDelegateParameters>();
	static List<DelegateParameters> delegateParameters = new List<DelegateParameters>();
	static List<float> delayedDelegatesTimeParams = new List<float>();
 
	static List<DelayedDelegateInt> delayedDelegatesInt = new List<DelayedDelegateInt>();
	static List<int> delegateParametersInt = new List<int>();
	static List<float> delayedDelegatesTimeInt = new List<float>();
 
	static List<DelayedDelegateString> delayedDelegatesString = new List<DelayedDelegateString>();
	static List<string> delegateParametersString = new List<string>();
	static List<float> delayedDelegatesTimeString = new List<float>();
 
	static List<DelayedDelegateFloat> delayedDelegatesFloat = new List<DelayedDelegateFloat>();
	static List<float> delegateParametersFloat = new List<float>();
	static List<float> delayedDelegatesTimeFloat = new List<float>();
 
	static List<DelayedDelegateVector3> delayedDelegatesVector3 = new List<DelayedDelegateVector3>();
	static List<Vector3> delegateParametersVector3 = new List<Vector3>();
	static List<float> delayedDelegatesTimeVector3 = new List<float>();
 
	static List<DelayedDelegateVector2> delayedDelegatesVector2 = new List<DelayedDelegateVector2>();
	static List<Vector2> delegateParametersVector2 = new List<Vector2>();
	static List<float> delayedDelegatesTimeVector2 = new List<float>();
 
	static List<DelayedDelegateGameObject> delayedDelegatesGameObject = new List<DelayedDelegateGameObject>();
	static List<GameObject> delegateParametersGameObject = new List<GameObject>();
	static List<float> delayedDelegatesTimeGameObject = new List<float>();
 
	static List<DelayedDelegateCollider> delayedDelegatesCollider = new List<DelayedDelegateCollider>();
	static List<Collider> delegateParametersCollider = new List<Collider>();
	static List<float> delayedDelegatesTimeCollider = new List<float>();
 
 
	static DelayedDelegateRun delegateRunner;
 
	static bool hasDelegates;
 
	public static int Count
	{
		get { return delayedDelegates.Count+delayedDelegatesFloat.Count+delayedDelegatesGameObject.Count+delayedDelegatesInt.Count+ delayedDelegatesParams.Count+delayedDelegatesString.Count+delayedDelegatesVector2.Count+delayedDelegatesTimeVector3.Count; }
	}
 
	public static bool HasDelegates
	{
		get { return hasDelegates; }
	}
 
	public static void RemoveAll(DelayedDelegate method)
	{
 
 
		for (int x = delayedDelegates.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegates[x] == method)
            {
 
                delayedDelegates.RemoveAt(x);
				delayedDelegatesTime.RemoveAt(x);
				delayedDelegatesFrame.RemoveAt(x);
            }
 
        }
 
	}
 
 
	public static void RemoveAll(DelayedDelegateParameters method) 
	{
 
		for (int x = delayedDelegatesParams.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesParams[x] == method)
            {
 
                delayedDelegatesParams.RemoveAt(x);
				delayedDelegatesTimeParams.RemoveAt(x);
				delegateParameters.RemoveAt(x);
 
			}
 
 
        }
 
	}
 
	public static void RemoveAll(DelayedDelegateInt method)
	{
 
		for (int x = delayedDelegatesInt.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesInt[x] == method)
            {
 
                delayedDelegatesInt.RemoveAt(x);
				delayedDelegatesTimeInt.RemoveAt(x);
				delegateParametersInt.RemoveAt(x);
 
			}
 
 
        }
 
	}
 
	public static void RemoveAll(DelayedDelegateString method)
	{
 
		for (int x = delayedDelegatesString.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesString[x] == method)
            {
 
                delayedDelegatesString.RemoveAt(x);
				delayedDelegatesTimeString.RemoveAt(x);
				delegateParametersString.RemoveAt(x);
 
			}
 
 
        }
 
	}
 
	public static void RemoveAll(DelayedDelegateFloat method)
	{
 
		for (int x = delayedDelegatesFloat.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesFloat[x] == method)
            {
 
                delayedDelegatesFloat.RemoveAt(x);
				delayedDelegatesTimeFloat.RemoveAt(x);
				delegateParametersFloat.RemoveAt(x);
 
			}
 
 
        }
 
	}
 
	public static void RemoveAll(DelayedDelegateVector3 method)
	{
 
		for (int x = delayedDelegatesVector3.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesVector3[x] == method)
            {
 
                delayedDelegatesVector3.RemoveAt(x);
				delayedDelegatesTimeVector3.RemoveAt(x);
				delegateParametersVector3.RemoveAt(x);
 
			}
 
 
        }
 
	}
 
	public static void RemoveAll(DelayedDelegateVector2 method)
	{
 
		for (int x = delayedDelegatesVector2.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesVector2[x] == method)
            {
 
                delayedDelegatesVector2.RemoveAt(x);
				delayedDelegatesTimeVector2.RemoveAt(x);
				delegateParametersVector2.RemoveAt(x);
 
			}
 
 
        }
 
	}
 
	public static void RemoveAll(DelayedDelegateGameObject method)
	{
 
		for (int x = delayedDelegatesGameObject.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesGameObject[x] == method)
            {
 
                delayedDelegatesGameObject.RemoveAt(x);
				delayedDelegatesTimeGameObject.RemoveAt(x);
				delegateParametersGameObject.RemoveAt(x);
 
			}
 
 
        }
 
	}
 
	public static void RemoveAll(DelayedDelegateCollider method)
	{
 
		for (int x = delayedDelegatesCollider.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesCollider[x] == method)
            {
 
                delayedDelegatesCollider.RemoveAt(x);
				delayedDelegatesTimeCollider.RemoveAt(x);
				delegateParametersCollider.RemoveAt(x);
 
			}
 
 
        }
 
	}
 
	public static void RunAll()
	{
 
 
		for (int x = delayedDelegates.Count-1; x >= 0; x --)
				delayedDelegatesTime[x] = Time.realtimeSinceStartup;
 
		for (int x = delayedDelegatesParams.Count-1; x >= 0; x --)
				delayedDelegatesTimeParams[x] = Time.realtimeSinceStartup;
 
		for (int x = delayedDelegatesInt.Count-1; x >= 0; x --)
			delayedDelegatesTimeInt[x] = Time.realtimeSinceStartup;
 
		for (int x = delayedDelegatesString.Count-1; x >= 0; x --)
			delayedDelegatesTimeString[x] = Time.realtimeSinceStartup;
 
		for (int x = delayedDelegatesFloat.Count-1; x >= 0; x --)
			delayedDelegatesTimeFloat[x] = Time.realtimeSinceStartup;
 
		for (int x = delayedDelegatesVector3.Count-1; x >= 0; x --)
			delayedDelegatesTimeVector3[x] = Time.realtimeSinceStartup;
 
		for (int x = delayedDelegatesVector2.Count-1; x >= 0; x --)
			delayedDelegatesTimeVector2[x] = Time.realtimeSinceStartup;
 
		for (int x = delayedDelegatesGameObject.Count-1; x >= 0; x --)
			delayedDelegatesTimeGameObject[x] = Time.realtimeSinceStartup;
 
		for (int x = delayedDelegatesCollider.Count-1; x >= 0; x --)
			delayedDelegatesTimeCollider[x] = Time.realtimeSinceStartup;
 
		DelayedDelegates.RunDelegates();
 
	}
 
	public static void RunAll(DelayedDelegate method)
	{
 
 
		for (int x = delayedDelegates.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegates[x] == method)
            {
 
				delayedDelegatesTime[x] = Time.realtimeSinceStartup;
            }
 
        }
 
		DelayedDelegates.RunDelegates();
 
	}
 
 
	public static void RunAll(DelayedDelegateParameters method) 
	{
 
		for (int x = delayedDelegatesParams.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesParams[x] == method)
            {
 
				delayedDelegatesTimeParams[x] = Time.realtimeSinceStartup;
 
			}
 
 
        }
 
		DelayedDelegates.RunDelegates();
 
	}
 
	public static void RunAll(DelayedDelegateInt method)
	{
 
		for (int x = delayedDelegatesInt.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesInt[x] == method)
            {
 
				delayedDelegatesTimeInt[x] = Time.realtimeSinceStartup;
 
			}
 
        }
 
		DelayedDelegates.RunDelegates();
 
	}
 
	public static void RunAll(DelayedDelegateString method)
	{
 
		for (int x = delayedDelegatesString.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesString[x] == method)
            {
 
				delayedDelegatesTimeString[x] = Time.realtimeSinceStartup;
 
			}
 
 
        }
 
		DelayedDelegates.RunDelegates();
 
	}
 
	public static void RunAll(DelayedDelegateFloat method)
	{
 
		for (int x = delayedDelegatesFloat.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesFloat[x] == method)
            {
 
				delayedDelegatesTimeFloat[x] = Time.realtimeSinceStartup;
 
			}
 
 
        }
 
		DelayedDelegates.RunDelegates();
 
	}
 
	public static void RunAll(DelayedDelegateVector3 method)
	{
 
		for (int x = delayedDelegatesVector3.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesVector3[x] == method)
            {
 
				delayedDelegatesTimeVector3[x] = Time.realtimeSinceStartup;
 
			}
 
 
        }
 
		DelayedDelegates.RunDelegates();
 
	}
 
	public static void RunAll(DelayedDelegateVector2 method)
	{
 
		for (int x = delayedDelegatesVector2.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesVector2[x] == method)
            {
 
				delayedDelegatesTimeVector2[x] = Time.realtimeSinceStartup;
 
			}
 
 
        }
 
		DelayedDelegates.RunDelegates();
 
	}
 
	public static void RunAll(DelayedDelegateGameObject method)
	{
 
		for (int x = delayedDelegatesGameObject.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesGameObject[x] == method)
            {
 
				delayedDelegatesTimeGameObject[x] = Time.realtimeSinceStartup;
 
			}
 
 
        }
 
		DelayedDelegates.RunDelegates();
 
	}
 
	public static void RunAll(DelayedDelegateCollider method)
	{
 
		for (int x = delayedDelegatesCollider.Count-1; x >= 0; x --)
        {
 
            if (delayedDelegatesCollider[x] == method)
            {
 
				delayedDelegatesTimeCollider[x] = Time.realtimeSinceStartup;
 
			}
 
 
        }
 
		DelayedDelegates.RunDelegates();
 
	}
 
 
	public static void Add(DelayedDelegate method, float delay) { PreAdd(); delayedDelegates.Add(method); delayedDelegatesTime.Add(Time.realtimeSinceStartup + delay);  delayedDelegatesFrame.Add(0); }
 
	/// <summary>
	/// Delays the function until the next frame
	/// </summary>
	/// <param name='method'>
	/// Method.
	/// </param>
	public static void Skip(DelayedDelegate method, int skipFrames) { PreAdd(); delayedDelegates.Add(method); delayedDelegatesTime.Add(0); delayedDelegatesFrame.Add(Time.frameCount+skipFrames); }
 
	public static void Add(DelayedDelegateParameters method, DelegateParameters param, float delay) { PreAdd(); delayedDelegatesParams.Add(method); delayedDelegatesTimeParams.Add(Time.realtimeSinceStartup + delay); delegateParameters.Add(param); }
	public static void Add(DelayedDelegateString method, string param,  float delay) { PreAdd(); delayedDelegatesString.Add(method); delayedDelegatesTimeString.Add(Time.realtimeSinceStartup + delay); delegateParametersString.Add(param);  }
	public static void Add(DelayedDelegateInt method, int param, float delay) { PreAdd(); delayedDelegatesInt.Add(method); delayedDelegatesTimeInt.Add(Time.realtimeSinceStartup + delay); delegateParametersInt.Add(param); }
	public static void Add(DelayedDelegateFloat method, float param, float delay) { PreAdd(); delayedDelegatesFloat.Add(method); delayedDelegatesTimeFloat.Add(Time.realtimeSinceStartup + delay); delegateParametersFloat.Add(param); }
	public static void Add(DelayedDelegateVector3 method, Vector3 param, float delay) { PreAdd(); delayedDelegatesVector3.Add(method); delayedDelegatesTimeVector3.Add(Time.realtimeSinceStartup + delay); delegateParametersVector3.Add(param); }
	public static void Add(DelayedDelegateVector2 method, Vector2 param, float delay) { PreAdd(); delayedDelegatesVector2.Add(method); delayedDelegatesTimeVector2.Add(Time.realtimeSinceStartup + delay); delegateParametersVector2.Add(param); }
	public static void Add(DelayedDelegateGameObject method, GameObject param, float delay) { PreAdd(); delayedDelegatesGameObject.Add(method); delayedDelegatesTimeGameObject.Add(Time.realtimeSinceStartup + delay); delegateParametersGameObject.Add(param); }
    public static void Add(DelayedDelegateCollider method, Collider param, float delay) { PreAdd(); delayedDelegatesCollider.Add(method); delayedDelegatesTimeCollider.Add(Time.realtimeSinceStartup + delay); delegateParametersCollider.Add(param); }
 
	static void PreAdd()
	{
 
		if (delegateRunner == null)
		{
 
			GameObject go = new GameObject("DelayedDelegates");
			delegateRunner = (DelayedDelegateRun) go.AddComponent<DelayedDelegateRun>();
 
		}
 
		hasDelegates = true;
 
	}
 
    public static void RunDelegates()
    {
 
		if (hasDelegates)
		{
 
 
			for (int x = delayedDelegates.Count-1; x >= 0; x --)
	        {
 
	            if ((Time.realtimeSinceStartup >= delayedDelegatesTime[x] && delayedDelegatesFrame[x] == 0) ||  (Time.frameCount >= delayedDelegatesFrame[x] && delayedDelegatesTime[x] == 0))
	            {
 
					delayedDelegates[x]();
	                delayedDelegates.RemoveAt(x);
					delayedDelegatesTime.RemoveAt(x);
					delayedDelegatesFrame.RemoveAt(x);
 
	            }
 
 
	        }
 
			for (int x = delayedDelegatesParams.Count-1; x >= 0; x --)
	        {
 
	            if (Time.realtimeSinceStartup >= delayedDelegatesTimeParams[x])
	            {
 
					delayedDelegatesParams[x](delegateParameters[x]);
	                delayedDelegatesParams.RemoveAt(x);
					delayedDelegatesTimeParams.RemoveAt(x);
					delegateParameters.RemoveAt(x);
 
				}
 
 
	        }
 
 
			for (int x = delayedDelegatesInt.Count-1; x >= 0; x --)
	        {
 
	            if (Time.realtimeSinceStartup >= delayedDelegatesTimeInt[x])
	            {
 
					delayedDelegatesInt[x](delegateParametersInt[x]);
	                delayedDelegatesInt.RemoveAt(x);
					delayedDelegatesTimeInt.RemoveAt(x);
					delegateParametersInt.RemoveAt(x);
 
				}
 
 
	        }
 
			for (int x = delayedDelegatesFloat.Count-1; x >= 0; x --)
	        {
 
	            if (Time.realtimeSinceStartup >= delayedDelegatesTimeFloat[x])
	            {
 
					delayedDelegatesFloat[x](delegateParametersFloat[x]);
	                delayedDelegatesFloat.RemoveAt(x);
					delayedDelegatesTimeFloat.RemoveAt(x);
					delegateParametersFloat.RemoveAt(x);
 
				}
 
	        }
 
			for (int x = delayedDelegatesString.Count-1; x >= 0; x --)
	        {
 
	            if (Time.realtimeSinceStartup >= delayedDelegatesTimeString[x])
	            {
 
					delayedDelegatesString[x](delegateParametersString[x]);
	                delayedDelegatesString.RemoveAt(x);
					delayedDelegatesTimeString.RemoveAt(x);
					delegateParametersString.RemoveAt(x);
 
				}
 
	        }
 
			for (int x = delayedDelegatesVector3.Count-1; x >= 0; x --)
	        {
 
	            if (Time.realtimeSinceStartup >= delayedDelegatesTimeVector3[x])
	            {
 
					delayedDelegatesVector3[x](delegateParametersVector3[x]);
	                delayedDelegatesVector3.RemoveAt(x);
					delayedDelegatesTimeVector3.RemoveAt(x);
					delegateParametersVector3.RemoveAt(x);
 
				}
 
	        }
 
			for (int x = delayedDelegatesVector2.Count-1; x >= 0; x --)
	        {
 
	            if (Time.realtimeSinceStartup >= delayedDelegatesTimeVector2[x])
	            {
 
					delayedDelegatesVector2[x](delegateParametersVector2[x]);
	                delayedDelegatesVector2.RemoveAt(x);
					delayedDelegatesTimeVector2.RemoveAt(x);
					delegateParametersVector2.RemoveAt(x);
 
				}
 
	        }
 
			for (int x = delayedDelegatesGameObject.Count-1; x >= 0; x --)
	        {
 
	            if (Time.realtimeSinceStartup >= delayedDelegatesTimeGameObject[x])
	            {
 
					delayedDelegatesGameObject[x](delegateParametersGameObject[x]);
	                delayedDelegatesGameObject.RemoveAt(x);
					delayedDelegatesTimeGameObject.RemoveAt(x);
					delegateParametersGameObject.RemoveAt(x);
 
				}
 
 
	        }
 
			for (int x = delayedDelegatesCollider.Count-1; x >= 0; x --)
	        {
 
	            if (Time.realtimeSinceStartup >= delayedDelegatesTimeCollider[x])
	            {
 
					delayedDelegatesCollider[x](delegateParametersCollider[x]);
	                delayedDelegatesCollider.RemoveAt(x);
					delayedDelegatesTimeCollider.RemoveAt(x);
					delegateParametersCollider.RemoveAt(x);
 
				}
 
 
	        }
 
			if (delayedDelegates.Count == 0 && delayedDelegatesFloat.Count == 0 && delayedDelegatesGameObject.Count == 0 && delayedDelegatesParams.Count == 0
			    && delayedDelegatesInt.Count == 0 && delayedDelegatesString.Count == 0 && delayedDelegatesVector2.Count == 0 && delayedDelegatesVector3.Count == 0)
			{
				hasDelegates = false;
			}
 
		}
 
 
    }
 
}
 
public class DelayedDelegateRun : MonoBehaviour 
{
 
	void Update()
	{
 
		DelayedDelegates.RunDelegates();
 
	}
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox