c# - 使用 Unity 的新输入系统检测鼠标滚轮滚动输入

标签 c# unity3d input user-input mousewheel

我目前正在尝试为我目前在 Unity 引擎中进行的 2D 游戏检测鼠标滚轮滚动输入。

我正在使用新的输入系统,目前我被以下代码困住了。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem.Controls;

public class ZoomController : MonoBehaviour
{

private PlayerControls playerControls;

private void Awake()
{
    playerControls = new PlayerControls();
}


private void OnEnable()
{
    playerControls.Enable();
}

private void OnDisable()
{
    playerControls.Disable();
}

void Start()
{
    
}

void Update()
{

    if (playerControls.Land.Zoom.ReadValue<Vector2>().y >= 1)
    {
        Debug.Log("Scroll 1");
    } else if (playerControls.Land.Zoom.ReadValue<Vector2>().y <= -1)
    {
        Debug.Log("Scroll 2");
    }
    
 }
}

enter image description here

如果我运行代码什么也没有发生,我不知道为什么。

最佳答案

如果您将滚轮视为“一维轴”,您需要告诉 Unity 哪一侧(向上或向下)应该获胜。否则,您只会收到一个始终为 0 的平均值。

Action as 1D Axis - tell Unity which side wins

另一个更好的选择是让值通过并添加“绑定(bind)”。 enter image description here

enter image description here

请注意,在大多数系统上默认的滚动值是 120,但在 Linux 上它似乎是 1。因此我建议简单地检查该值是大于还是小于 0 来决定它是向上滚动还是向下滚动。

float z = zoom.ReadValue<float>();
if (z > 0)
    Debug.Log("Scroll UP");
else if (z < 0)
    Debug.Log("Scroll DOWN");

关于c# - 使用 Unity 的新输入系统检测鼠标滚轮滚动输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66543338/

相关文章:

c# - 使用 Linq to sql 将 List<String> 插入到 ntext 字段

c# - JSON.NET 干扰 MSChart 日期

android - 显示自定义排行榜 UI

c# - 新的统一输入系统中 GetKey 的等价物是什么?

c++ - 编写一个接受用户输入并应该定位输入文件的 C 程序?

c# - 如何使用 DataAnnotations 和 Regex - c# - ASP.NET Core

c# - 我应该为此使用哪种设计模式?

c# - 在使用 Mono/Unity3D 的 Android 设备上使用 RabbitMQ 的问题

android - 在某个事件发生后,如何通过我自己的应用程序打开和使用另一个 Android 应用程序?

c++ - 在这种情况下如何从结构中输入字符串数组?