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 按钮上面。
本文介绍了一个Unity脚本,用于实现按钮上的长按与短按检测功能。该脚本通过跟踪触摸开始时间和触摸状态来区分两种操作,并在检测到相应事件时输出日志。
1182

被折叠的 条评论
为什么被折叠?



