Unity 基于Input实现多按键组合

时间:2024-02-29 18:47:11

Unity Input多按键组合

  1. 基于InputManager读取按键
  2. InputConfig存放KeyCode组合
  3. 以字典形式存放InputConfig
  4. 遍历字典访问KeyCode信息

InputConfig类

枚举Action方便获取KeyCode[]内容

namespace Configs 
{
   public class InputConfig 
   {
      public KeyCode[] keyCodes { get; private set; }

      public InputConfig(KeyCode[] keyCodes)
      {
         this.keyCodes = keyCodes;
      }
      
   }
	//KeyCode顺序与枚举顺序相同,方便阅读
   enum Action 
   {
      Up,
      Down,
      Left,
      Right,
      Jump,
   }

}

InputReader字典

配置InputConfig到字典中,并通过InputManager读取按键信息

public static class InputReader 
{
	static InputReader() 
	{
		KeyCode[] keyCodes = {
			KeyCode.W,
			KeyCode.S,
			KeyCode.A,
			KeyCode.D,
			KeyCode.LeftShift,
		};
		KeyCode[] keyCodes2 = {
			KeyCode.UpArrow,
			KeyCode.DownArrow,
			KeyCode.LeftArrow,
			KeyCode.RightArrow,
			KeyCode.Space,
		};
                //多套按键配置
		InputConfigDic.Add(0, new InputConfig(keyCodes));
		InputConfigDic.Add(1, new InputConfig(keyCodes2));
	}
    //按键按住相应
    private static bool CheckInput(Action index) 
	{
		foreach (var item in InputConfigDic.Values) 
		{
			if (Input.GetKey(item.keyCodes[(int)index])) {
				return true;
			}
		}
		return false;
	}	
        //按键按下相应
	private static bool CheckInputDown(Action index) 
	{
		foreach (var item in InputConfigDic.Values) 
		{
			if (Input.GetKeyDown(item.keyCodes[(int)index])) 
			{
				return true;
			}
		}
		return false;
	}
    //方向移动针对性方法
    //类似Input.GetAxisRaw()
    public static int HorizontalRaw 
	{
		get 
		{
			if (CheckInput(Action.Right))
				return 1;
			if (CheckInput(Action.Left))
				return -1;
			return 0;
		}
	}
        //垂直方向
	public static int VerticalRaw
	{
		get 
		{
			if (CheckInput(Action.Down))
				return -1;
			if (CheckInput(Action.Up))
				return 1;
			return 0;
		}
	}
}

特定行为按键

public static bool Up { get { return CheckInput(Action.Up); } }
public static bool Down { get { return CheckInput(Action.Down); } }
public static bool Left { get { return CheckInput(Action.Left); } }
public static bool Right { get { return CheckInput(Action.Right); } }
public static bool Jump { get { return CheckInputDown(Action.Jump); } }

完整代码

using Configs;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class InputReader 
{
   static InputReader() 
   {
       KeyCode[] keyCodes = {
           KeyCode.W,
           KeyCode.S,
           KeyCode.A,
           KeyCode.D,
           KeyCode.LeftShift,
       };
       KeyCode[] keyCodes2 = {
           KeyCode.UpArrow,
           KeyCode.DownArrow,
           KeyCode.LeftArrow,
           KeyCode.RightArrow,
           KeyCode.Space,
       };        
       //多套按键配置
       InputConfigDic.Add(0, new InputConfig(keyCodes));
       InputConfigDic.Add(1, new InputConfig(keyCodes2));
   }
    
   public static readonly Dictionary<int, InputConfig> InputConfigDic = new Dictionary<int, InputConfig>();
    
   public static bool Up => CheckInput(Action.Up);
   public static bool Down => CheckInput(Action.Down);
   public static bool Left => CheckInput(Action.Left);
   public static bool Right => CheckInput(Action.Right);
   public static bool Jump => CheckInputDown(Action.Jump);
   public static bool Run => CheckInput(Action.Run);
   public static bool Pull => CheckInput(Action.Pull);
   public static bool Crouch => CheckInput(Action.Crouch);
   public static bool InterActive => CheckInput(Action.InterActive);
   public static bool ChangeController => CheckInputDown(Action.ChangeController);

   public static int HorizontalRaw 
   {
      get 
      {
         if (CheckInput(Action.Right))
            return 1;
         if (CheckInput(Action.Left))
            return -1;
         return 0;
      }
   }
   public static int VerticalRaw
   {
      get 
      {
         if (CheckInput(Action.Down))
            return -1;
         if (CheckInput(Action.Up))
            return 1;
         return 0;
      }
   }

   private static bool CheckInput(Action index) 
   {
      foreach (var item in InputConfigDic.Values) 
      {
         if (Input.GetKey(item.keyCodes[(int)index])) {
            return true;
         }
      }
      return false;
   }  
   private static bool CheckInputDown(Action index) 
   {
      foreach (var item in InputConfigDic.Values) 
      {
         if (Input.GetKeyDown(item.keyCodes[(int)index])) 
         {
            return true;
         }
      }
      return false;
   }

}
namespace Configs 
{
   public class InputConfig 
   {
      public KeyCode[] keyCodes { get; private set; }

      public InputConfig(KeyCode[] keyCodes)
      {
         this.keyCodes = keyCodes;
      }
      
   }

   enum Action 
   {
      Up,
      Down,
      Left,
      Right,
      Jump,
   }

}