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

Unity输入系统:旧版Input_System

Input 是 Unity 内置的静态类(属于 UnityEngine 命名空间),用于获取各种输入设备的原始数据(如键盘、鼠标、触摸、手柄等)。它是 Unity 旧输入系统(Legacy Input System)的核心入口,提供了大量静态属性和方法来访问输入状态

  • Input.touches:返回当前所有活跃的触摸点数组(Touch[] 类型)。
  • Input.touchCount:返回当前触摸点的数量

我的上一篇EventSystem这篇文章中解释过的TouchInputModule 类正是通过 Input 类获取原始触摸数据,再将其转换为事件系统可识别的 PointerEventData

// TouchInputModule 内部的 ProcessTouchEvents 简化实现
protected virtual void ProcessTouchEvents()
{// 通过 Input 类获取所有触摸点for (int i = 0; i < Input.touchCount; i++){Touch touch = Input.touches[i];int pointerId = touch.fingerId; // 触摸点唯一ID// 将触摸数据转换为 PointerEventDataPointerEventData eventData;GetPointerData(pointerId, out eventData, true);eventData.position = touch.position; // 触摸位置(屏幕坐标)// 根据触摸状态分发事件(按下、移动、抬起等)if (touch.phase == TouchPhase.Began){ProcessPointerPress(eventData); // 处理按下事件}else if (touch.phase == TouchPhase.Moved){ProcessPointerMove(eventData); // 处理移动事件}else if (touch.phase == TouchPhase.Ended){ProcessPointerRelease(eventData); // 处理抬起事件}}
}

输入相关直接看代码使用

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class OldInputSystem : MonoBehaviour
{void BasicInoutCheck(){//任意按键被按下if (Input.anyKey){Debug.Log("任意按键被按下");}//任意按键在在当前帧被按下if (Input.anyKeyDown){Debug.Log("任意按键在在当前帧被按下");}//重置所有输入轴if (Input.GetKeyDown(KeyCode.R)){Input.ResetInputAxes();Debug.Log("重置所有输入轴");}}//鼠标操作void MouseInputHandling(){//鼠标是否存在if (Input.mousePresent){//鼠标位置Vector3 mousePos = Input.mousePosition;//与屏幕的距离Vector3 distance =Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2, 0);Vector2 scroll = Input.mouseScrollDelta;//鼠标滚轮if (scroll.y != 0){Debug.Log("鼠标滚轮滚动");}//鼠标按键按下 左键 右键 中间if (Input.GetMouseButtonDown(0)) { }if (Input.GetMouseButtonUp(1)) { }if (Input.GetMouseButton(2)) { }}}//键盘操作void KeyboardInputHandling(){//按键检测 KeyCode枚举if (Input.GetKeyDown(KeyCode.A)) { }if (Input.GetKeyUp(KeyCode.A)) { }if (Input.GetKey(KeyCode.A)) { }if (Input.GetKeyUp(KeyCode.Return)){Debug.Log("Enter");}//组合键if (Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.S)){Debug.Log("Ctrl+s");}}//触摸void TouchInputHandling(){//是否支持触摸if (Input.touchSupported){//触摸点数量if (Input.touchCount > 0){//获取第一个触摸点Touch touch = Input.touches[0];//触摸位置Vector2 touchPos = Input.touches[0].position;//触摸状态 开始 移动 静止 结束 取消switch (touch.phase){case TouchPhase.Began:Debug.Log("开始");break;case TouchPhase.Moved:Debug.Log("移动");break;case TouchPhase.Stationary:Debug.Log("静止");break;case TouchPhase.Ended:Debug.Log("结束");break;case TouchPhase.Canceled:Debug.Log("取消");break;}//多点触摸 扁你所有触摸点foreach (Touch t in Input.touches){Debug.Log(t.fingerId);}}}}void ControllerInputHandling(){//手柄数量// Debug.Log($"手柄数量:{Input.joystickCount}");  //AI说2019后的版本可用 (但不知道为什么我的2022版本报错下面的旧版却不报错)// 获取手柄数量int joystickCount = Input.GetJoystickNames().Length;Debug.Log($"连接的手柄数量:{joystickCount}");//手柄名称列表string[] joystickNames = Input.GetJoystickNames();foreach (string name in joystickNames){Debug.Log($"手柄名称: {name}");}//手柄按钮 需要在InputManager中配置if (Input.GetButtonDown("Fire1")){Debug.Log("Fire1按钮按下(通常是Ctrl或手柄按键)");}if (Input.GetButtonUp("Jump")){Debug.Log("Jump按钮抬起_通常是空格或手柄按键");}//手柄摇杆float horizontal = Input.GetAxis("Horizontal");float vertical = Input.GetAxis("Vertical");if (Mathf.Abs(horizontal) > 0.1f || Mathf.Abs(vertical) > 0.1f){Debug.Log($"摇杆移动: X={horizontal}, Y={vertical}");}}//虚拟轴void VirtualAxisHandling(){//平滑轴float horizontal = Input.GetAxis("Horizontal");//原始轴float vertical = Input.GetAxisRaw("Vertical");//自定义轴(InputManager中配置)float customAxis = Input.GetAxis("CustomAxis");}
}

http://www.lryc.cn/news/621555.html

相关文章:

  • 大气负氧离子自动监测站:解密空气的科技密码
  • SSL和TLS协议的消息认证码(MAC)
  • 【opencv-Python学习笔记(5):几何变换】
  • 《Effective Java》第1条:用静态工厂方法代替构造器
  • 【R语言】R 语言中 gsub 与正则表达式详解(含 POSIX 与 Perl 风格实例)
  • 【R语言】更换电脑后,如何在新设备上快速下载原来设备的 R 包?
  • 智能体开发实战:用Deepseek做一个生成思维导图的智能体
  • 2025高防IP vs 普通IP:本质差异与选型指南
  • 移动板房的网络化建设
  • StarRocks集群部署
  • 39 C++ STL模板库8-容器1-array
  • 常见IP模块的仲裁策略和实现
  • YOLO11分割模型使用rknn2量化部署
  • 网络安全蓝队常用工具全景与实战指南
  • 【DDIA】第二部分:分布式数据
  • 从零到一:发布你的第一个 npm 开源库(2025 终极指南)
  • Elasticsearch赋能规章制度智能检索:从海量文档到秒级响应
  • app-5 控制卡升级
  • 【CV 目标检测】②R-CNN模型
  • 「iOS」————UITableView性能优化
  • GCC深度剖析:从编译原理到嵌入式底层实战
  • 阿里云出里两款新的云服务器
  • 基于单片机的超市储物柜设计
  • 打破传统局限,人工智能+虚拟仿真赋能日化品设计实验教学
  • 异步并发×编译性能:Dart爬虫的实战突围
  • 笔试——Day39
  • Python洛谷做题39:P5729 【深基5.例7】工艺品制作
  • 【题解|两种做法】[ZJOI2008] 洛谷 P2600 瞭望塔[半平面交]
  • 第十章 项目进度管理-10.3 规划进度管理
  • Mini MAX AI应用矩阵测评报告——基于旗下多款产品的综合体验与行业价值分析