怎样才能完成Unity3d使用UGUI实现长按功能呢?可能许多人也不是很清楚,现在我们就来看一下。

UGUI的Button组件只有OnClick事件的监听,要实现长按功能,要监听按下事件和抬起事件,所以要使用到EventTrigger组件中的OnPointerDown和OnPointerUp来监听。

为了方便,把EventTrigger中的事件监听都封装起来,

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class EventTriggerListener: UnityEngine.EventSystems.EventTrigger
{
    public delegate void VoidDelegate(GameObject go);
    public delegate void BoolDelegate(GameObject go, bool state);
    public delegate void FloatDelegate(GameObject go, float delta);
    public delegate void VectorDelegate(GameObject go, Vector2 delta);
    public delegate void ObjectDelegate(GameObject go, GameObject obj);
    public delegate void KeyCodeDelegate(GameObject go, KeyCode key);
    public VoidDelegate onClick;
    public VoidDelegate onDown;
    public VoidDelegate onEnter;
    public VoidDelegate onExit;
    public VoidDelegate onUp;
    public VoidDelegate onSelect;
    public VoidDelegate onUpdateSelect;
    static public EventTriggerListener Get(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent < EventTriggerListener > ();
        if(listener == null) listener = go.AddComponent < EventTriggerListener > ();
        return listener;
    }
    static public EventTriggerListener Get(Transform transform)
    {
        EventTriggerListener listener = transform.GetComponent < EventTriggerListener > ();
        if(listener == null) listener = transform.gameObject.AddComponent < EventTriggerListener > ();
        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);
    }
}

使用EventTriggerListener的Get方法就可以了,和NGUI的UIEventListener一样。
例如:
EventTriggerListener.Get(gameObject).onDown += OnClickDown;
EventTriggerListener.Get(gameObject).onUp += OnClickUp;

然后在场景中添加一个按钮

Unity3d使用UGUI实现长按功能

再按钮上挂一个事件监听的脚本,譬如我要做一个类似技能CD冷却的效果,点击开始遮罩,松开恢复原状。
脚本如下:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EventTest: MonoBehaviour
{
    private bool isUp;
    private Image img;
    void Start()
    {
        /*EventTriggerListener.Get(gameObject).onDown += (go) => { Debug.Log("按下!"); };
        EventTriggerListener.Get(gameObject).onUp += (go) => { Debug.Log("抬起!"); };
        EventTriggerListener.Get(gameObject).onSelect += (go) => { Debug.Log("选中!"); };
        EventTriggerListener.Get(gameObject).onEnter += (go) => { Debug.Log("进入!"); };
        EventTriggerListener.Get(gameObject).onExit += (go) => { Debug.Log("退出!"); };
        * */
        img = transform.Find("Image").GetComponent < Image > ();
        EventTriggerListener.Get(gameObject).onDown += OnClickDown;
        EventTriggerListener.Get(gameObject).onUp += OnClickUp;
    }
    void OnClickDown(GameObject go)
    {
        isUp = false;
        StartCoroutine(grow());
    }
    void OnClickUp(GameObject go)
    {
        isUp = true;
        img.fillAmount = 0 f;
    }
    private IEnumerator grow()
    {
        while(true)
        {
            if(isUp)
            {
                break;
            }
            img.fillAmount += 0.5 f * Time.deltaTime;
            if(img.fillAmount == 0)
            {
                break;
            }
            yield
            return null;
        }
    }
}

长按效果:

Unity3d使用UGUI实现长按功能