您的位置:首页 > 移动开发 > Unity3D

[UnityUI]UGUI按钮长按效果

2016-05-19 16:09 537 查看
参考链接:http://blog.csdn.net/huutu/article/details/46448313

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.Events;

public class RepeatButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler {

public bool invokeOnce = false;//是否只调用一次
private bool hadInvoke = false;//是否已经调用过

public float interval = 0.1f;//按下后超过这个时间则认定为"长按"
private bool isPointerDown = false;
private float recordTime;

public UnityEvent onPress = new UnityEvent();//按住时调用
public UnityEvent onRelease = new UnityEvent();//松开时调用

void Start ()
{

}

void Update ()
{
if (invokeOnce && hadInvoke) return;
if (isPointerDown)
{
if ((Time.time - recordTime) > interval)
{
onPress.Invoke();
hadInvoke = true;
}
}
}

public void OnPointerDown(PointerEventData eventData)
{
isPointerDown = true;
recordTime = Time.time;
}

public void OnPointerUp(PointerEventData eventData)
{
isPointerDown = false;
hadInvoke = false;
onRelease.Invoke();
}

public void OnPointerExit(PointerEventData eventData)
{
isPointerDown = false;
hadInvoke = false;
onRelease.Invoke();
}
}


可以通过面板拖拽或者代码来进行事件的绑定:
buttonL.GetComponent<RepeatButton>().onPress.AddListener(MoveLeft);
buttonL.GetComponent<RepeatButton>().onRelease.AddListener(Stand);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  按钮长按