Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- /// <summary>
- /// Simple enum to represent input types.
- /// </summary>
- public enum InputType
- {
- Keyboard = 0,
- XBox360 = 1,
- }
- /// <summary>
- /// This is the singleton manager for handling player input. It will be set
- /// to run before any other scripts that might utilize player input to make sure
- /// that input is available and up-to-date when queried.
- /// </summary>
- public class InputManager : MonoBehaviour
- {
- // Key states. These are intended to be used for inequalities with Down and Pressed.
- //
- // If a button is down, it should also be pressed.
- // If a button is pressed, it may not necessarily be down this frame, but cannot be up
- // If a button is up, it should not be down or pressed
- // If a button is at rest / default, it should be none of these.
- //
- // By this, if a key state is >= Down, and similarly for >= Pressed and Up
- //
- private const int KEY_DEFAULT = 0;
- private const int KEY_DOWN = 3;
- private const int KEY_PRESSED = 2;
- private const int KEY_UP = 1;
- // Deadzones for the axes. These are a bit repetitive as the Axis definitions within Unity's InputManager editor
- // have their own sensitivities and deadzones
- private const float DEADZONE_LEFT = 0.2f;
- private const float DEADZONE_RIGHT = 0.02f;
- // We'll keep track of which directions are being held
- private static Vector2 movementInput = Vector2.zero;
- private static Vector2 cameraInput = Vector2.zero;
- // We might be curious if movement was detected at all
- private static bool movementDetected = false;
- private static bool panningDetected = false;
- // Rather than mapping to a specific controller layout, the variables
- // here will be named after the intended functionality we intend to have
- // for whichever controller scheme the player is using.
- private static int stateSubmit = 0;
- private static int stateBack = 0;
- private static int stateCheck = 0;
- // Middle buttons on most controllers
- private static int stateStart = 0;
- private static int stateSelect = 0;
- // Active input
- private static InputType activeInput = InputType.Keyboard;
- /// <summary>
- /// The update for our InputManager is set to run before any other scripts
- /// via the project settings / Script Execution Order.
- /// </summary>
- void Update()
- {
- // Check if there's a controller active
- InputManager.ActiveInput = InputManager.CheckInputType();
- // We'll need to capture our input here
- switch (InputManager.ActiveInput)
- {
- default:
- case InputType.Keyboard:
- InputManager.UseKeyboardInput();
- break;
- case InputType.XBox360:
- InputManager.UseXBoxInput();
- break;
- }
- // Update our flags that will be used across platforms
- InputManager.movementDetected = (InputManager.movementInput.sqrMagnitude >= InputManager.DEADZONE_LEFT);
- InputManager.panningDetected = (InputManager.cameraInput.sqrMagnitude >= InputManager.DEADZONE_RIGHT);
- }
- /// <summary>
- /// Check what sort of input we're using. Assume that a 360 controller is the intended
- /// way of playing.
- /// </summary>
- /// <returns></returns>
- private static InputType CheckInputType()
- {
- // This returns an array of active joystick names, but will always have
- // a first element -- even if there isn't a controller plugged in.
- //
- string[] joysticks = Input.GetJoystickNames();
- // We'll check if "360" is here to indicate an xbox controller
- for (int k = 0; k < joysticks.Length; k++)
- {
- if (joysticks[k].Contains("360"))
- return InputType.XBox360;
- }
- // We'll fix this later
- return InputType.Keyboard;
- }
- /// <summary>
- /// Updates the input values with the keyboard layout.
- /// </summary>
- private static void UseKeyboardInput()
- {
- // Movement
- InputManager.movementInput.x = InputManager.KeyAxis(KeyCode.D, KeyCode.A);
- InputManager.movementInput.y = InputManager.KeyAxis(KeyCode.W, KeyCode.S);
- InputManager.movementInput.Normalize();
- // Camera Input
- //
- // For the keyboard layout, I don't want the mouse to always move the camera as
- // that will get annoying quickly as we learned with Dark Souls. The camera doesn't
- // always need to move, but we want it to behave similarly to map UIs.
- //
- if (Input.GetMouseButton(ControlCodes.MOUSE_RIGHT))
- {
- InputManager.cameraInput.x = Input.GetAxis("Mouse X");
- InputManager.cameraInput.y = Input.GetAxis("Mouse Y");
- }
- else
- {
- InputManager.cameraInput = Vector2.zero;
- }
- // Button actions
- InputManager.stateBack = InputManager.KeyState(KeyCode.Escape);
- InputManager.stateSubmit = InputManager.KeyState(KeyCode.E);
- InputManager.stateStart = InputManager.KeyState(KeyCode.Escape);
- InputManager.stateSelect = InputManager.KeyState(KeyCode.Tab);
- }
- /// <summary>
- /// Updates input values with the XBox 360 controller layout.
- /// </summary>
- private static void UseXBoxInput()
- {
- // Since the XBox 360 buttons are bound to different keycodes across platforms,
- // we need to check compile directives when mapping these.
- InputManager.movementInput.x = Input.GetAxis(InputNames.LEFT_AXIS_X);
- InputManager.movementInput.y = Input.GetAxis(InputNames.LEFT_AXIS_Y);
- #if UNITY_STANDALONE_WIN
- InputManager.cameraInput.x = Input.GetAxis(InputNames.RIGHT_AXIS_X_WINDOWS);
- InputManager.cameraInput.y = Input.GetAxis(InputNames.RIGHT_AXIS_Y_WINDOWS);
- InputManager.stateBack = InputManager.KeyState(ControlCodes.XBOX_WIN_B);
- InputManager.stateSubmit = InputManager.KeyState(ControlCodes.XBOX_WIN_A);
- InputManager.stateStart = InputManager.KeyState(ControlCodes.XBOX_WIN_START);
- InputManager.stateSelect = InputManager.KeyState(ControlCodes.XBOX_WIN_BACK);
- #endif
- #if UNITY_STANDALONE_OSX
- InputManager.cameraInput.x = Input.GetAxis(InputNames.RIGHT_AXIS_X_OSX);
- InputManager.cameraInput.y = Input.GetAxis(InputNames.RIGHT_AXIS_Y_OSX);
- InputManager.stateBack = InputManager.KeyState(ControlCodes.XBOX_OSX_B);
- InputManager.stateSubmit = InputManager.KeyState(ControlCodes.XBOX_OSX_A);
- InputManager.stateStart = InputManager.KeyState(ControlCodes.XBOX_OSX_START);
- InputManager.stateSelect = InputManager.KeyState(ControlCodes.XBOX_OSX_BACK);
- #endif
- #if UNITY_STANDALONE_LINUX
- InputManager.cameraInput.x = Input.GetAxis(InputNames.RIGHT_AXIS_X_LINUX);
- InputManager.cameraInput.y = Input.GetAxis(InputNames.RIGHT_AXIS_Y_LINUX);
- InputManager.stateBack = InputManager.KeyState(ControlCodes.XBOX_LINUX_B);
- InputManager.stateSubmit = InputManager.KeyState(ControlCodes.XBOX_LINUX_A);
- InputManager.stateStart = InputManager.KeyState(ControlCodes.XBOX_LINUX_START);
- InputManager.stateSelect = InputManager.KeyState(ControlCodes.XBOX_LINUX_BACK);
- #endif
- }
- /// <summary>
- /// Simulates a float-style axis using button inputs.
- /// </summary>
- /// <param name="positive"></param>
- /// <param name="negative"></param>
- /// <returns></returns>
- public static float KeyAxis(KeyCode positive, KeyCode negative)
- {
- int p = Input.GetKey(positive) ? 1 : 0;
- int b = Input.GetKey(negative) ? 1 : 0;
- return p - b;
- }
- /// <summary>
- /// Returns an integer indicating the state of this key. This can also be used to get controller
- /// inputs, provided the correct enumerated joystick button is used (i.e. KeyCode.Joystick1Button8)
- ///
- /// 0 == Not Pressed or UP this frame
- /// 1 == PRESSED this frame
- /// 2 == PRESSED
- /// 3 == UP this frame
- ///
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- private static int KeyState(KeyCode key)
- {
- if (Input.GetKeyDown(key))
- return InputManager.KEY_DOWN;
- else if (Input.GetKey(key))
- return InputManager.KEY_PRESSED;
- else if (Input.GetKeyUp(key))
- return InputManager.KEY_UP;
- else
- return InputManager.KEY_DEFAULT;
- }
- /// <summary>
- /// Returns the normalized Vector3 of player movement. This will return the zero vector
- /// if no movement was detected.
- /// </summary>
- public static Vector3 MovementInput
- {
- get { return movementInput; }
- }
- /// <summary>
- /// Returns whether or not the player is using the movement axis.
- /// </summary>
- public static bool MovementDetected
- {
- get { return movementDetected; }
- }
- /// <summary>
- /// Returns the normalized Vector3 of player movement. This will return the zero vector
- /// if no movement was detected.
- /// </summary>
- public static Vector3 CameraInput
- {
- get { return cameraInput; }
- }
- /// <summary>
- /// Returns whether or not the player attempted to pan the camera.
- /// </summary>
- public static bool PanningDetected
- {
- get { return panningDetected; }
- }
- /// <summary>
- /// Gets the active input method currently detected.
- /// </summary>
- public static InputType ActiveInput
- {
- get { return InputManager.activeInput; }
- private set
- {
- if (InputManager.activeInput != value)
- {
- InputManager.activeInput = value;
- InputManager.OnActiveInputChanged();
- }
- }
- }
- /// <summary>
- /// Called when the user's input method has changed.
- /// </summary>
- private static void OnActiveInputChanged()
- {
- // Just call it for now, eventually we'll change the scriptable objects
- // determining their control scheme.
- }
- /// <summary>
- /// Helper values to keep information about how Unity maps the various controller
- /// inputs to its Input system.
- /// </summary>
- public static class ControlCodes
- {
- // Mouse
- public const int MOUSE_LEFT = 0;
- public const int MOUSE_RIGHT = 1;
- public const int MOUSE_MIDDLE = 2;
- // Windows
- public const KeyCode XBOX_WIN_A = KeyCode.Joystick1Button0;
- public const KeyCode XBOX_WIN_B = KeyCode.Joystick1Button1;
- public const KeyCode XBOX_WIN_X = KeyCode.Joystick1Button2;
- public const KeyCode XBOX_WIN_Y = KeyCode.Joystick1Button3;
- public const KeyCode XBOX_WIN_LB = KeyCode.Joystick1Button4;
- public const KeyCode XBOX_WIN_RB = KeyCode.Joystick1Button5;
- public const KeyCode XBOX_WIN_BACK = KeyCode.Joystick1Button6;
- public const KeyCode XBOX_WIN_START = KeyCode.Joystick1Button7;
- public const KeyCode XBOX_WIN_LEFT_STICK_CLICK = KeyCode.Joystick1Button8;
- public const KeyCode XBOX_WIN_RIGHT_STICK_CLICK = KeyCode.Joystick1Button9;
- // OSX
- public const KeyCode XBOX_OSX_A = KeyCode.Joystick1Button16;
- public const KeyCode XBOX_OSX_B = KeyCode.Joystick1Button17;
- public const KeyCode XBOX_OSX_X = KeyCode.Joystick1Button18;
- public const KeyCode XBOX_OSX_Y = KeyCode.Joystick1Button19;
- public const KeyCode XBOX_OSX_LB = KeyCode.Joystick1Button13;
- public const KeyCode XBOX_OSX_RB = KeyCode.Joystick1Button14;
- public const KeyCode XBOX_OSX_BACK = KeyCode.Joystick1Button10;
- public const KeyCode XBOX_OSX_START = KeyCode.Joystick1Button9;
- public const KeyCode XBOX_OSX_LEFT_STICK_CLICK = KeyCode.Joystick1Button11;
- public const KeyCode XBOX_OSX_RIGHT_STICK_CLICK = KeyCode.Joystick1Button12;
- // Linux
- public const KeyCode XBOX_LINUX_A = KeyCode.Joystick1Button0;
- public const KeyCode XBOX_LINUX_B = KeyCode.Joystick1Button1;
- public const KeyCode XBOX_LINUX_X = KeyCode.Joystick1Button2;
- public const KeyCode XBOX_LINUX_Y = KeyCode.Joystick1Button3;
- public const KeyCode XBOX_LINUX_LB = KeyCode.Joystick1Button4;
- public const KeyCode XBOX_LINUX_RB = KeyCode.Joystick1Button5;
- public const KeyCode XBOX_LINUX_BACK = KeyCode.Joystick1Button6;
- public const KeyCode XBOX_LINUX_START = KeyCode.Joystick1Button7;
- public const KeyCode XBOX_LINUX_LEFT_STICK_CLICK = KeyCode.Joystick1Button9;
- public const KeyCode XBOX_LINUX_RIGHT_STICK_CLICK = KeyCode.Joystick1Button10;
- }
- /// <summary>
- /// Helper values to standardize what we've named certain things in the editor's InputManager.
- ///
- /// As Axes are the only thing that still need manual configuration, most of these values will
- /// be names of the axes.
- /// </summary>
- public static class InputNames
- {
- // Axes
- public const string RIGHT_AXIS_X_WINDOWS = "Right X Windows";
- public const string RIGHT_AXIS_Y_WINDOWS = "Right Y Windows";
- public const string RIGHT_AXIS_X_OSX = "Right X OSX";
- public const string RIGHT_AXIS_Y_OSX = "Right Y OSX";
- public const string RIGHT_AXIS_X_LINUX = "Right X Linux";
- public const string RIGHT_AXIS_Y_LINUX = "Right Y Linux";
- // The left stick is always the same, so it only needs one entry in the InputManager.asset
- public const string LEFT_AXIS_X = "Left X";
- public const string LEFT_AXIS_Y = "Left Y";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement