介绍一下我们的主角–>InputSystem
请上Google百度一下!(注意unity版本)
安装
在Windows->Packages Manager中找到Input System,点击安装
编写示例
接下来编写一个简单的按键示例,代码如下
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Input;
public class Test1 : MonoBehaviour
{
public InputAction fInput;
//激活
private void OnEnable()
{
fInput.Enable();
}
private void OnDisable()
{
fInput.Disable();
}
private void Awake()
{
Action<InputAction.CallbackContext> tellMeYourName = (InputAction.CallbackContext context) =>
{
Debug.Log("my keyboard is " + fInput.name);
};
fInput.performed += tellMeYourName;
}
}
需要使用 inputAction.Enable() 来激活
然后我们将该脚本拖到场景对象上(我是直接放到主摄像机上了)
设置按键
接下来为我们的定义的InputAction属性->fInput设置按键(我设置的是F键)
运行结果
最后我们就可以运行了,在游戏窗口内按下F键,就可以看输出的打印内容
当然还有另外一种方法,也是游戏开发中比较常用的;请参考我的另外一篇文章https://blog.csdn.net/weixin_40124181/article/details/98850440