NoticeEffect.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace SC.XR.Unity
  7. {
  8. public class NoticeEffect : MonoBehaviour
  9. {
  10. Coroutine effect;
  11. [HideInInspector]
  12. public float effectDurtion = 3;
  13. [HideInInspector]
  14. public bool isEnable = false;
  15. private float tempTime = 0;
  16. private List<Image> ImageList;
  17. private List<TextMeshProUGUI> TextMeshProUGUIList;
  18. private void OnEnable()
  19. {
  20. Init();
  21. isEnable = true;
  22. effect = StartCoroutine(EffectFunction(effectDurtion));
  23. }
  24. private void OnDisable()
  25. {
  26. if (effect != null)
  27. {
  28. isEnable = false;
  29. foreach (var item in ImageList)
  30. {
  31. item.color = new Color(item.color.r, item.color.g, item.color.b, 0);
  32. }
  33. foreach (var item in TextMeshProUGUIList)
  34. {
  35. item.color = new Color(item.color.r, item.color.g, item.color.b, 0);
  36. }
  37. StopCoroutine(effect);
  38. ImageList = null;
  39. TextMeshProUGUIList = null;
  40. }
  41. }
  42. private void Init()
  43. {
  44. ImageList = new List<Image>(GetComponentsInChildren<Image>());
  45. TextMeshProUGUIList = new List<TextMeshProUGUI>(GetComponentsInChildren<TextMeshProUGUI>());
  46. foreach (var item in ImageList)
  47. {
  48. item.color = new Color(item.color.r, item.color.g, item.color.b, 0);
  49. }
  50. foreach (var item in TextMeshProUGUIList)
  51. {
  52. item.color = new Color(item.color.r, item.color.g, item.color.b, 0);
  53. }
  54. }
  55. IEnumerator EffectFunction(float time)
  56. {
  57. yield return new WaitForSeconds(0.2f);
  58. AudioSystem.getInstance.PlayAudioOneShot(gameObject, SCAudiosConfig.AudioType.Notification);
  59. tempTime = 0;
  60. while ((tempTime += Time.deltaTime * 1.5f) < time)
  61. {
  62. float flag = Mathf.Clamp01((tempTime) / time);
  63. foreach (var item in ImageList)
  64. {
  65. item.color = new Color(item.color.r, item.color.g, item.color.b, flag);
  66. }
  67. foreach (var item in TextMeshProUGUIList)
  68. {
  69. item.color = new Color(item.color.r, item.color.g, item.color.b, flag);
  70. }
  71. yield return null;
  72. }
  73. yield return new WaitForSeconds(2);
  74. tempTime = 0;
  75. while ((tempTime += Time.deltaTime) < time)
  76. {
  77. float flag = Mathf.Clamp01((time - tempTime) / time);
  78. foreach (var item in ImageList)
  79. {
  80. item.color = new Color(item.color.r, item.color.g, item.color.b, flag);
  81. }
  82. foreach (var item in TextMeshProUGUIList)
  83. {
  84. item.color = new Color(item.color.r, item.color.g, item.color.b, flag);
  85. }
  86. yield return null;
  87. }
  88. this.enabled = false;
  89. }
  90. }
  91. }