帧同步之CustomUnityInput

自定义键位输入系统

using System;
using System.Collections.Generic;
using Testbed.Abstractions;
using UnityEditor;
using UnityEngine.InputSystem;
using UMouseButton = UnityEngine.UIElements.MouseButton;
using UModifiers = UnityEngine.EventModifiers;

  public interface IInput
    {
        bool IsKeyDown(KeyCodes key);

        bool IsKeyPressed(KeyCodes key);

        bool IsKeyUp(KeyCodes key);

        bool IsMouseDown(MouseButton button);

        bool IsMousePressed(MouseButton button);

        bool IsMouseUp(MouseButton button);
    }

    public class UnityInput : IInput
    {
        public Keyboard Keyboard;

        public Mouse Mouse;

        public UnityInput()
        {
            SetupKeyCodeMap();
            SetupUnityKeyCodeMap();
            if (Keyboard.current != null)
            {
                Keyboard = Keyboard.current;
            }

            if (Mouse.current != null)
            {
                Mouse = Mouse.current;
            }
        }

        public void UpdateDevice()
        {
            if (Keyboard.current != null)
            {
                Keyboard = Keyboard.current;
            }

            if (Mouse.current != null)
            {
                Mouse = Mouse.current;
            }
        }

        /// <inheritdoc />
        public bool IsKeyDown(KeyCodes key)
        {
            if (Keyboard == null)
            {
                return false;
            }

            return Keyboard.current[KeyCodeMap[key]].wasPressedThisFrame;
        }

        /// <inheritdoc />
        public bool IsKeyPressed(KeyCodes key)
        {
            if (Keyboard == null)
            {
                return false;
            }

            return Keyboard.current[KeyCodeMap[key]].isPressed;
        }

        /// <inheritdoc />
        public bool IsKeyUp(KeyCodes key)
        {
            if (Keyboard == null)
            {
                return false;
            }

            return Keyboard.current[KeyCodeMap[key]].wasReleasedThisFrame;
        }

        /// <inheritdoc />
        public bool IsMouseDown(MouseButton button)
        {
            if (Mouse == null)
            {
                return false;
            }

            switch (button)
            {
            case MouseButton.Left:
                return Mouse.current.leftButton.wasPressedThisFrame;
            case MouseButton.Right:
                return Mouse.current.rightButton.wasPressedThisFrame;
            case MouseButton.Middle:
                return Mouse.current.middleButton.wasPressedThisFrame;
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值