小工具知识网

当前位置: 首页 > 行业知识 > Unity游戏输入系统(新版+旧版)

行业知识

Unity游戏输入系统(新版+旧版)

2026-11-02 17:15:31 来源:互联网转载

使用新版还是旧版

旧版

using System.Collections;using System.Collections.Generic;using UnityEngine;public class c5 : MonoBehaviour{    void Start()    {            }    void Update()    {        // 注意要在游戏中 点鼠标键盘进行测试        // 鼠标        // 0左键 1右键 2滚轮        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))  // 写法1        if (Input.GetKey("a"))           //写法2        {            Debug.Log("持续A键");        }        // 松开        if (Input.GetKeyUp(KeyCode.A))        {            Debug.Log("松开A键");        }    }}

新版

1、安装

安装���版后,在 [编辑-> 项目设置-> 玩家-> 其他设置-> 输入活动 ]处理中设置,新版或者两个。

2、设置

安装后,首先要挂新版的组件 “Playre Input”,使用搜索,输入Input。

3、新建 Input Aactions

4、设置键位

双击新建的文件,出现下面的菜单。

若想使用键盘:keyboard --> By Location of Key(Using US Layout) --> 选择按键。

若想使用鼠标:Mouse --> left Button(左键)

5、使用设置好的文件

还需要使用脚本

using System.Collections;using System.Collections.Generic;using UnityEngine;// 导入新版 游戏操作系统using UnityEngine.InputSystem;public class c6 : MonoBehaviour{    // 声明    PlayerInput input;    void Start()    {        // 获取 新游戏操作系统的组件        input = GetComponent();        // 如需切换 另一套动作        // input.SwitchCurrentActionMap("动作名字");        // 开启该动作        input.currentActionMap.Enable();        // 获取 按下动作事件 (跳跃)        input.actions["Jump"].performed += Jump;        // 获取 松开动作事件  (跳跃)        input.actions["Jump"].canceled += Jump;        // 获取 按下动作事件 (攻击)        input.actions["Attack"].performed += Attack;    }    /*    private void Update()    {        // 获取Move的Vector2向量 (我设置了一个手柄的按键)        Vector2 v = input.actions["Move"].ReadValue();        Debug.Log("水平轴:" + v.x + ", 垂直轴:" + v.y);    }    */    // 攻击函数    private void Attack(InputAction.CallbackContext obj)    {        Debug.Log("按下攻击");    }    // 跳跃函数    private void Jump(InputAction.CallbackContext obj)    {        if (obj.performed == true)        {            Debug.Log("按下跳跃");        }        if (obj.canceled == true)        {            Debug.Log("松开跳跃");        }    }}

效果如下:

设定上下左右

将4个绑定按键

asd就不截图了,都一样。

using System.Collections;using System.Collections.Generic;using UnityEngine;// 导入新版 游戏操作系统using UnityEngine.InputSystem;public class c6 : MonoBehaviour{    // 声明    PlayerInput input;    void Start()    {        // 获取 新游戏操作系统的组件        input = GetComponent();        // 如需切换 另一套动作        // input.SwitchCurrentActionMap("动作名字");        // 开启该动作        input.currentActionMap.Enable();        // 获取 按下动作事件 (跳跃)        input.actions["Jump"].performed += Jump;        // 获取 松开动作事件  (跳跃)        input.actions["Jump"].canceled += Jump;        // 获取 按下动作事件 (攻击)        input.actions["Attack"].performed += Attack;    }    private void Update()    {        // 获取Move的Vector2向量 (我设置了一个手柄的按键)        Vector2 v = input.actions["Move"].ReadValue();        Debug.Log("水平轴:" + v.x + ", 垂直轴:" + v.y);    }    // 攻击函数    private void Attack(InputAction.CallbackContext obj)    {        Debug.Log("按下攻击");    }    // 跳跃函数    private void Jump(InputAction.CallbackContext obj)    {        if (obj.performed == true)        {            Debug.Log("按下跳跃");        }        if (obj.canceled == true)        {            Debug.Log("松开跳跃");        }    }}

按键后可观察到,两个轴的数值发生变化。

上一篇:Carparkingios下载全攻略,一键安装,轻松上手!

下一篇:宝塔面板隐藏index.html秘籍,网站安全无痕保护指南!