鼠标移入、移出

IPointerEnterHandler

该接口实现方法如下:

public void OnPointerEnter(PointerEventData eventData)
{
    //当鼠标光标移入该对象时触发
}

IPointerExitHandler

该接口实现方法如下:

public void OnPointerExit(PointerEventData eventData)
{
    //当鼠标光标移出该对象时触发
}

案例说明

利用 IPointerEnterHandler 和 IPointerExitHandler 接口,实现鼠标移入移出图片时,显示或隐藏图片信息的效果,即 ToolTip 效果。

具体实施

(1) 创建 2D 工程; (2) 创建两个 Image,分别命名为 Image1 和 Image2; (3) 创建 Text,重命名为 TextView;

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

public class UIInfo : MonoBehaviour,
    IPointerEnterHandler,IPointerExitHandler{

    public Text TextContent;

    void Start()
    {
        HideText();
    }

    public void ShowText()
    {
        TextContent.text = "This is " + name + "'s infomation!";
    }

    public void HideText()
    {
        TextContent.text = "None infomation.";
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        ShowText();
        Debug.Log("OnPointerEnter call by " + name);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        HideText();
        Debug.Log("OnPointerExit call by" + name);
    }
}

5) 将此脚本指定给 Image1 和 Image2,并将 TextView 指定给二者 TextContent 字段;

results matching ""

    No results matching ""