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

目录

[1 用Input接口去监测:鼠标,键盘,虚拟轴,虚拟按键](#1 用Input接口去监测:鼠标,键盘,虚拟轴,虚拟按键)

[2 鼠标 MouseButton 事件](#2 鼠标 MouseButton 事件)

[2.1 鼠标的基本操作](#2.1 鼠标的基本操作)

[2.2 测试代码](#2.2 测试代码)

[2.3 测试情况](#2.3 测试情况)

[3 键盘Key事件](#3 键盘Key事件)

[3.1 键盘的枚举方式](#3.1 键盘的枚举方式)

[3.2 测试代码同上](#3.2 测试代码同上)

[3.3 测试代码同上](#3.3 测试代码同上)

[3.4 测试结果](#3.4 测试结果)

[4 Joystick虚拟轴 Axis](#4 Joystick虚拟轴 Axis)

[4.1 什么是虚拟轴](#4.1 什么是虚拟轴)

[4.2 虚拟轴的设置](#4.2 虚拟轴的设置)

[4.3 虚拟轴的写法](#4.3 虚拟轴的写法)

[4.4 测试代码同上](#4.4 测试代码同上)

[4.5 虚拟轴的测试](#4.5 虚拟轴的测试)

[5 虚拟按键 Button](#5 虚拟按键 Button)

[5.1 什么是虚拟按键](#5.1 什么是虚拟按键)

[5.2 虚拟按键](#5.2 虚拟按键)

[5.3 测试代码同上](#5.3 测试代码同上)

[5.4 测试结果](#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 测试代码

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestKey : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void 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 测试结果

相关推荐
深海潜水员6 小时前
【Behavior Tree】-- 行为树AI逻辑实现- Unity 游戏引擎实现
游戏·unity·c#
Thomas_YXQ15 小时前
Unity3D游戏内存优化指南
游戏·unity·职场和发展·性能优化·蓝桥杯·游戏引擎·unity3d
chillxiaohan16 小时前
Unity接入Steamworks.NET实现通信功能
unity
枯萎穿心攻击1 天前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
X_StarX1 天前
【Unity笔记02】订阅事件-自动开门
笔记·学习·unity·游戏引擎·游戏开发·大学生
Thomas_YXQ2 天前
Unity URP法线贴图实现教程
开发语言·unity·性能优化·游戏引擎·unity3d·贴图·单一职责原则
徐子竣2 天前
[学习记录]Unity-Shader-几何着色器
unity·游戏引擎·着色器
EQ-雪梨蛋花汤2 天前
【Part 3 Unity VR眼镜端播放器开发与优化】第四节|高分辨率VR全景视频播放性能优化
unity·音视频·vr
与火星的孩子对话2 天前
Unity进阶课程【六】Android、ios、Pad 终端设备打包局域网IP调试、USB调试、性能检测、控制台打印日志等、C#
android·unity·ios·c#·ip
幻世界2 天前
【Unity智能模型系列】Unity + MediaPipe + Sentis + ArcFace模型:构建高效人脸识别比对系统
unity·游戏引擎