微信扫一扫 分享朋友圈

已有 1296 人浏览分享

开启左侧

【转载】Unity3d使用UGUI实现长按功能

[复制链接]
1296 0

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

代码:

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;

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

然后再按钮上挂一个事件监听的脚本,譬如我要做一个类似技能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 = 0f;
    }



    private IEnumerator grow()
    {
        while (true)
        {
            if (isUp)
            {              
                break;
            }
            img.fillAmount += 0.5f * Time.deltaTime;
            if (img.fillAmount == 0)
            {
                break;
            }
            yield return null;
        }
    }

}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

0

关注

0

粉丝

12

主题
精彩推荐
热门资讯
网友晒图
图文推荐
  • iOS App

  • 安卓App

Archiver|手机版|小黑屋|技你太美101

GMT+8, 2024-11-23 20:31 , Processed in 0.110545 second(s), 31 queries .

Powered by 技你太美101

© 2024 JNTM101 Team