NewUserGuider.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using DG.Tweening;
  7. public class NewUserGuider : MonoBehaviour
  8. {
  9. [SerializeField] public HollowOutMask hollowOutMask;
  10. [SerializeField] public RectTransform iconPointerHit;
  11. [SerializeField] public RectTransform iconPointer;
  12. [SerializeField] public RectTransform frameTip;
  13. [NonSerialized] public NewUserGuiderConfig config;
  14. [NonSerialized] public bool customPreparePass = true;
  15. [NonSerialized] public bool clickedWillPlayAudioBtn = true;
  16. private Sequence seq1;
  17. private Sequence seq2;
  18. void Start()
  19. {
  20. StartCoroutine(ExecuteConfig());
  21. }
  22. void OnDestroy()
  23. {
  24. if (seq1 != null && seq1.IsActive()) seq1.Kill();
  25. if (seq2 != null && seq2.IsActive()) seq2.Kill();
  26. action_OnDestroy?.Invoke();
  27. }
  28. void Update()
  29. {
  30. action_Update?.Invoke();
  31. }
  32. public Action action_Update;
  33. public Action action_OnDestroy;
  34. IEnumerator ExecuteConfig()
  35. {
  36. SetCanvasSortOrder(5);
  37. //延迟3帧,避免因为目标ui还未初始完成导致获取到的ui信息(位置大小等)不准确
  38. int waitFrameCount = 0;
  39. do {
  40. waitFrameCount++;
  41. yield return null;
  42. } while (waitFrameCount < 3);
  43. customPreparePass = true;
  44. while (config.onPrepare != null) {
  45. config.onPrepare?.Invoke(this);
  46. if (customPreparePass) break;
  47. else yield return null;
  48. }
  49. if (config.hitActive) {
  50. if (config.hitPosType == 1) {
  51. iconPointerHit.anchoredPosition = config.hitPos;
  52. } else {
  53. iconPointerHit.position = config.hitPos;
  54. }
  55. }
  56. iconPointerHit.gameObject.SetActive(config.hitActive);
  57. if (config.pointerActive) {
  58. iconPointer.rotation = iconPointer.rotation * Quaternion.AngleAxis(config.pointerRotZ, Vector3.forward);
  59. if (config.pointerPosType == 1) {
  60. iconPointer.anchoredPosition = config.pointerPos;
  61. } else {
  62. iconPointer.position = config.pointerPos;
  63. }
  64. }
  65. iconPointer.gameObject.SetActive(config.pointerActive);
  66. if (seq1 != null && seq1.IsActive()) seq1.Kill();
  67. if (seq2 != null && seq2.IsActive()) seq2.Kill();
  68. if (config.hitActive && config.pointerActive) {
  69. seq1 = DOTween.Sequence();
  70. Vector3 oldPos = iconPointer.position;
  71. seq1.AppendInterval(1);
  72. seq1.Append(iconPointer.DOMove(iconPointerHit.position, 1));
  73. seq1.Append(iconPointer.DOMove(oldPos, 0.5f));
  74. seq1.SetLoops(-1);
  75. seq1.SetUpdate(true);
  76. AnimateIconPointerHit();
  77. }
  78. switch (config.frameTipPivot) {
  79. case "lt":
  80. frameTip.pivot = new Vector2(0, 1);
  81. break;
  82. case "rt":
  83. frameTip.pivot = new Vector2(1, 1);
  84. break;
  85. case "lb":
  86. frameTip.pivot = new Vector2(0, 0);
  87. break;
  88. case "rb":
  89. frameTip.pivot = new Vector2(1, 0);
  90. break;
  91. case "ct":
  92. frameTip.pivot = new Vector2(0.5f, 0.5f);
  93. break;
  94. case "lc":
  95. frameTip.pivot = new Vector2(0, 0.5f);
  96. break;
  97. }
  98. if (config.frameTipPosType == 1) {
  99. frameTip.anchoredPosition = config.frameTipPos;
  100. } else {
  101. frameTip.position = config.frameTipPos;
  102. }
  103. if (config.frameTipText != null) {
  104. frameTip.GetComponentInChildren<Text>().text = config.frameTipText;
  105. }
  106. else if (config.frameTipTextKey != null) {
  107. frameTip.Find("Text").gameObject.AddComponent<TextAutoLanguage2>().SetTextKey(config.frameTipTextKey);
  108. }
  109. else {
  110. frameTip.Find("Text").gameObject.AddComponent<TextAutoLanguage2>().SetTextKey("new-user-guider_tip_" + config.key);
  111. }
  112. frameTip.gameObject.SetActive(true);
  113. GetMaskClickedEvent().AddListener(OnClick_ToNext);
  114. config.onStart?.Invoke(this);
  115. }
  116. public void AnimateIconPointerHit()
  117. {
  118. if (seq2 != null && seq2.IsActive()) seq2.Kill();
  119. seq2 = DOTween.Sequence();
  120. Vector3 s = iconPointerHit.localScale;
  121. seq2.Append(iconPointerHit.DOScale(s * 0.8f, 0.5f));
  122. seq2.Append(iconPointerHit.DOScale(s, 0.5f));
  123. seq2.SetLoops(-1);
  124. seq2.SetUpdate(true);
  125. }
  126. public void OnClick_Skip()
  127. {
  128. AudioMgr.ins.PlayBtn();
  129. Destroy(gameObject);
  130. NewUserGuiderManager.ins.OnSkip();
  131. }
  132. public void OnClick_ToNext()
  133. {
  134. if (clickedWillPlayAudioBtn) AudioMgr.ins?.PlayBtn();
  135. Destroy(gameObject);
  136. NewUserGuiderManager.ins?.OnClickedDestroyed(config.key);
  137. }
  138. public Button.ButtonClickedEvent GetMaskClickedEvent()
  139. {
  140. return hollowOutMask.GetComponent<Button>().onClick;
  141. }
  142. NewUserGuiderButton _newUserGuiderButton;
  143. public NewUserGuiderButton GetNewUserGuiderButton() {
  144. if (_newUserGuiderButton == null) {
  145. _newUserGuiderButton = hollowOutMask.gameObject.AddComponent<NewUserGuiderButton>();
  146. }
  147. return _newUserGuiderButton;
  148. }
  149. public void SetIconPointerHitOpacity(float opacity)
  150. {
  151. Image img = iconPointerHit.GetComponent<Image>();
  152. Color c = img.color;
  153. c.a = opacity;
  154. img.color = c;
  155. }
  156. public void SetMaskOpacity(float opacity)
  157. {
  158. Color c = hollowOutMask.color;
  159. c.a = opacity;
  160. hollowOutMask.color = c;
  161. }
  162. public void SetCanvasSortOrder(int sortOrder) {
  163. transform.GetComponent<Canvas>().sortingOrder = sortOrder;
  164. }
  165. }