Unity 点击按钮回调,反馈

  1. uButtonScale: 鼠标进入,点击等互动的按钮反馈
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;

namespace uTools {
    [AddComponentMenu("uTools/Tween/Button Scale(uTools)")]
    public class uButtonScale : MonoBehaviour, uIPointHandler {

        public RectTransform tweenTarget;
        public Vector3 enter = new Vector3(1.1f, 1.1f, 1.1f);
        public Vector3 down = new Vector3(1.05f, 1.05f, 1.05f);
        public float duration = .2f;

        Vector3 mScale;

        // Use this for initialization
        void Start () {
            if (tweenTarget == null) {
                tweenTarget = GetComponent();
            }
            mScale = tweenTarget.localScale;
        }

        public void OnPointerEnter (PointerEventData eventData) {
            Scale(enter);
        }

        public void OnPointerExit(PointerEventData eventData) {
            Scale(mScale);      
        }

        public void OnPointerDown (PointerEventData eventData) {
            Scale(down);
        }

        public void OnPointerUp (PointerEventData eventData) {
            Scale(mScale);      
        }

        public void OnPointerClick (PointerEventData eventData) {
        }

        void Scale(Vector3 to) {
            uTweenScale.Begin(tweenTarget.gameObject, tweenTarget.localScale, to, duration);
        }
    }
}
  1. 回调介质
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;


public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger
{
    public delegate void VoidDelegate(GameObject go);
    public VoidDelegate onClick;
    public VoidDelegate onDown;
    public VoidDelegate onEnter;
    public VoidDelegate onExit;
    public VoidDelegate onUp;
    public VoidDelegate onSelect;
    public VoidDelegate onUpdateSelect;

    public VoidDelegate onDrag;
    public VoidDelegate onDragEnd;


    static public EventTriggerListener Get(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent();
        if (listener == null)
            listener = go.AddComponent();
        return listener;
    }

    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null)
            onClick(gameObject);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null)
            onDown(gameObject);
    }
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (onEnter != null)
            onEnter(gameObject);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (onExit != null)
            onExit(gameObject);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null)
            onUp(gameObject);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null)
            onSelect(gameObject);
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null)
            onUpdateSelect(gameObject);
    }

}

然后注册一个回调函数就可以。

你可能感兴趣的:(Unity3D,Unity,UGUI)