using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using TMPro; public class Tips : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler { public GameObject tip; private Text _uiText; private TMP_Text _tmpText; private string _key; void Awake() { CacheTextRef(); if (tip != null) tip.SetActive(false); } void CacheTextRef() { _uiText = null; _tmpText = null; _key = null; if (tip == null) return; var all = tip.GetComponentsInChildren(true); foreach (var t in all) { if (t.CompareTag("txt")) { _uiText = t.GetComponent(); _key = t.name; return; } if (t.CompareTag("txtpro")) { _tmpText = t.GetComponent(); _key = t.name; return; } } } public void Show() { if (tip == null) return; tip.SetActive(true); if ((_uiText == null && _tmpText == null) || string.IsNullOrEmpty(_key)) CacheTextRef(); if (!string.IsNullOrEmpty(_key)) { string tr = TRANS.This(_key); if (_tmpText != null) _tmpText.text = tr; else if (_uiText != null) _uiText.text = tr; } } public void Hide() { if (tip == null) return; tip.SetActive(false); } public void OnPointerEnter(PointerEventData eventData) { Show(); } public void OnPointerExit(PointerEventData eventData) { Hide(); } public void OnPointerClick(PointerEventData eventData) { Hide(); } }