NoticeEffect.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace SC.XR.Unity.Module_DetectorSystem
  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<Text> TextList;
  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 TextList)
  34. {
  35. item.color = new Color(item.color.r, item.color.g, item.color.b, 0);
  36. }
  37. StopCoroutine(effect);
  38. ImageList = null;
  39. TextList = null;
  40. GetComponentInChildren<Canvas>(true).enabled=false;
  41. }
  42. }
  43. private void Init()
  44. {
  45. ImageList = new List<Image>(GetComponentsInChildren<Image>());
  46. TextList = new List<Text>(GetComponentsInChildren<Text>());
  47. foreach (var item in ImageList)
  48. {
  49. item.color = new Color(item.color.r, item.color.g, item.color.b, 0);
  50. }
  51. foreach (var item in TextList)
  52. {
  53. item.color = new Color(item.color.r, item.color.g, item.color.b, 0);
  54. }
  55. }
  56. IEnumerator EffectFunction(float time)
  57. {
  58. yield return new WaitForSeconds(0.2f);
  59. AudioSystem.getInstance.PlayAudioOneShot(gameObject, SCAudiosConfig.AudioType.Notification);
  60. tempTime = 0;
  61. while ((tempTime += Time.deltaTime * 1.5f) < time)
  62. {
  63. float flag = Mathf.Clamp01((tempTime) / time);
  64. foreach (var item in ImageList)
  65. {
  66. item.color = new Color(item.color.r, item.color.g, item.color.b, flag);
  67. }
  68. foreach (var item in TextList)
  69. {
  70. item.color = new Color(item.color.r, item.color.g, item.color.b, flag);
  71. }
  72. yield return null;
  73. }
  74. yield return new WaitForSeconds(2);
  75. tempTime = 0;
  76. while ((tempTime += Time.deltaTime) < time)
  77. {
  78. float flag = Mathf.Clamp01((time - tempTime) / time);
  79. foreach (var item in ImageList)
  80. {
  81. item.color = new Color(item.color.r, item.color.g, item.color.b, flag);
  82. }
  83. foreach (var item in TextList)
  84. {
  85. item.color = new Color(item.color.r, item.color.g, item.color.b, flag);
  86. }
  87. yield return null;
  88. }
  89. this.enabled = false;
  90. }
  91. }
  92. }