using System.Collections; using System.Collections.Generic; using UnityEngine; public class InputManager : Singleton { private List m_CommandRecord; private readonly int kCommandRecords = 10; // 本帧内的指令,会在下一帧被清空 private Command m_CurrentCommand; public Command CurrentCommand { get { return m_CurrentCommand; } } public void Init() { m_CommandRecord = new List(); m_CurrentCommand = new Command(GamepadButton.Blank, 0); } public void Update() { GamepadButton cmd = GamepadButton.Blank; // 移动 if (Input.GetKeyDown("w")) cmd = GamepadButton.Up; if (Input.GetKeyDown("s")) cmd = GamepadButton.Down; if (Input.GetKeyDown("a")) cmd = GamepadButton.Left; if (Input.GetKeyDown("d")) cmd = GamepadButton.Right; // 动作 if (Input.GetKeyDown("j")) cmd = GamepadButton.Circle; if (Input.GetKeyDown("k")) cmd = GamepadButton.Triangle; if (Input.GetKeyDown("l")) cmd = GamepadButton.Square; if(Input.GetKeyDown("u")) cmd = GamepadButton.Cross; if(cmd != GamepadButton.Blank) { float time = Time.time; Command command = new Command(cmd, time); //Debug.Log(CommandToString(command)); m_CurrentCommand = command; m_CommandRecord.Add(command); if(m_CommandRecord.Count > 10) m_CommandRecord.RemoveRange(0, m_CommandRecord.Count - 10); } else if(m_CurrentCommand.code != GamepadButton.Blank) { m_CurrentCommand = Command.Blank; } } string CommandCodeToString(GamepadButton cmd) { switch(cmd) { case GamepadButton.Left: return "←"; case GamepadButton.Right: return "→"; case GamepadButton.Up: return "↑"; case GamepadButton.Down: return "↓"; case GamepadButton.Circle: return "○"; case GamepadButton.Triangle: return "△"; case GamepadButton.Square: return "□"; case GamepadButton.Cross: return "×"; default: return "Unknown"; } } string CommandToString(Command cmd) { string sign = CommandCodeToString(cmd.code); return sign + " " + cmd.time + "s" + " " + cmd.id; } string GetGamepadButtonKey(GamepadButton button) { switch (button) { case GamepadButton.Blank: return ""; case GamepadButton.Up: return "w"; case GamepadButton.Down: return "s"; case GamepadButton.Left: return "a"; case GamepadButton.Right: return "d"; case GamepadButton.Circle: return "j"; case GamepadButton.Triangle: return "k"; case GamepadButton.Square: return "l"; case GamepadButton.Cross: return "u"; } return ""; } public bool IsButtonHold(GamepadButton button) { return Input.GetKey(GetGamepadButtonKey(button)); } }