NewUserGuider.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. public Action action_OnDestroy;
  29. IEnumerator ExecuteConfig()
  30. {
  31. SetCanvasSortOrder(5);
  32. //延迟3帧,避免因为目标ui还未初始完成导致获取到的ui信息(位置大小等)不准确
  33. int waitFrameCount = 0;
  34. do {
  35. waitFrameCount++;
  36. yield return null;
  37. } while (waitFrameCount < 3);
  38. customPreparePass = true;
  39. while (config.onPrepare != null) {
  40. config.onPrepare?.Invoke(this);
  41. if (customPreparePass) break;
  42. else yield return null;
  43. }
  44. if (config.hitActive) {
  45. if (config.hitPosType == 1) {
  46. iconPointerHit.anchoredPosition = config.hitPos;
  47. } else {
  48. iconPointerHit.position = config.hitPos;
  49. }
  50. }
  51. iconPointerHit.gameObject.SetActive(config.hitActive);
  52. if (config.pointerActive) {
  53. iconPointer.rotation = iconPointer.rotation * Quaternion.AngleAxis(config.pointerRotZ, Vector3.forward);
  54. if (config.pointerPosType == 1) {
  55. iconPointer.anchoredPosition = config.pointerPos;
  56. } else {
  57. iconPointer.position = config.pointerPos;
  58. }
  59. }
  60. iconPointer.gameObject.SetActive(config.pointerActive);
  61. if (seq1 != null && seq1.IsActive()) seq1.Kill();
  62. if (seq2 != null && seq2.IsActive()) seq2.Kill();
  63. if (config.hitActive && config.pointerActive) {
  64. seq1 = DOTween.Sequence();
  65. Vector3 oldPos = iconPointer.position;
  66. seq1.AppendInterval(1);
  67. seq1.Append(iconPointer.DOMove(iconPointerHit.position, 1));
  68. seq1.Append(iconPointer.DOMove(oldPos, 0.5f));
  69. seq1.SetLoops(-1);
  70. seq1.SetUpdate(true);
  71. seq2 = DOTween.Sequence();
  72. Vector3 s = iconPointerHit.localScale;
  73. seq2.Append(iconPointerHit.DOScale(s * 0.8f, 0.5f));
  74. seq2.Append(iconPointerHit.DOScale(s, 0.5f));
  75. seq2.SetLoops(-1);
  76. seq2.SetUpdate(true);
  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 OnClick_Skip()
  117. {
  118. AudioMgr.ins.PlayBtn();
  119. Destroy(gameObject);
  120. NewUserGuiderManager.ins.OnSkip();
  121. }
  122. public void OnClick_ToNext()
  123. {
  124. if (clickedWillPlayAudioBtn) AudioMgr.ins?.PlayBtn();
  125. Destroy(gameObject);
  126. NewUserGuiderManager.ins?.OnClickedDestroyed(config.key);
  127. }
  128. public Button.ButtonClickedEvent GetMaskClickedEvent()
  129. {
  130. return hollowOutMask.GetComponent<Button>().onClick;
  131. }
  132. NewUserGuiderButton _newUserGuiderButton;
  133. public NewUserGuiderButton GetNewUserGuiderButton() {
  134. if (_newUserGuiderButton == null) {
  135. _newUserGuiderButton = hollowOutMask.gameObject.AddComponent<NewUserGuiderButton>();
  136. }
  137. return _newUserGuiderButton;
  138. }
  139. public void SetIconPointerHitOpacity(float opacity)
  140. {
  141. Image img = iconPointerHit.GetComponent<Image>();
  142. Color c = img.color;
  143. c.a = opacity;
  144. img.color = c;
  145. }
  146. public void SetCanvasSortOrder(int sortOrder) {
  147. transform.GetComponent<Canvas>().sortingOrder = sortOrder;
  148. }
  149. }