UGUI - 长按事件实现

来源:互联网 发布:淘宝怎么卖游戏帐号 编辑:程序博客网 时间:2024/05/24 07:26

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.Events;
public class UILongPressEvent: MonoBehaviour {
//==============Start===========Var 变量=================//
private bool isTouchDown = false;
private float touchBegin = 0;
float interval = 0.1f;
float longPressDelay = 0.5f;
//==============End===================Var 变量========================//
void Update()
{
if (Time.time - touchBegin > longPressDelay && isTouchDown)
{
Debug.Log(“长按”);
}
}
// 手指按下
private void OnButtonDown()
{
touchBegin = Time.time;
isTouchDown = true;

}
//手指抬起
private void OnButtonUp()
{
if (Time.time - touchBegin <= interval)
{
Debug.Log(“短按”);
}
isTouchDown = false;
}
//手指离开
private void OnButtonExit()
{
isTouchDown = false;
}

}

这个脚本放在 Button 按钮上面。

0 0