AnimatedAlpha.cs 851 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. /// <summary>
  8. /// Makes it possible to animate alpha of the widget or a panel.
  9. /// </summary>
  10. [ExecuteInEditMode]
  11. public class AnimatedAlpha : MonoBehaviour
  12. {
  13. #if !UNITY_3_5
  14. [Range(0f, 1f)]
  15. #endif
  16. public float alpha = 1f;
  17. CanvasGroup mPanel;
  18. Image mImage;
  19. void OnEnable ()
  20. {
  21. mImage = GetComponent<Image>();
  22. mPanel = GetComponent<CanvasGroup>();
  23. LateUpdate();
  24. }
  25. void LateUpdate ()
  26. {
  27. if (mPanel != null)
  28. mPanel.alpha = alpha;
  29. else if (mImage != null)
  30. {
  31. Color col = mImage.color;
  32. col.a = alpha;
  33. mImage.color = col;
  34. }
  35. }
  36. }