首页 > 代码库 > Unity3d使用UGUI实现长按功能
Unity3d使用UGUI实现长按功能
UGUI的Button组件只有OnClick事件的监听,要实现长按功能,要监听按下事件和抬起事件,所以要使用到EventTrigger组件中的OnPointerDown和OnPointerUp来监听。
为了方便,把EventTrigger中的事件监听都封装起来,我是使用了雨松MOMO写的EventTriggerListener,用起来很方便,地址:http://www.xuanyusong.com/archives/3325
代码:
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; } }}
长按效果:
Unity3d使用UGUI实现长按功能
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。