NewUserGuider.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. if (config.delayExecute) {
  38. //延迟3帧,避免因为目标ui还未初始完成导致获取到的ui信息(位置大小等)不准确
  39. int waitFrameCount = 0;
  40. do {
  41. waitFrameCount++;
  42. yield return null;
  43. } while (waitFrameCount < 3);
  44. }
  45. customPreparePass = true;
  46. while (config.onPrepare != null) {
  47. config.onPrepare?.Invoke(this);
  48. if (customPreparePass) break;
  49. else yield return null;
  50. }
  51. if (config.hitActive) {
  52. if (config.hitPosType == 1) {
  53. iconPointerHit.anchoredPosition = config.hitPos;
  54. } else {
  55. iconPointerHit.position = config.hitPos;
  56. }
  57. }
  58. iconPointerHit.gameObject.SetActive(config.hitActive);
  59. if (config.pointerActive) {
  60. iconPointer.rotation = iconPointer.rotation * Quaternion.AngleAxis(config.pointerRotZ, Vector3.forward);
  61. if (config.pointerPosType == 1) {
  62. iconPointer.anchoredPosition = config.pointerPos;
  63. } else {
  64. iconPointer.position = config.pointerPos;
  65. }
  66. }
  67. iconPointer.gameObject.SetActive(config.pointerActive);
  68. if (seq1 != null && seq1.IsActive()) seq1.Kill();
  69. if (seq2 != null && seq2.IsActive()) seq2.Kill();
  70. if (config.hitActive && config.pointerActive) {
  71. seq1 = DOTween.Sequence();
  72. Vector3 oldPos = iconPointer.position;
  73. seq1.AppendInterval(1);
  74. seq1.Append(iconPointer.DOMove(iconPointerHit.position, 1));
  75. seq1.Append(iconPointer.DOMove(oldPos, 0.5f));
  76. seq1.SetLoops(-1);
  77. seq1.SetUpdate(true);
  78. seq2 = DOTween.Sequence();
  79. Vector3 s = iconPointerHit.localScale;
  80. seq2.Append(iconPointerHit.DOScale(s * 0.8f, 0.5f));
  81. seq2.Append(iconPointerHit.DOScale(s, 0.5f));
  82. seq2.SetLoops(-1);
  83. seq2.SetUpdate(true);
  84. }
  85. switch (config.frameTipPivot) {
  86. case "lt":
  87. frameTip.pivot = new Vector2(0, 1);
  88. break;
  89. case "rt":
  90. frameTip.pivot = new Vector2(1, 1);
  91. break;
  92. case "lb":
  93. frameTip.pivot = new Vector2(0, 0);
  94. break;
  95. case "rb":
  96. frameTip.pivot = new Vector2(1, 0);
  97. break;
  98. case "ct":
  99. frameTip.pivot = new Vector2(0.5f, 0.5f);
  100. break;
  101. case "lc":
  102. frameTip.pivot = new Vector2(0, 0.5f);
  103. break;
  104. }
  105. if (config.frameTipPosType == 1) {
  106. frameTip.anchoredPosition = config.frameTipPos;
  107. } else {
  108. frameTip.position = config.frameTipPos;
  109. }
  110. if (config.frameTipText != null) {
  111. frameTip.GetComponentInChildren<Text>().text = config.frameTipText;
  112. }
  113. else if (config.frameTipTextKey != null) {
  114. frameTip.Find("Text").gameObject.AddComponent<TextAutoLanguage2>().SetTextKey(config.frameTipTextKey);
  115. }
  116. else {
  117. frameTip.Find("Text").gameObject.AddComponent<TextAutoLanguage2>().SetTextKey("new-user-guider_tip_" + config.key);
  118. }
  119. frameTip.gameObject.SetActive(true);
  120. GetMaskClickedEvent().AddListener(OnClick_ToNext);
  121. config.onStart?.Invoke(this);
  122. }
  123. public void OnClick_Skip()
  124. {
  125. AudioMgr.ins.PlayBtn();
  126. Destroy(gameObject);
  127. NewUserGuiderManager.ins.OnSkip();
  128. }
  129. public void OnClick_ToNext()
  130. {
  131. if (clickedWillPlayAudioBtn) AudioMgr.ins?.PlayBtn();
  132. Destroy(gameObject);
  133. NewUserGuiderManager.ins?.OnClickedDestroyed(config.key);
  134. }
  135. public Button.ButtonClickedEvent GetMaskClickedEvent()
  136. {
  137. return hollowOutMask.GetComponent<Button>().onClick;
  138. }
  139. NewUserGuiderButton _newUserGuiderButton;
  140. public NewUserGuiderButton GetNewUserGuiderButton() {
  141. if (_newUserGuiderButton == null) {
  142. _newUserGuiderButton = hollowOutMask.gameObject.AddComponent<NewUserGuiderButton>();
  143. }
  144. return _newUserGuiderButton;
  145. }
  146. public void SetIconPointerHitOpacity(float opacity)
  147. {
  148. Image img = iconPointerHit.GetComponent<Image>();
  149. Color c = img.color;
  150. c.a = opacity;
  151. img.color = c;
  152. }
  153. public void SetMaskOpacity(float opacity)
  154. {
  155. Color c = hollowOutMask.color;
  156. c.a = opacity;
  157. hollowOutMask.color = c;
  158. }
  159. public void SetCanvasSortOrder(int sortOrder) {
  160. transform.GetComponent<Canvas>().sortingOrder = sortOrder;
  161. }
  162. public void ActiveBtnSkip(bool value)
  163. {
  164. transform.Find("BtnSkip").gameObject.SetActive(value);
  165. }
  166. public void FixFrameTipWidth(float width)
  167. {
  168. frameTip.GetComponent<ContentSizeFitter>().horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
  169. Vector2 size = frameTip.sizeDelta;
  170. size.x = width;
  171. frameTip.sizeDelta = size;
  172. }
  173. }