[Unity][UGUI]Tooltip提示框

版权声明:本博客一本正经胡说八道,文章内容仅供参考。本文为博主原创文章,未经博主允许不得转载。该博客所用图片资源均作学习分享用途,仅供参考,请勿用于商业行为。传播者自负。 如果本博客所写文章侵犯到您的权益,请主动联系留言,我们将及时删除相关内容。 https://blog.csdn.net/BuladeMian/article/details/79963990




using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


public class Test_ItemTooltip : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler
{
    public GameObject go;

    public void OnPointerDown(PointerEventData eventData)
    {
    }
    /// <summary>
    /// 进入触发显示物品信息
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerEnter(PointerEventData eventData)
    {
        go.SetActive(true);
        go.GetComponent<RectTransform>().position = new Vector3(Input.mousePosition.x + (go.GetComponent<RectTransform>().sizeDelta.x / 2),
            Input.mousePosition.y - (go.GetComponent<RectTransform>().sizeDelta.y / 2)
            , 0);
    }
    /// <summary>
    /// 移除隐藏显示
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerExit(PointerEventData eventData)
    {
        go.SetActive(false);
    }
}




由于鼠标的 Input.mousePosition获取,在这个地方使用会出现 Tooltip不停闪烁的 BUG。

因此用 transform.position代替Input.mousePosition

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


public class Test_ItemTooltip : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler
{
    public GameObject go;

    public void OnPointerDown(PointerEventData eventData)
    {
    }
    /// <summary>
    /// 进入触发显示物品信息
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerEnter(PointerEventData eventData)
    {
        go.SetActive(true);
        go.GetComponent<RectTransform>().position = new Vector3(transform.position.x + 20 + (go.GetComponent<RectTransform>().sizeDelta.x / 2),
            transform.position.y - 20 - (go.GetComponent<RectTransform>().sizeDelta.y / 2)
            , 0);
    }
    /// <summary>
    /// 移除隐藏显示
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerExit(PointerEventData eventData)
    {
        go.SetActive(false);
    }
}


相关文章:

1. 

[Unity][UGUI]Tooltip提示框

2.[Unity][UGUI]获得鼠标对应屏幕的坐标,图片跟随鼠标移动

3. 

[Untiy][UGUI]Toggle切换单选框

4.[Unity][UGUI][UI布局]Tooltip事件闪烁BUG解决办法

5.


阅读更多

没有更多推荐了,返回首页