DelayCall.cs 697 B

123456789101112131415161718192021222324252627282930313233343536
  1. using UnityEngine;
  2. using System.Collections;
  3. public class DelayCall : MonoBehaviour
  4. {
  5. public DelayCallDelegate callFunc;
  6. public float delay;
  7. private float startTime;
  8. // Use this for initialization
  9. void Start ()
  10. {
  11. startTime = GameTime.time;
  12. }
  13. // Update is called once per frame
  14. void Update ()
  15. {
  16. if(GameTime.time - startTime >= delay)
  17. {
  18. callFunc();
  19. Destroy(this.gameObject);
  20. }
  21. }
  22. public delegate void DelayCallDelegate();
  23. public static void Call(DelayCallDelegate callFunc, float delay)
  24. {
  25. GameObject gameObj = new GameObject("DelayCall");
  26. DelayCall delayCall = gameObj.AddComponent<DelayCall>();
  27. delayCall.delay = delay;
  28. delayCall.callFunc = callFunc;
  29. }
  30. }