当前位置: 首页 > news >正文

【Unity输入系统】自定义与双击不冲突的单击Interaction

一、背景

unity自带的两种默认interaction——单击(tap)、多击(multip tap)在实际使用时可以发现,假设有多个action,action A绑定了按键A单击,action B绑定了按键A双击,则双击按键A时会导致action A也触发。为了规避这种情况,自定义了一个与双击不冲突的单击Interaction

二、思路及代码

  • 在检测到点击后延迟一小段时间(clickDelay)再判断(确认是否有第二次点击)是否进入perform状态

  • 如果在 clickDelay 期间没有再次点击,并且按住的时间不超过 holdThreshold,就判定为 单击

  • 如果超过 holdThreshold 就是 长按(取消单击逻辑)。

  • 如果在 clickDelay 时间内再次按下,则判断为 双击,取消单击逻辑。

using System.Diagnostics;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Utilities;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingleClickOnlyIfNotDouble : IInputInteraction
{public float clickDelay = 0.3f;      // 点击后的等待时间,用于检测是否为双击(单位:秒)public float holdThreshold = 0.3f;   // 判定为长按的最小时间(单位:秒)private double m_TapStartTime;       // 记录第一次按下的时间点bool hasClicked = false;            // 标记是否已经处理过一次点击#if UNITY_EDITOR[UnityEditor.InitializeOnLoadMethod]
#else[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
#endifpublic static void Initialize(){InputSystem.RegisterInteraction<SingleClickOnlyIfNotDouble>();}public void Process(ref InputInteractionContext context){switch (context.phase){case InputActionPhase.Waiting:if (context.control is ButtonControl button && button.wasPressedThisFrame){m_TapStartTime = context.time;context.Started();context.SetTimeout(clickDelay);}break;case InputActionPhase.Started:if (context.control is ButtonControl button2){if (button2.wasReleasedThisFrame && !hasClicked){// UnityEngine.Debug.Log("按住时间" + (context.time - m_TapStartTime) + hasClicked);if (context.time - m_TapStartTime <= holdThreshold)//松手且未超时才判定为单击{// UnityEngine.Debug.Log("按住时间" + (context.time - m_TapStartTime) + hasClicked);hasClicked = true;// UnityEngine.Debug.Log("单击检测");}else{context.Canceled();// UnityEngine.Debug.Log("Hold detected → canceled");}}if (hasClicked){if (context.time - m_TapStartTime >= clickDelay){context.Performed();// UnityEngine.Debug.Log("Single click performed");}else if (button2.wasPressedThisFrame){context.Canceled();// UnityEngine.Debug.Log("Double click detected → canceled");}}}break;}}public void Reset(){hasClicked = false;}
}

需要注意的是,

  •  m_TapStartTime不能被context.startTime替代,context.startTime和context.time并不会在每次触发该interaction时重置,startTime是一个固定值,time是一个累计值,这会导致算出的时间差越来越长
  • clickDelay和holdThreshold的时间设置无关,因为一个是针对第一次按住检测,一个是针对第一次按下后检测。
  • holdThreshold设置应短于Hold的HoldTime,否则会导致其单击操作无法与Hold区分
  • clickDelay设置应长于MultiTap的Max Tap Spacing,否则会导致其单击操作无法与双击区分

三、参考

【Unity】Input Systemでシングルタップとマルチタップを判別する | ねこじゃらシティ

【Unity】Input SystemのInteractionの仕組みと使い方 | ねこじゃらシティ

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.7/manual/Interactions.html#default-interaction

http://www.dtcms.com/a/319523.html

相关文章:

  • 【第八章】函数进阶宝典:参数、返回值与作用域全解析
  • RedisBloom使用
  • 任务进度状态同步 万能版 参考 工厂+策略+观察者设计模式 +锁设计 springboot+redission
  • itextPdf获取pdf文件宽高不准确
  • 设计模式-装饰模式 Java
  • 客户端利用MinIO对服务器数据进行同步
  • VN1 供应链销量预测建模竞赛技巧总结与分享(七)
  • 四边形面积
  • 极简 5 步:Ubuntu+RTX4090 源码编译 vLLM
  • JavaWeb03——基础标签及样式(表单)(黑马视频笔记)
  • 八、基于GD32 Embedded Builder开发GD32VW553(蓝牙广播)
  • 复杂光照场景漏检率↓76%!陌讯多模态融合算法在打电话识别的边缘部署优化
  • 使用Puppeteer轻松自动化浏览器操作
  • PYLON交叉编译:Ubuntu是x86,编译出arm64上运行的程序
  • 无人机航拍数据集|第8期 无人机海上目标检测YOLO数据集3641张yolov11/yolov8/yolov5可训练
  • 下载 | Windows Server 2016最新原版ISO映像!(集成7月更新、标准版、数据中心版、14393.8246)
  • 基于 C 语言的多态机制的驱动架构
  • 十八、k8s细粒度流量管理:服务网格
  • UiPath Studio介绍
  • CS231n2017 Assignment3 RNN、LSTM部分
  • 仁懋高压MOSFET在新能源汽车充电领域的应用
  • Java并发与数据库锁机制:悲观锁、乐观锁、隐式锁与显式锁
  • Java基础学习1(Java语言概述)
  • 音视频时间戳获取与同步原理详解
  • 如何为WordPress启用LiteSpeed缓存
  • --- Eureka 服务注册发现 ---
  • 安卓Handler和Looper的学习记录
  • 计算机视觉-OpenCV
  • GPT-5 将在周五凌晨1点正式发布,王炸模型将免费使用??
  • Android 之 Kotlin 扩展库KTX