RandomDirLightManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Every X seconds, changes the lights in all child objects to a random (smae) color and rotates them to new, random directions
  6. /// if it contains a RandomDirectionMover object.
  7. /// Used for the ZED Dark Room example scene.
  8. /// </summary>
  9. public class RandomDirLightManager : MonoBehaviour
  10. {
  11. /// <summary>
  12. /// Time between each pulse. Should match the beats-per-second of the music.
  13. /// </summary>
  14. [Tooltip("Time between each pulse. Should match the beats-per-second of the music. ")]
  15. public float secondsBetweenPulses = 0.5f;
  16. /// <summary>
  17. /// How long to wait after start to start pulsing. Use to sync to music.
  18. /// </summary>
  19. [Tooltip("How long to wait after start to start pulsing. Use to sync to music.")]
  20. public float startDelay = 0.1f;
  21. /// <summary>
  22. /// Pool of colors that the lights could become on each pulse.
  23. /// </summary>
  24. [Tooltip("Pool of colors that the lights could become on each pulse. ")]
  25. public List<Color> ColorOptions = new List<Color>();
  26. /// <summary>
  27. /// Length of time in seconds since the last pulse.
  28. /// Gets incremented by Time.deltaTime in Update(). When it hits secondsBetweenPulses, it triggers a pulse and resets.
  29. /// </summary>
  30. private float pulseTimer;
  31. /// <summary>
  32. /// List of all Light components in this object and its children. Filled in Start().
  33. /// </summary>
  34. private List<Light> lightList;
  35. /// <summary>
  36. /// List of all RandomDirectionMover components in this object and its children. Filled in Start.
  37. /// </summary>
  38. private List<RandomDirectionMover> randPointerList;
  39. /// <summary>
  40. /// Stores the index of the color we used during the last pulse.
  41. /// Used to prevent the same color appearing twice in a row.
  42. /// </summary>
  43. private int lastcolorindex = -1;
  44. // Use this for initialization
  45. void Start ()
  46. {
  47. lightList = new List<Light>(GetComponentsInChildren<Light>());
  48. randPointerList = new List<RandomDirectionMover>(GetComponentsInChildren<RandomDirectionMover>());
  49. pulseTimer = -startDelay;
  50. }
  51. // Update is called once per frame
  52. void Update ()
  53. {
  54. pulseTimer += Time.deltaTime;
  55. if(pulseTimer > secondsBetweenPulses)
  56. {
  57. PulseLights();
  58. pulseTimer = pulseTimer % secondsBetweenPulses;
  59. }
  60. }
  61. /// <summary>
  62. /// Picks a random color, sets all Lights to that color, and tells all RandomDirectionMovers to move randomly.
  63. /// </summary>
  64. private void PulseLights()
  65. {
  66. if (ColorOptions.Count > 0) //We have at least one color indexed, so we can pick a color from the list.
  67. {
  68. int newcolorindex;
  69. if (ColorOptions.Count > 1)
  70. {
  71. newcolorindex = Random.Range(0, ColorOptions.Count - 1);
  72. while (newcolorindex == lastcolorindex) //Don't pick the same color twice in a row if we have more than one color available.
  73. {
  74. newcolorindex = Random.Range(0, ColorOptions.Count - 1);
  75. }
  76. lastcolorindex = newcolorindex;
  77. }
  78. else newcolorindex = 0;
  79. foreach(Light light in lightList)
  80. {
  81. light.color = ColorOptions[newcolorindex];
  82. }
  83. }
  84. foreach(RandomDirectionMover pointer in randPointerList)
  85. {
  86. StartCoroutine(pointer.NewDirection()); //Causes it to move to a random direction.
  87. }
  88. }
  89. }