AnimatedLight.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. // =================================
  7. // Define namespace.
  8. // =================================
  9. namespace MirzaBeig
  10. {
  11. namespace ParticleSystems
  12. {
  13. // =================================
  14. // Classes.
  15. // =================================
  16. [RequireComponent(typeof(Light))]
  17. public class AnimatedLight : MonoBehaviour
  18. {
  19. // =================================
  20. // Nested classes and structures.
  21. // =================================
  22. // ...
  23. // =================================
  24. // Variables.
  25. // =================================
  26. // ...
  27. new Light light;
  28. public float time { get; set; }
  29. public float duration = 1.0f;
  30. bool evaluating = true;
  31. public Gradient colourOverLifetime;
  32. public AnimationCurve intensityOverLifetime = new AnimationCurve(
  33. new Keyframe(0.0f, 0.0f),
  34. new Keyframe(0.5f, 1.0f),
  35. new Keyframe(1.0f, 0.0f));
  36. // ...
  37. public bool loop = true;
  38. public bool autoDestruct = false;
  39. // ...
  40. Color startColour;
  41. float startIntensity;
  42. // =================================
  43. // Functions.
  44. // =================================
  45. // ...
  46. void Awake()
  47. {
  48. light = GetComponent<Light>();
  49. }
  50. // ...
  51. void Start()
  52. {
  53. startColour = light.color;
  54. startIntensity = light.intensity;
  55. light.color = startColour * colourOverLifetime.Evaluate(0.0f);
  56. light.intensity = startIntensity * intensityOverLifetime.Evaluate(0.0f);
  57. }
  58. // ...
  59. void OnEnable()
  60. {
  61. }
  62. void OnDisable()
  63. {
  64. // Reset for next OnEnable if required.
  65. light.color = startColour;
  66. light.intensity = startIntensity;
  67. time = 0.0f;
  68. evaluating = true;
  69. light.color = startColour * colourOverLifetime.Evaluate(0.0f);
  70. light.intensity = startIntensity * intensityOverLifetime.Evaluate(0.0f);
  71. }
  72. // ...
  73. void Update()
  74. {
  75. if (evaluating)
  76. {
  77. if (time < duration)
  78. {
  79. time += Time.deltaTime;
  80. if (time > duration)
  81. {
  82. if (autoDestruct)
  83. {
  84. Destroy(gameObject);
  85. }
  86. else if (loop)
  87. {
  88. time = 0.0f;
  89. }
  90. else
  91. {
  92. time = duration;
  93. evaluating = false;
  94. }
  95. }
  96. }
  97. // ...
  98. if (time <= duration)
  99. {
  100. float normalizedTime = time / duration;
  101. light.color = startColour * colourOverLifetime.Evaluate(normalizedTime);
  102. light.intensity = startIntensity * intensityOverLifetime.Evaluate(normalizedTime);
  103. }
  104. }
  105. }
  106. // =================================
  107. // End functions.
  108. // =================================
  109. }
  110. // =================================
  111. // End namespace.
  112. // =================================
  113. }
  114. }
  115. // =================================
  116. // --END-- //
  117. // =================================