| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- using AppUI.Manager.View;
- using DG.Tweening;
- using TMPro;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.UI;
- namespace AppUI.View.Component
- {
- /// <summary>
- /// 运行时挂在 <c>Resources/Manager/ModuleView</c> 实例上,绑定层级:
- /// ModuleView / BG、Frame / GameObject(Image + Text TMP)、BtnAgree;拒绝按钮节点若存在则仅隐藏不参与逻辑。
- /// 弹出与关闭参考 <see cref="PopupMgr"/> 的 Tip:Frame 缩放进入,同意后缩放消失再 <see cref="Object.Destroy"/>。
- /// </summary>
- public class ModuleView : MonoBehaviour
- {
- const string PathFrame = "Frame";
- const string PathContentRow = "Frame/GameObject";
- const string PathContentImage = "Frame/GameObject/Image";
- const string PathContentTextTmp = "Frame/GameObject/Text (TMP)";
- const string PathContentTextLegacy = "Frame/Text";
- const string PathBtnReject = "Frame/BtnReject";
- const string PathBtnAgree = "Frame/BtnAgree";
- const string PathButtonLabelTmp = "Text (TMP)";
- const float PopInDuration = 0.25f;
- const float PopOutDuration = 0.3f;
- public string text;
- public string textKey;
- public object[] textFormatArgs = { };
- /// <summary>左侧图标;为 null 则保持预制体默认且不隐藏。</summary>
- public Sprite contentIcon;
- public UnityAction onAgree;
- public string onAgreeTextKey;
- /// <summary>为 true(默认)时,点击同意会先播放收起动画再销毁自身。</summary>
- public bool willDestroyAfterClick = true;
- bool _dismissing;
- Tween _popTween;
- public static ModuleView Show()
- {
- ViewConfig viewConfig = ViewManager.GetConfig(UIPrefabType.ModuleView);
- GameObject go = Instantiate(Resources.Load<GameObject>(viewConfig.path));
- return go.AddComponent<ModuleView>();
- }
- void OnDestroy()
- {
- _popTween?.Kill();
- transform.DOKill(true);
- }
- void Start()
- {
- HideRejectIfPresent();
- BindContentText();
- ApplyContentIcon();
- BindAgreeButton();
- TrySetButtonLabelKey(PathBtnAgree, onAgreeTextKey);
- var frameRt = transform.Find(PathFrame) as RectTransform;
- if (frameRt != null)
- LayoutRebuilder.ForceRebuildLayoutImmediate(frameRt);
- PlayPopIn();
- }
- void HideRejectIfPresent()
- {
- Transform reject = transform.Find(PathBtnReject);
- if (reject != null)
- reject.gameObject.SetActive(false);
- }
- void PlayPopIn()
- {
- var frame = transform.Find(PathFrame) as RectTransform;
- if (frame == null)
- return;
- frame.DOKill();
- frame.localScale = Vector3.zero;
- _popTween = frame
- .DOScale(1f, PopInDuration)
- .SetEase(Ease.OutBack)
- .SetUpdate(true);
- }
- /// <summary>与点击同意后的收起一致:缩放 Frame 后销毁(不再次触发 <see cref="onAgree"/>)。</summary>
- public void CloseWithTween()
- {
- if (_dismissing)
- return;
- if (!willDestroyAfterClick)
- return;
- _dismissing = true;
- PlayPopOutAndDestroy();
- }
- void BindAgreeButton()
- {
- Transform btnTr = transform.Find(PathBtnAgree);
- if (btnTr == null)
- return;
- var button = btnTr.GetComponent<Button>();
- if (button == null)
- return;
- button.onClick.AddListener(OnAgreeClicked);
- }
- void OnAgreeClicked()
- {
- if (_dismissing)
- return;
- onAgree?.Invoke();
- if (!willDestroyAfterClick)
- return;
- _dismissing = true;
- PlayPopOutAndDestroy();
- }
- void PlayPopOutAndDestroy()
- {
- Transform btnTr = transform.Find(PathBtnAgree);
- if (btnTr != null)
- {
- var button = btnTr.GetComponent<Button>();
- if (button != null)
- button.interactable = false;
- }
- var frame = transform.Find(PathFrame) as RectTransform;
- if (frame == null)
- {
- Destroy(gameObject);
- return;
- }
- _popTween?.Kill();
- frame.DOKill();
- _popTween = frame
- .DOScale(0f, PopOutDuration)
- .SetEase(Ease.InBack)
- .SetUpdate(true)
- .OnComplete(() => Destroy(gameObject));
- }
- void BindContentText()
- {
- Transform tmpTr = transform.Find(PathContentTextTmp);
- if (tmpTr != null)
- {
- if (!string.IsNullOrEmpty(textKey))
- {
- var t2 = tmpTr.gameObject.GetComponent<TextAutoLanguage2>();
- if (t2 == null)
- t2 = tmpTr.gameObject.AddComponent<TextAutoLanguage2>();
- t2.textFormatArgs = textFormatArgs;
- t2.SetTextKey(textKey);
- }
- else
- {
- var tmp = tmpTr.GetComponent<TMP_Text>();
- if (tmp != null)
- tmp.text = text ?? string.Empty;
- }
- return;
- }
- Transform legacyTr = transform.Find(PathContentTextLegacy);
- if (legacyTr == null)
- return;
- if (!string.IsNullOrEmpty(textKey))
- {
- var t2 = legacyTr.gameObject.GetComponent<TextAutoLanguage2>();
- if (t2 == null)
- t2 = legacyTr.gameObject.AddComponent<TextAutoLanguage2>();
- t2.textFormatArgs = textFormatArgs;
- t2.SetTextKey(textKey);
- }
- else
- {
- var uguiText = legacyTr.GetComponent<Text>();
- if (uguiText != null)
- uguiText.text = text ?? string.Empty;
- else
- {
- var tmp = legacyTr.GetComponent<TMP_Text>();
- if (tmp != null)
- tmp.text = text ?? string.Empty;
- }
- }
- }
- void ApplyContentIcon()
- {
- if (contentIcon == null)
- return;
- Transform imgTr = transform.Find(PathContentImage);
- if (imgTr == null)
- return;
- var image = imgTr.GetComponent<Image>();
- if (image != null)
- {
- image.sprite = contentIcon;
- image.enabled = true;
- }
- }
- static void TrySetButtonLabelKey(Transform buttonRoot, string key)
- {
- if (buttonRoot == null || string.IsNullOrEmpty(key))
- return;
- Transform labelTr = buttonRoot.Find(PathButtonLabelTmp);
- GameObject labelGo = labelTr != null
- ? labelTr.gameObject
- : buttonRoot.GetComponentInChildren<TMP_Text>(true)?.gameObject;
- if (labelGo == null)
- return;
- var t2 = labelGo.GetComponent<TextAutoLanguage2>();
- if (t2 == null)
- t2 = labelGo.AddComponent<TextAutoLanguage2>();
- t2.SetTextKey(key);
- }
- void TrySetButtonLabelKey(string buttonPath, string key)
- {
- TrySetButtonLabelKey(transform.Find(buttonPath), key);
- }
- /// <summary>仅隐藏 <c>Frame/GameObject/Image</c>,不改文案。</summary>
- public void SetContentImageVisible(bool visible)
- {
- Transform imgTr = transform.Find(PathContentImage);
- if (imgTr == null)
- return;
- var image = imgTr.GetComponent<Image>();
- if (image != null)
- image.enabled = visible;
- }
- /// <summary>控制左侧图文行整行显隐。</summary>
- public void SetContentRowVisible(bool visible)
- {
- Transform row = transform.Find(PathContentRow);
- if (row != null)
- row.gameObject.SetActive(visible);
- }
- }
- }
|