自定义键位输入系统
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;