NG_/Assets/Scripts/UI/全局/MouseEnter/EnterImageUIChange.cs
2025-01-10 16:52:52 +08:00

44 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
// 鼠标移入时图片样式发生改变
public class EnterImageUIChange : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
private Image image; // 被改变的图片
/**
* 移入时只会改变图片的透明度
* 移出时默认恢复到白色,当有点击按钮时会恢复到点击后的颜色
*/
public bool changeColor = true; // 是否改变颜色
private Color enterColor; // 移入颜色
public Color exitColor=Color.white; // 移出颜色
public bool changeCursor = true; // 是否改变光标
private Texture2D handCursor; // 用于存放小手光标图标
public Vector2 hotSpot = new Vector2(0, 0); // 设置光标热点
private void Start()
{
image = GetComponent<Image>();
handCursor = Resources.Load<Texture2D>("Cursor/MouseCursor");
}
public void OnPointerEnter(PointerEventData eventData)
{
enterColor = image.color;
exitColor = image.color;
enterColor.a = 0.5f;
if(changeColor)image.color = enterColor;
if(changeCursor)Cursor.SetCursor(handCursor, hotSpot, CursorMode.Auto);
}
public void OnPointerExit(PointerEventData eventData)
{
if(changeColor)image.color = exitColor;
if(changeCursor)Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
}
}