[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);
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);
相关文章推荐
- 按钮长按功能
- iOS模拟键盘长按删除
- Unity中的C#内存管理(三)
- Unity中的C#内存管理(二)
- Unity中的C#内存管理(一)
- Unity内置 Asset store下载文件夹的位置
- Unity之AssetPostprocessor学习一
- I am invited to be a mentor and translator in the mooc community
- Unity 基础 - Input 类
- 【unity插件】unity手柄插件Input输入管理插件,兼容各种主流手柄
- 渠道SDK接入的各种坑
- Unity 基础 - 脚本的生命周期
- Unity3D脚本各函数执行顺序And不同脚本的执行顺序
- Ubuntu 重置unity
- iOS - 将Unity导出的Xcode工程导入到另一个Xcode项目, 及常见报错的解决方法
- Error: duplicate files during packaging of APK D:\eclipse\workspace\YC_SafeHelpe r_Community\build\o
- Unity3D遮挡剔除
- Unity编译iOS工程的自动化配置(XUPorter)
- 矩阵在图形学以及Unity中的应用
- Unity3d 同步加载 Assetbundle 方法 AssetBundle.CreateFromFile