示例
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;
/// <summary>
/// 用于UI转发点击事件
/// </summary>
public class UIButtonMessage : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
{
[Serializable]
// 定义按钮OnClick事件类
public class OnClickEvent : UnityEvent { }
// 防止序列化变量重命名后丢失引用
[FormerlySerializedAs("onClick")]
[SerializeField]
private OnClickEvent m_OnClick = new OnClickEvent();
public Image image;
public Vector3 downScale = Vector3.one;
public Color downColor = Color.white;
private Vector3 originScale;
private Color originColor;
public void OnPointerClick(PointerEventData eventData)
{
m_OnClick.Invoke();
}
public void OnPointerDown(PointerEventData eventData)
{
RectTransform rt = this.GetComponent<RectTransform>();
originScale = rt.localScale;
rt.localScale = downScale;
if (image)
{
originColor = image.color;
image.color = downColor;
}
}
public void OnPointerUp(PointerEventData eventData)
{
RectTransform rt = this.GetComponent<RectTransform>();
rt.localScale = originScale;
if (image)
image.color = originColor;
}
}