Unity 点击双击连击长按 事件检测(支持PC和移动端)
Posted DaLiangChen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 点击双击连击长按 事件检测(支持PC和移动端)相关的知识,希望对你有一定的参考价值。
InputDefine 类:(公共变量、常量、枚举、事件)定义
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputDefine
/// <summary>两次点击的时间间隔</summary>
public const float DOUBLE_CLICK_RATE = 0.3f;
/// <summary>连续按住0.5s后判定为长按</summary>
public const float LONG_PRESS_SCALE = 0.5f;
/// <summary>判断是否点击在UI上面</summary>
public static bool IsTouchUI()
if (UnityEngine.EventSystems.EventSystem.current == null) return false;
if (Application.platform == RuntimePlatform.android || Application.platform == RuntimePlatform.IPhonePlayer)
if (Input.touchCount < 1) return false;
if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) return true;
else
if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) return true;
return false;
/// <summary>输入事件类型</summary>
public enum EnumInputEventType
/// <summary>没有事件</summary>
None,
/// <summary>单击</summary>
Click,
/// <summary>双击</summary>
DoubleClick,
/// <summary>滑动</summary>
//Slide,
/// <summary>长按</summary>
LongPress,
/// <summary>
/// 输入事件
/// </summary>
/// <param name="isTouchUI">是否触摸到UI</param>
/// <param name="keyCode">(PC端)0表示左键,1表示右键,2表示中键</param>
/// <param name="enumInputEventType">输入事件类型</param>
public delegate void DelInputEvent(bool isTouchUI, int keyCode, EnumInputEventType enumInputEventType);
/// <summary>
/// 鼠标点击
/// </summary>
/// <param name="isTouchUI">是否触摸到UI</param>
/// <param name="keyCode">(PC端)0表示左键,1表示右键,2表示中键</param>
/// <param name="clickCount">连续点次数</param>
public delegate void DelClickEvent(bool isTouchUI, int keyCode, Vector2 clickPs, int clickCount);
MouseEvent 类:鼠标事件(PC端)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
/// <summary>鼠标事件</summary>
public class MouseEvent : MonoBehaviour
public DelInputEvent onInputEventChange;
public DelClickEvent onClickEvent;
private MouseParam[] mouseParams = new MouseParam[] new MouseParam(), new MouseParam(), new MouseParam() ;
private void Update()
addMouseInputEventTypeJudge();
/// <summary>添加</summary>
private void addMouseInputEventTypeJudge()
for(int i=0;i< mouseParams.Length;i++)
mouseInputEventTypeJudge(i);
/// <summary>鼠标左键输入事件判定</summary>
private void mouseInputEventTypeJudge(int mouseKeyCode)
//鼠标按下
if (Input.GetMouseButtonDown(mouseKeyCode))
mouseParams[mouseKeyCode].startTime = Time.realtimeSinceStartup;
//鼠标按住
if (Input.GetMouseButton(mouseKeyCode))
mouseParams[mouseKeyCode].holdTime = Time.realtimeSinceStartup - mouseParams[mouseKeyCode].startTime;
if (mouseParams[mouseKeyCode].holdTime >= InputDefine.LONG_PRESS_SCALE)
if (onInputEventChange != null) onInputEventChange(InputDefine.IsTouchUI(),mouseKeyCode,EnumInputEventType.LongPress);
//鼠标抬起
if (Input.GetMouseButtonUp(mouseKeyCode))
if (mouseParams[mouseKeyCode].holdTime >= InputDefine.LONG_PRESS_SCALE) return;
if (onInputEventChange != null) onInputEventChange(InputDefine.IsTouchUI(), mouseKeyCode,EnumInputEventType.Click);
mouseParams[mouseKeyCode].t2 = Time.realtimeSinceStartup;
if (mouseParams[mouseKeyCode].t2 - mouseParams[mouseKeyCode].t1 < InputDefine.DOUBLE_CLICK_RATE)
//Debug.Log(mouseKeyCode + ":双击!");
mouseParams[mouseKeyCode].clickCount++;
if (onInputEventChange != null) onInputEventChange(InputDefine.IsTouchUI(), mouseKeyCode,EnumInputEventType.DoubleClick);
else
mouseParams[mouseKeyCode].clickCount = 1;
mouseParams[mouseKeyCode].t1 = mouseParams[mouseKeyCode].t2;
if (onClickEvent != null) onClickEvent(InputDefine.IsTouchUI(), mouseKeyCode,Input.mousePosition, mouseParams[mouseKeyCode].clickCount);
public struct MouseParam
public float startTime;
public float holdTime;
public float t1;
public float t2;
public int clickCount;
TouchEvent 类:触摸事件(移动端)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>触摸事件</summary>
public class TouchEvent : MonoBehaviour
public DelClickEvent onClickEvent;
private void Update()
if (Input.touchCount > 0)
for (int i = 0; i < Input.touchCount; i++)
Touch touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began)
if (onClickEvent != null) onClickEvent(InputDefine.IsTouchUI(), i, touch.position, touch.tapCount);
//Debug.Log(i + "---" + "touch.tapCount:" + touch.tapCount+ "---touch.position:"+ touch.position);
以上是关于Unity 点击双击连击长按 事件检测(支持PC和移动端)的主要内容,如果未能解决你的问题,请参考以下文章
微信小程序-双击长按longtap事件与tap事件冲突的解决办法