当前位置: 首页 > 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.dtcms.com/a/333481.html

相关文章:

  • 第四章:大模型(LLM)】06.langchain原理-(3)langchain 数据连接方法
  • kubernetes(4) 微服务
  • 前往中世纪 送修改器(Going Medieval)免安装中文版
  • AI大模型配置项
  • 【mysql数据库全部重点知识】
  • 企业级时序数据库选型指南:从传统架构向智能时序数据管理的转型之路
  • 昆仑万维重磅发布Mureka V7.5与MoE-TTS,AI音乐与语音合成再升级!
  • 嵌入式学习 day52 IMX6ULL裸机开发-I2C
  • 基于Spring Boot的智能民宿预订与游玩系统设计与实现 民宿管理系统 民宿预订系统 民宿订房系统
  • 蓝桥杯 二叉树
  • [ CSS 前端 ] 网页内容的修饰
  • linux下找到指定目录下最新日期log文件
  • liteflow
  • CSS从入门到精通完整指南
  • 【学习笔记】Java并发编程的艺术——第8章 Java中的并发工具类
  • Python工具箱系列(六十二)
  • 写作在学习中的重要性
  • 【完整源码+数据集+部署教程】脑部健康状态检测系统源码和数据集:改进yolo11-AIFI
  • 力扣top100(day04-05)--堆
  • **标题:发散创新之力,探索隐私计算的未来**隐私计算,作为当下数字化时代的热门话题,正受
  • MCP简单入门及简单操作案例(高德地图调用实现酒店查询天气查询等[Lima]示范)
  • 在执行部署脚本后,通过 ls -la 命令查看远程服务器文件时,显示的文件所有者是 games 而不是预期的 root 用户
  • 二、DOCKER常用命令
  • 最长递增子序列-dp问题+二分优化
  • Vue 侦听器(watch 与 watchEffect)全解析1
  • 【161页PPT】智慧方案企业数字化转型概述(课件)(附下载方式)
  • pcl法线估计的踩坑
  • 【GPT入门】第47课 大模型量化中 float32/float16/uint8/int4 的区别解析:从位数到应用场景
  • 《P1194 买礼物》
  • PyTorch的安装-CPU版本或者GPU安装有什么区别吗