当我将鼠标锁定在C#脚本中并且尝试访问Scroll值(在我的情况下,scroll.y
)时,我总是得到0。
当我按下esc
并解锁鼠标时,使用滚轮时,该值正确显示(-1或1)。如果我制作游戏,它也可以工作。
我已经创建了一个InputData来管理我的输入:
using UnityEngine;
using UnityEngine.InputSystem;
[CreateAssetMenu]
public class InputData : ScriptableObject
{
public InputAction MovementAction;
public InputAction LookAction;
public InputAction ScrollAction;
private void OnEnable()
{
MovementAction.Enable();
LookAction.Enable();
ScrollAction.Enable();
}
private void OnDisable()
{
MovementAction.Disable();
LookAction.Disable();
ScrollAction.Disable();
}
}
在此脚本中,我获得了滚动增量:
using UnityEngine;
public class weaponSwitching : MonoBehaviour
{
private int selectedWeapon = 0;
public InputData actions;
private float prevVal = 0;
void Start()
{
SelectWeapon();
}
void Update()
{
Vector2 scroll = actions.ScrollAction.ReadValue<Vector2>();
Debug.Log(scroll.y);
if (scroll.y > 0f)
{
selectedWeapon++;
SelectWeapon();
}
else if (scroll.y < 0f)
{
}
}
谢谢您的帮助! :)