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

unity学习26:用Input接口去监测: 鼠标,键盘,虚拟轴,虚拟按键

目录

1 用Input接口去监测:鼠标,键盘,虚拟轴,虚拟按键

2 鼠标 MouseButton 事件

2.1 鼠标的基本操作

2.2 测试代码

2.3 测试情况

3 键盘Key事件

3.1 键盘的枚举方式

3.2 测试代码同上

3.3 测试代码同上

3.4 测试结果

4 Joystick虚拟轴 Axis

4.1 什么是虚拟轴

4.2 虚拟轴的设置

4.3 虚拟轴的写法

4.4  测试代码同上

4.5 虚拟轴的测试

5 虚拟按键 Button

5.1 什么是虚拟按键

5.2 虚拟按键

5.3 测试代码同上

5.4 测试结果


1 用Input接口去监测:鼠标,键盘,虚拟轴,虚拟按键

  • 用Input接口去监测: 如 Input.GetMouseButton()
  • 鼠标,MouseButton
  • 键盘,Key
  • 虚拟轴,Axis
  • 虚拟按键,Button

还是挂上脚本来测试

2 鼠标 MouseButton 事件

2.1 鼠标的基本操作

  • "按下了鼠标左键"
  • Input.GetMouseButtonDown(0)

  • "持续按下鼠标左键"
  • Input.GetMouseButton(0)

  • "抬起了鼠标左键"
  • Input.GetMouseButtonUp(0)

2.2 测试代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class TestKey : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){//鼠标相关if(Input.GetMouseButtonDown(0)){Debug.Log("按下了鼠标左键");}if(Input.GetMouseButton(0)){Debug.Log("持续按下鼠标左键");}if(Input.GetMouseButtonUp(0)){Debug.Log("抬起了鼠标左键");}//键盘相关if(Input.GetKeyDown(KeyCode.A)){Debug.Log("按下了A");}if(Input.GetKey(KeyCode.A)){Debug.Log("持续按下A");}if(Input.GetKeyUp("a")){Debug.Log("抬起了A");}//虚拟轴相关float horizontal=Input.GetAxis("Horizontal");float vertical=Input.GetAxis("Vertical");Debug.Log(horizontal + " " + vertical);//虚拟按键if(Input.GetButtonDown("Jump")){Debug.Log("按下了Jump空格");}if(Input.GetButton("Jump")){Debug.Log("持续按Jump空格");}if(Input.GetButtonUp("Jump")){Debug.Log("抬起了Jump空格");}}
}

2.3 测试情况

3 键盘Key事件

3.1 键盘的枚举方式

  • 键盘的枚举方式
  • GetKey(KeyCode.A)
  • GetKey("a")

3.2 测试代码同上

  • 按下了按键A
  • Input.GetKeyDown(KeyCode.A)
  • 持续按住A
  • Input.GetKey(KeyCode.A)
  • 松开了A
  • Input.GetKeyUp("a")

3.3 测试代码同上

3.4 测试结果

4 Joystick虚拟轴 Axis

4.1 什么是虚拟轴

4.2 虚拟轴的设置

  • 默认的,只有水平和垂直的是虚拟轴,其他其实都是虚拟按键

4.3 虚拟轴的写法

  • 默认的,只有水平和垂直的是虚拟轴,其他其实都是虚拟按键
  • float horizontal=Input.GetAxis("Horizontal");
  • float vertical=Input.GetAxis("Vertical");
  • Debug.Log(horizontal + " " + vertical);

4.4  测试代码同上

4.5 虚拟轴的测试

  • 比如实测水平轴
  • 按下 a s 水平轴的数值,会持续的变化

5 虚拟按键 Button

5.1 什么是虚拟按键

  • 虚拟按键,和虚拟轴不一样,只有1个按键
  • 比如jump轴,只有1个 space按键

5.2 虚拟按键

  • "按下了Jump空格"     Input.GetButtonDown("Jump")
  • "持续按Jump空格"     Input.GetButton("Jump")
  • "抬起了Jump空格"    Input.GetButtonUp("Jump")

5.3 测试代码同上

5.4 测试结果

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

相关文章:

  • GB/T 43698-2024 《网络安全技术 软件供应链安全要求》标准解读
  • ASP.NET Core与EF Core的集成
  • 【AI大模型】Ubuntu18.04安装deepseek-r1模型+服务器部署+内网访问
  • SpringAI系列 - 使用LangGPT编写高质量的Prompt
  • Github - 记录一次对“不小心包含了密码的PR”的修复
  • 【玩转 Postman 接口测试与开发2_014】第11章:测试现成的 API 接口(下)——自动化接口测试脚本实战演练 + 测试集合共享
  • 前后端通过docker部署笔记
  • 五十天精通硬件设计第四天-场效应管知识及选型
  • 了解 ALV 中的 field catalog (ABAP List Viewer)
  • 【基于SprintBoot+Mybatis+Mysql】电脑商城项目之修改密码和个人资料
  • 十一、CentOS Stream 9 安装 Docker
  • FreeRTOS学习 --- 中断管理
  • 如何在Intellij IDEA中识别一个文件夹下的多个Maven module?
  • 机器学习模型--线性回归、逻辑回归、分类
  • gitlab个别服务无法启动可能原因
  • react的antd表格数据回显在form表单中
  • 深度分析:网站快速收录与网站内容多样性的关系
  • feign 远程调用详解
  • 【Android】jni开发之导入opencv和libyuv来进行图像处理
  • 【Elasticsearch】terms聚合误差问题
  • 深入理解 `box-sizing: border-box;`:CSS 布局的利器
  • 【原子工具】快速幂 快速乘
  • Apache SeaTunnel 整体架构运行原理
  • Nginx如何实现 TCP和UDP代理?
  • 蓝桥杯思维训练营(三)
  • 开箱即用的.NET MAUI组件库 V-Control 发布了!
  • 动手学图神经网络(9):利用图神经网络进行节点分类 WeightsBiases
  • 【文件上传、秒传、分片上传、断点续传、重传】
  • 使用Pygame制作“打砖块”游戏
  • 【完整版】DeepSeek-R1大模型学习笔记(架构、训练、Infra)