| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using DG.Tweening;
- public class NewUserGuider : MonoBehaviour
- {
- [SerializeField] public HollowOutMask hollowOutMask;
- [SerializeField] public RectTransform iconPointerHit;
- [SerializeField] public RectTransform iconPointer;
- [SerializeField] public RectTransform frameTip;
- [NonSerialized] public NewUserGuiderConfig config;
- [NonSerialized] public bool customPreparePass = true;
- [NonSerialized] public bool clickedWillPlayAudioBtn = true;
- private Sequence seq1;
- private Sequence seq2;
- void Start()
- {
- StartCoroutine(ExecuteConfig());
- }
- void OnDestroy()
- {
- if (seq1 != null && seq1.IsActive()) seq1.Kill();
- if (seq2 != null && seq2.IsActive()) seq2.Kill();
- action_OnDestroy?.Invoke();
- }
- void Update()
- {
- action_Update?.Invoke();
- }
- public Action action_Update;
- public Action action_OnDestroy;
- IEnumerator ExecuteConfig()
- {
- SetCanvasSortOrder(5);
- if (config.delayExecute) {
- //延迟3帧,避免因为目标ui还未初始完成导致获取到的ui信息(位置大小等)不准确
- int waitFrameCount = 0;
- do {
- waitFrameCount++;
- yield return null;
- } while (waitFrameCount < 3);
- }
-
- customPreparePass = true;
- while (config.onPrepare != null) {
- config.onPrepare?.Invoke(this);
- if (customPreparePass) break;
- else yield return null;
- }
- if (config.hitActive) {
- if (config.hitPosType == 1) {
- iconPointerHit.anchoredPosition = config.hitPos;
- } else {
- iconPointerHit.position = config.hitPos;
- }
- }
- iconPointerHit.gameObject.SetActive(config.hitActive);
- if (config.pointerActive) {
- iconPointer.rotation = iconPointer.rotation * Quaternion.AngleAxis(config.pointerRotZ, Vector3.forward);
- if (config.pointerPosType == 1) {
- iconPointer.anchoredPosition = config.pointerPos;
- } else {
- iconPointer.position = config.pointerPos;
- }
- }
- iconPointer.gameObject.SetActive(config.pointerActive);
- if (seq1 != null && seq1.IsActive()) seq1.Kill();
- if (seq2 != null && seq2.IsActive()) seq2.Kill();
- if (config.hitActive && config.pointerActive) {
- seq1 = DOTween.Sequence();
- Vector3 oldPos = iconPointer.position;
- seq1.AppendInterval(1);
- seq1.Append(iconPointer.DOMove(iconPointerHit.position, 1));
- seq1.Append(iconPointer.DOMove(oldPos, 0.5f));
- seq1.SetLoops(-1);
- seq1.SetUpdate(true);
- seq2 = DOTween.Sequence();
- Vector3 s = iconPointerHit.localScale;
- seq2.Append(iconPointerHit.DOScale(s * 0.8f, 0.5f));
- seq2.Append(iconPointerHit.DOScale(s, 0.5f));
- seq2.SetLoops(-1);
- seq2.SetUpdate(true);
- }
- switch (config.frameTipPivot) {
- case "lt":
- frameTip.pivot = new Vector2(0, 1);
- break;
- case "rt":
- frameTip.pivot = new Vector2(1, 1);
- break;
- case "lb":
- frameTip.pivot = new Vector2(0, 0);
- break;
- case "rb":
- frameTip.pivot = new Vector2(1, 0);
- break;
- case "ct":
- frameTip.pivot = new Vector2(0.5f, 0.5f);
- break;
- case "lc":
- frameTip.pivot = new Vector2(0, 0.5f);
- break;
- }
- if (config.frameTipPosType == 1) {
- frameTip.anchoredPosition = config.frameTipPos;
- } else {
- frameTip.position = config.frameTipPos;
- }
- if (config.frameTipText != null) {
- frameTip.GetComponentInChildren<Text>().text = config.frameTipText;
- }
- else if (config.frameTipTextKey != null) {
- frameTip.Find("Text").gameObject.AddComponent<TextAutoLanguage2>().SetTextKey(config.frameTipTextKey);
- }
- else {
- frameTip.Find("Text").gameObject.AddComponent<TextAutoLanguage2>().SetTextKey("new-user-guider_tip_" + config.key);
- }
-
- frameTip.gameObject.SetActive(true);
- GetMaskClickedEvent().AddListener(OnClick_ToNext);
- config.onStart?.Invoke(this);
- }
- public void OnClick_Skip()
- {
- AudioMgr.ins.PlayBtn();
- Destroy(gameObject);
- NewUserGuiderManager.ins.OnSkip();
- }
- public void OnClick_ToNext()
- {
- if (clickedWillPlayAudioBtn) AudioMgr.ins?.PlayBtn();
- Destroy(gameObject);
- NewUserGuiderManager.ins?.OnClickedDestroyed(config.key);
- }
- public Button.ButtonClickedEvent GetMaskClickedEvent()
- {
- return hollowOutMask.GetComponent<Button>().onClick;
- }
- NewUserGuiderButton _newUserGuiderButton;
- public NewUserGuiderButton GetNewUserGuiderButton() {
- if (_newUserGuiderButton == null) {
- _newUserGuiderButton = hollowOutMask.gameObject.AddComponent<NewUserGuiderButton>();
- }
- return _newUserGuiderButton;
- }
- public void SetIconPointerHitOpacity(float opacity)
- {
- Image img = iconPointerHit.GetComponent<Image>();
- Color c = img.color;
- c.a = opacity;
- img.color = c;
- }
- public void SetMaskOpacity(float opacity)
- {
- Color c = hollowOutMask.color;
- c.a = opacity;
- hollowOutMask.color = c;
- }
- public void SetCanvasSortOrder(int sortOrder) {
- transform.GetComponent<Canvas>().sortingOrder = sortOrder;
- }
- public void ActiveBtnSkip(bool value)
- {
- transform.Find("BtnSkip").gameObject.SetActive(value);
- }
- public void FixFrameTipWidth(float width)
- {
- frameTip.GetComponent<ContentSizeFitter>().horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
- Vector2 size = frameTip.sizeDelta;
- size.x = width;
- frameTip.sizeDelta = size;
- }
- }
|