Unity InputSystem

时间:2021-04-10 13:33:21   收藏:0   阅读:0

最近迁移项目到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());//鼠标所在的当前位置
        }
    }


}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!