CFX_LightIntensityFade.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using UnityEngine;
  2. using System.Collections;
  3. // Cartoon FX - (c) 2013, Jean Moreno
  4. // Decreases a light's intensity over time.
  5. [RequireComponent(typeof(Light))]
  6. public class CFX_LightIntensityFade : MonoBehaviour
  7. {
  8. // Duration of the effect.
  9. public float duration = 1.0f;
  10. // Delay of the effect.
  11. public float delay = 0.0f;
  12. /// Final intensity of the light.
  13. public float finalIntensity = 0.0f;
  14. // Base intensity, automatically taken from light parameters.
  15. private float baseIntensity;
  16. // If <c>true</c>, light will destructs itself on completion of the effect
  17. public bool autodestruct;
  18. private float p_lifetime = 0.0f;
  19. private float p_delay;
  20. void Start()
  21. {
  22. baseIntensity = GetComponent<Light>().intensity;
  23. }
  24. void OnEnable()
  25. {
  26. p_lifetime = 0.0f;
  27. p_delay = delay;
  28. if(delay > 0) GetComponent<Light>().enabled = false;
  29. }
  30. void Update ()
  31. {
  32. if(p_delay > 0)
  33. {
  34. p_delay -= Time.deltaTime;
  35. if(p_delay <= 0)
  36. {
  37. GetComponent<Light>().enabled = true;
  38. }
  39. return;
  40. }
  41. if(p_lifetime/duration < 1.0f)
  42. {
  43. GetComponent<Light>().intensity = Mathf.Lerp(baseIntensity, finalIntensity, p_lifetime/duration);
  44. p_lifetime += Time.deltaTime;
  45. }
  46. else
  47. {
  48. if(autodestruct)
  49. GameObject.Destroy(this.gameObject);
  50. }
  51. }
  52. }