小功能丨Unity Button按钮实现鼠标移入移出触发相应事件

 

挂载在Button按钮上,继承IPointerEnterHandler和IPointerExitHandler接口,,实现移入和移出功能

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

public class ZhiShiKuUIChange : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    private Image image;
    public Sprite[] sprites;

    void Start()
    {
        image = GetComponent();
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        image.sprite = sprites[0];
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        image.sprite = sprites[1];
    }
}

 

接口复习:

接口定义、显示接口和隐式接口

你可能感兴趣的:(#,小功能)