UGUI-UIButtonMessage

作者:追风剑情 发布于:2019-6-5 18:23 分类:Unity3d

示例


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;
    }
}



标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号