FadeOutAccordingToScale.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// This component is used by the loading background sperms. They are fadeout according to the scaling
  5. /// to give the illusion of depth. The fading out is accomplished by darkening the color actually.
  6. /// </summary>
  7. public class FadeOutAccordingToScale : MonoBehaviour
  8. {
  9. public float minAlpha = 0.1f;
  10. public float maxAlpha = 0.5f;
  11. bool _isSetup;
  12. float _percentageBlack;
  13. void OnEnable()
  14. {
  15. //have to delay setup on update due to pooling
  16. _isSetup = false;
  17. NotificationCenter.AddListener(OnFadeOutUpdate, NotificationType.LoadingFadeOutUpdate);
  18. }
  19. void OnDisable()
  20. {
  21. NotificationCenter.RemoveListener(OnFadeOutUpdate, NotificationType.LoadingFadeOutUpdate);
  22. }
  23. void Update()
  24. {
  25. if (!_isSetup)
  26. {
  27. _percentageBlack = minAlpha + (maxAlpha - minAlpha) * transform.parent.localScale.y;
  28. SetBlackness(_percentageBlack);
  29. _isSetup = true;
  30. }
  31. }
  32. void SetBlackness(float p)
  33. {
  34. GetComponent<Renderer>().material.SetColor("_TintColor", new Color(p, p, p, 0.5f));
  35. }
  36. void OnFadeOutUpdate(Notification note)
  37. {
  38. float fadeOutPercentage = (float)note.data;
  39. SetBlackness(_percentageBlack * fadeOutPercentage);
  40. }
  41. }