当前位置: 首页 > 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.xdnf.cn/news/1304443.html

相关文章:

  • 链路聚合与软件网桥配置
  • Mac(一)常用的快捷键整理
  • JavaScript(JS)DOM(四)
  • 【数据分享】2022 年黑龙江省小麦、玉米和水稻幼苗影像数据集
  • Python基础(Flask①)
  • 基于机器学习的赌博网站识别系统设计与实现
  • 数据结构——顺序表单链表oj详解
  • 8.15 机器学习(2)K最近邻算法
  • k8s注意事项
  • Nginx反向代理Tomcat实战指南
  • 8月4日实训考察:重庆五一职院走进成都国际影像产业园
  • PCA降维 提升模型训练效率
  • 【科研绘图系列】R语言绘制多种饼图
  • nVidia Tesla P40使用anaconda本地重编译pytorch3d成功加载ComfyUI-3D-Pack
  • 前端动画库之gsap
  • 深入解析五大通信协议:TCP、UDP、HTTP_HTTPS、WebSocket与GRPC
  • Al大模型-本地私有化部署大模型-大模型微调
  • 腾讯位置商业授权微信小程序逆地址解析(坐标位置描述)
  • day29-进程和线程(2)
  • C语言:指针(5)
  • lcm通信库介绍与使用指南
  • 使用Docker容器化Python测试Pytest项目并配置GitHub Actions CI/CD流程
  • Pytest项目_day16(yaml和parametrize结合)
  • week1-[循环嵌套]蛇
  • Vue2与Vue3生命周期函数全面解析:从入门到精通
  • Linux操作系统--多线程(锁、线程同步)
  • 基本电子元件:贴片电阻器的种类
  • 达梦数据库使用控制台disql执行脚本
  • Mac(二)Homebrew 的安装和使用
  • HDFS数据倾斜导致MapReduce作业失败的排查与优化实践