TipShower.cs 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. public class TipShower : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
  7. {
  8. public RectTransform tips;
  9. public string data;
  10. public void OnPointerEnter(PointerEventData eventData)
  11. {
  12. currentShower = this;
  13. tips.anchoredPosition = this.GetComponent<RectTransform>().anchoredPosition;
  14. tips.gameObject.SetActive(true);
  15. tips.GetComponentInChildren<Text>().text = data;
  16. LayoutRebuilder.ForceRebuildLayoutImmediate(tips.GetChild(0).GetComponent<RectTransform>()); ; // 强制重新计算布局
  17. LayoutRebuilder.ForceRebuildLayoutImmediate(tips.GetComponent<RectTransform>()); // 强制重新计算布局
  18. }
  19. public static TipShower currentShower = null;
  20. public void OnPointerExit(PointerEventData eventData)
  21. {
  22. if (currentShower == this) {
  23. tips.gameObject.SetActive(false);
  24. currentShower = null;
  25. }
  26. }
  27. }