LightShow.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Enables one of the objects on its list at a time, and switches between them at a fixed time interval.
  6. /// Used in ZED Dark Room sample to switch the sequence of lights.
  7. /// </summary>
  8. public class LightShow : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// How long each "show" lasts before its object is disabled and the next is enabled.
  12. /// </summary>
  13. [Tooltip("How long each 'show' lasts before its object is disabled and the next is enabled. ")]
  14. public float sequenceDurationSeconds = 16;
  15. /// <summary>
  16. /// Each object that holds a "show". Should contain or be a parent of all light objects it interacts with.
  17. /// </summary>
  18. [Tooltip("Each object that holds a 'show'. Should contain or be a parent of all light objects it interacts with.")]
  19. public List<GameObject> sequenceObjects = new List<GameObject>();
  20. /// <summary>
  21. /// Runtime timer that indicates how long the current 'show' has been active.
  22. /// Update() increments it and advances the show when it reaches sequenceDurationSeconds, then resets it to 0.
  23. /// </summary>
  24. private float sequencetimer = 0;
  25. /// <summary>
  26. /// Index of the sequence. Used to advance through the SequenceObjects list.
  27. /// </summary>
  28. private int sequenceindex = 0;
  29. // Use this for initialization
  30. void OnEnable ()
  31. {
  32. //set the first show to active and the rest to not active.
  33. SwitchToSequence(0);
  34. }
  35. // Update is called once per frame
  36. void Update ()
  37. {
  38. sequencetimer += Time.deltaTime;
  39. if(sequencetimer >= sequenceDurationSeconds)
  40. {
  41. sequenceindex++;
  42. if(sequenceindex >= sequenceObjects.Count)
  43. {
  44. sequenceindex = 0;
  45. }
  46. SwitchToSequence(sequenceindex);
  47. sequencetimer = sequencetimer % sequenceDurationSeconds;
  48. }
  49. }
  50. private void SwitchToSequence(int index)
  51. {
  52. //Make sure that's a valid index
  53. if (sequenceObjects.Count <= index || sequenceObjects[index] == null) return;
  54. for(int i = 0; i < sequenceObjects.Count; i++)
  55. {
  56. sequenceObjects[i].SetActive(i == index);
  57. }
  58. }
  59. }