| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- using AppUI.Manager.View;
- using DG.Tweening;
- using TMPro;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.UI;
- namespace AppUI.View.Component
- {
- /// <summary>
- /// 横版 ModuleView:节点在预制体 Inspector 中拖拽绑定。
- /// 弹出与关闭参考 <see cref="PopupMgr"/> 的 Tip:Frame 缩放进入,按钮点击后缩放消失再销毁。
- /// </summary>
- public class ModuleViewHorizontal : MonoBehaviour
- {
- const float PopInDuration = 0.25f;
- const float PopOutDuration = 0.3f;
- [Header("布局节点")]
- [SerializeField]
- RectTransform frame;
- [Header("正文")]
- [SerializeField]
- TMP_Text contentTextTmp;
- [SerializeField]
- Text contentTextLegacy;
- [Header("按钮")]
- [SerializeField]
- Button btnReject;
- [SerializeField]
- TMP_Text btnRejectLabel;
- [SerializeField]
- Button btnAgree;
- [SerializeField]
- TMP_Text btnAgreeLabel;
- public string text;
- public string textKey;
- public object[] textFormatArgs = { };
- public UnityAction onAgree;
- public string onAgreeTextKey;
- public UnityAction onReject;
- public string onRejectTextKey;
- /// <summary>为 true(默认)时,点击按钮会先播放收起动画再销毁自身。</summary>
- public bool willDestroyAfterClick = true;
- bool _dismissing;
- Tween _popTween;
-
- public static ModuleViewHorizontal Show()
- {
- ViewConfig viewConfig = ViewManager.GetConfig(UIPrefabType.ModuleViewHorizontal);
- GameObject o = Instantiate(Resources.Load<GameObject>(viewConfig.path));
- var view = o.GetComponent<ModuleViewHorizontal>();
- if (view == null)
- view = o.AddComponent<ModuleViewHorizontal>();
- return view;
- }
- void OnDestroy()
- {
- _popTween?.Kill();
- transform.DOKill(true);
- }
- void Start()
- {
- SetupRejectButton();
- BindContentText();
- BindAgreeButton();
- TrySetButtonLabelKey(btnAgreeLabel, onAgreeTextKey);
- TrySetButtonLabelKey(btnRejectLabel, onRejectTextKey);
- if (frame != null)
- LayoutRebuilder.ForceRebuildLayoutImmediate(frame);
- PlayPopIn();
- }
- /// <summary>未设置拒绝回调且无拒绝文案 key 时隐藏拒绝按钮。</summary>
- void SetupRejectButton()
- {
- if (btnReject == null)
- return;
- bool showReject = onReject != null || !string.IsNullOrEmpty(onRejectTextKey);
- btnReject.gameObject.SetActive(showReject);
- if (!showReject)
- return;
- btnReject.onClick.AddListener(OnRejectClicked);
- }
- void PlayPopIn()
- {
- if (frame == null)
- return;
- frame.DOKill();
- frame.localScale = Vector3.zero;
- _popTween = frame
- .DOScale(1f, PopInDuration)
- .SetEase(Ease.OutBack)
- .SetUpdate(true);
- }
- /// <summary>缩放 Frame 后销毁(不再次触发按钮回调)。</summary>
- public void CloseWithTween()
- {
- if (_dismissing)
- return;
- if (!willDestroyAfterClick)
- return;
- _dismissing = true;
- PlayPopOutAndDestroy();
- }
- void BindAgreeButton()
- {
- if (btnAgree == null)
- return;
- btnAgree.onClick.AddListener(OnAgreeClicked);
- }
- void OnAgreeClicked()
- {
- if (_dismissing)
- return;
- onAgree?.Invoke();
- if (!willDestroyAfterClick)
- return;
- _dismissing = true;
- PlayPopOutAndDestroy();
- }
- void OnRejectClicked()
- {
- if (_dismissing)
- return;
- onReject?.Invoke();
- if (!willDestroyAfterClick)
- return;
- _dismissing = true;
- PlayPopOutAndDestroy();
- }
- void PlayPopOutAndDestroy()
- {
- SetButtonsInteractable(false);
- if (frame == null)
- {
- Destroy(gameObject);
- return;
- }
- _popTween?.Kill();
- frame.DOKill();
- _popTween = frame
- .DOScale(0f, PopOutDuration)
- .SetEase(Ease.InBack)
- .SetUpdate(true)
- .OnComplete(() => Destroy(gameObject));
- }
- void SetButtonsInteractable(bool interactable)
- {
- if (btnAgree != null)
- btnAgree.interactable = interactable;
- if (btnReject != null)
- btnReject.interactable = interactable;
- }
- void BindContentText()
- {
- if (contentTextTmp != null)
- {
- if (!string.IsNullOrEmpty(textKey))
- {
- var t2 = contentTextTmp.GetComponent<AppUITextAutoLanguage>();
- if (t2 == null)
- t2 = contentTextTmp.gameObject.AddComponent<AppUITextAutoLanguage>();
- t2.textFormatArgs = textFormatArgs;
- t2.SetTextKey(textKey);
- }
- else
- {
- contentTextTmp.text = text ?? string.Empty;
- }
- return;
- }
- if (contentTextLegacy == null)
- return;
- if (!string.IsNullOrEmpty(textKey))
- {
- var t2 = contentTextLegacy.GetComponent<AppUITextAutoLanguage>();
- if (t2 == null)
- t2 = contentTextLegacy.gameObject.AddComponent<AppUITextAutoLanguage>();
- t2.textFormatArgs = textFormatArgs;
- t2.SetTextKey(textKey);
- }
- else
- {
- contentTextLegacy.text = text ?? string.Empty;
- }
- }
- static void TrySetButtonLabelKey(TMP_Text label, string key)
- {
- if (label == null || string.IsNullOrEmpty(key))
- return;
- var t2 = label.GetComponent<AppUITextAutoLanguage>();
- if (t2 == null)
- t2 = label.gameObject.AddComponent<AppUITextAutoLanguage>();
- t2.SetTextKey(key);
- }
- }
- }
|