ActionTimer.cs 912 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. namespace ZenFulcrum.EmbeddedBrowser {
  6. /** When our trigger is touched by the player, does the actions in the list. (one-shot) */
  7. public class ActionTimer : MonoBehaviour {
  8. [Serializable]
  9. public class TimedAction {
  10. /** How long since the action should we wait? */
  11. public float delay;
  12. /** What action should we take? */
  13. public UnityEvent action;
  14. }
  15. public TimedAction[] thingsToDo;
  16. private bool triggered;
  17. public void OnTriggerEnter(Collider other) {
  18. if (triggered) return;
  19. var inventory = other.GetComponent<PlayerInventory>();
  20. if (!inventory) return;
  21. triggered = true;
  22. StartCoroutine(DoThings());
  23. }
  24. private IEnumerator DoThings() {
  25. for (int idx = 0; idx < thingsToDo.Length; ++idx) {
  26. yield return new WaitForSeconds(thingsToDo[idx].delay);
  27. thingsToDo[idx].action.Invoke();
  28. }
  29. }
  30. }
  31. }