Unity——新输入系统Input System

1.安装

安装:

        直接到包管理器Window > Package Manager安装即可,安装后提示需要重启,重启后即可使用。

注意:

        在Project Settings中的Player设置里将Active Input Handling设置为Input System。

        需要将默认场景中的EventSystem中的Standalone Input Module变更为新的Input System UI Input Module组件。

2. 使用

1.创建

两种途径可以创建:

1.右键单击Create-->Input Actions创建 

2.在想要控制的物体上挂载组件Player Input后单击Create Actions创建。

2.进入配置界面创建Action

如果想要移动,跳跃等动作,可以双击创建创建的Input Actions或者选中此文件单击Edit asset

 进入配置界面

 点击 + 号创建ActionMaps,Actions并绑定按键

3.使用

1.需要引用的命名空间using UnityEngine.InputSystem;

2.使用wasd进行移动

using UnityEngine;
using UnityEngine.InputSystem;

public class PlayController : MonoBehaviour
{
    [SerializeField]
    private InputActionReference moveInput;//绑定PlayerMove/Move
    
    private void OnEnable()
    {
        moveInput.action.Enable();
        
    }
    private void OnDisable()
    {
        moveInput.action.Disable();
    }

    private void Update()
    {
        //检测是否按下wasd键
        //方法1:
        if (moveInput.action.WasPressedThisFrame())
        {
            Debug.Log("按下");
        }
        if (moveInput.action.WasReleasedThisFrame())
        {
            Debug.Log("抬起");
        }
        if (moveInput.action.WasPerformedThisFrame())
        {
            Debug.Log("按下+抬起");
        }

        //方法2:
        /*moveInput.action.performed += ctx =>
        {
            //移动
            transform.Translate(ctx.ReadValue<Vector2>());
        };*/
    }
}

3.把代码挂载到对应的gameObject上

 

补充:自定义按键的更换(跳跃)

 1.跳跃

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;

public class JumpManager : MonoBehaviour
{
    [SerializeField]
    public InputActionReference jumpInput;
    private void OnEnable()
    {
        jumpInput.action.Enable();
    }

    private void OnDisable()
    {
        jumpInput.action.Disable();
    }

    private void Update()
    {
        if(jumpInput.action.WasPressedThisFrame())
        {
            Debug.Log("jump");
        }
    }
}

2.更换按键

using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;

public class RebindButtons : MonoBehaviour
{
    public InputActionReference jump;
    public JumpManager jumpManager;
    
    public Button jumpButton;
    public TextMeshProUGUI text;

    void Awake()
    {
        jump = jumpManager.jumpInput;
    }
    private void Start()
    {
        jumpButton.onClick.AddListener(ReBinding);
    }

    //更改按键
    private void ReBinding()
    {
        Debug.Log("-----");
        jump.action.Disable();
        var rebind = jump.action.PerformInteractiveRebinding(0)
            .OnCancel(
                    operation =>
                    {
                        jump.action.Enable();
                    })
             .OnComplete(
                    operation =>
                    {
                        Debug.Log("OnComplete");
                        jump.action.Enable();
                    });
        rebind.Start();
    }
}

(更换按键步骤:点击按钮,选择并按下选择的按键,点击刚刚更换的按钮就可以执行了)

### Unity 输入系统中的 Scroll 功能说明 在 Unity输入系统Input System)中,`Scroll` 是用于处理滚动事件的一个重要功能。相比旧版 `InputManager` 中通过 `GetAxis("Scroll Wheel")` 获取鼠标滚轮数据的方式,输入系统提供了更灵活和直观的方法。 以下是关于如何在输入系统中使用 `Scroll` 的详细介绍: #### 1. 基本概念 输入系统允许开发者直接访问设备的具体属性,例如鼠标的滚动值。这使得代码更加清晰且易于维护。具体来说,可以通过 `Mouse.current.scroll.ReadValue()` 来读取当前的滚动值[^4]。 #### 2. 实现方法 要实现滚动功能,可以按照以下方式编写脚本: ```csharp using UnityEngine; using UnityEngine.InputSystem; public class ScrollExample : MonoBehaviour { void Update() { if (Mouse.current != null) { Vector2 scroll = Mouse.current.scroll.ReadValue(); // 读取滚动值 if (scroll.y != 0) { Debug.Log($"Scroll Value: {scroll.y}"); // 在这里添加滚动逻辑 } } } } ``` 上述代码展示了如何监听鼠标的滚动操作并打印其垂直方向上的变化量。如果需要水平滚动,则可以关注 `scroll.x` 属性。 #### 3. 配置 Input Action 资源文件 为了更好地利用输入系统的强大功能,建议创建一个自定义的 `InputActionAsset` 文件,并配置相应的动作映射。这样不仅可以简化代码结构,还能提高项目的可扩展性和灵活性。 - 打开 **Player Settings** 并启用输入系统。 - 创建一个的 `Input Actions` 文件,在其中定义名为 “Scroll” 的动作。 - 将该动作绑定到 `Mouse ScrollWheel` 输入源上。 完成以上设置后,可以在脚本中加载此资源文件并通过调用相应 API 访问滚动数据。 #### 4. 应用场景举例 假设我们需要开发一款支持缩放地图的游戏界面,那么就可以基于上面提到的技术方案构建如下交互流程: 当玩家向前推动滚轮时放大视图;向后拉动则缩小视图。 ```csharp private Camera mainCamera; private float zoomSpeed = 5f; private float minZoom = 10f; private float maxZoom = 50f; void Start() { mainCamera = GetComponent<Camera>(); } void Update() { if (Mouse.current != null) { var scrollDelta = Mouse.current.scroll.ReadValue().y * Time.deltaTime * zoomSpeed; mainCamera.fieldOfView -= scrollDelta; mainCamera.fieldOfView = Mathf.Clamp(mainCamera.fieldOfView, minZoom, maxZoom); } } ``` 这段程序实现了简单的相机视野调整效果,充分体现了输入系统的优势——简洁高效的同时保持高度可控性。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值