当前位置:   article > 正文

unity鼠标悬停在Button上显示介绍_unity检测鼠标在button上

unity检测鼠标在button上

鼠标停在按钮上,会显示详情介绍提示框,代码如下;

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. /// <summary>
  7. /// 鼠标悬停于按钮上显示介绍的基类
  8. /// </summary>
  9. public class BtnHoverBase : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  10. {
  11. /// <summary>
  12. /// 鼠标移动到按钮上显示的东西
  13. /// </summary>
  14. public GameObject tipsBg { get; set; }
  15. public Text textTips { get; set; }
  16. private void Awake()
  17. {
  18. tipsBg = transform.GetChild(0).gameObject;
  19. textTips = tipsBg.transform.GetChild(0).GetComponent<Text>();
  20. }
  21. public void SetValue(string text)
  22. {
  23. textTips.text = text;
  24. }
  25. public void OnPointerEnter(PointerEventData eventData)
  26. {
  27. tipsBg.SetActive(true);
  28. }
  29. public void OnPointerExit(PointerEventData eventData)
  30. {
  31. tipsBg.SetActive(false);
  32. }
  33. }

这是基类,可以写一个子类代码继承这个基类,设置具体信息,把基类中的Awake()方法删掉,在子类中编写,代码拖给需要显示信息的按钮上,代码如下:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. /// <summary>
  7. /// 鼠标移动到按钮上显示介绍
  8. /// </summary>
  9. public class TestButton : BtnHoverBase
  10. {
  11. private void Awake()
  12. {
  13. tipsBg = transform.GetChild(0).gameObject;
  14. textTips = tipsBg.transform.GetChild(0).GetComponent<Text>();
  15. textTips.text = "仙剑奇侠传";
  16. }
  17. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/542857
推荐阅读
相关标签
  

闽ICP备14008679号