Unity InputSystem

最近迁移项目到UnityXR框架,发现UnityXR框架使用了新的输入系统(InputSystem)然后就学习了一下。

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


public class PlayerInputSys : MonoBehaviour
{
    Keyboard m_Keyboard = Keyboard.current;
    Mouse m_Mouse = Mouse.current;
    Pointer m_Pointer = Pointer.current;

    private void Update()
    {
        if(m_Keyboard !=null)
        {
            if (m_Keyboard.wKey.wasPressedThisFrame)
            {
                Debug.Log("按下了W键");
            }
            if (m_Keyboard.wKey.wasReleasedThisFrame)
            {
                Debug.Log("释放了W键");
            }

            Debug.Log("是否按住W键:" +m_Keyboard.wKey.IsPressed());
        }
        if(m_Mouse != null)
        {
            Debug.Log(m_Mouse.scroll.ReadValue());
            if(m_Mouse.leftButton.wasPressedThisFrame)
            {
                Debug.Log("按下了鼠标左键");
            }
            if(m_Mouse.rightButton.wasPressedThisFrame)
            {
                Debug.Log("按下了鼠标右键");
            }
            if (m_Mouse.middleButton.wasPressedThisFrame)
            {
                Debug.Log("按下了鼠标中间");
            }
        }
        if(m_Pointer !=null)
        {
            Debug.Log(m_Pointer.delta.ReadValue());//与上一帧的偏移量
            Debug.Log(m_Pointer.position.ReadValue());//鼠标所在的当前位置
        }
    }


}

 

上一篇:Python按键脚本


下一篇:javascript – 如何在A-Frame中反转拖动旋转?