Skip to content

Instantly share code, notes, and snippets.

@InfiniteAmmoInc
Last active August 29, 2015 13:57
Show Gist options
  • Save InfiniteAmmoInc/9352467 to your computer and use it in GitHub Desktop.
Save InfiniteAmmoInc/9352467 to your computer and use it in GitHub Desktop.
/*
InputX for Unity by Alec Holowka
WARNING: Work-in-progress!
http://InfiniteAmmo.com - @infinite_ammo
*/
/*
Note: use this code in an editor script to initialize the InputManager asset for use by InputX
[MenuItem("X/Setup InputManager Asset")]
static void SetupInputManagerAsset()
{
var inputManagerAsset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0];
var serializedObject = new SerializedObject(inputManagerAsset);
var axisArray = serializedObject.FindProperty("m_Axes");
axisArray.arraySize = 100;
serializedObject.ApplyModifiedProperties();
int axisIndex = 0;
for (int joystick = 1; joystick <= 10; joystick++)
{
for (int analog = 0; analog <= 9; analog++)
{
var axis = axisArray.GetArrayElementAtIndex(axisIndex++);
GetChildProperty(axis, "m_Name").stringValue = string.Format("joystick {0} analog {1}", joystick, analog);
GetChildProperty(axis, "descriptiveName").stringValue = "";
GetChildProperty(axis, "descriptiveNegativeName").stringValue = "";
GetChildProperty(axis, "negativeButton").stringValue = "";
GetChildProperty(axis, "positiveButton").stringValue = "";
GetChildProperty(axis, "altNegativeButton").stringValue = "";
GetChildProperty(axis, "altPositiveButton").stringValue = "";
GetChildProperty(axis, "gravity").floatValue = 10.0f;
GetChildProperty(axis, "dead").floatValue = 0.001f;
GetChildProperty(axis, "sensitivity").floatValue = 1.0f;
GetChildProperty(axis, "snap").boolValue = false;
GetChildProperty(axis, "invert").boolValue = false;
GetChildProperty(axis, "type").intValue = 2;
GetChildProperty(axis, "axis").intValue = analog;
GetChildProperty(axis, "joyNum").intValue = joystick;
}
}
serializedObject.ApplyModifiedProperties();
EditorUtility.DisplayDialog("Success", "InputManager asset has been initialized.", "OK");
}
static SerializedProperty GetChildProperty(SerializedProperty parent, string name)
{
SerializedProperty child = parent.Copy();
child.Next(true);
do
{
if (child.name == name)
return child;
}
while (child.Next(false));
return null;
}
*/
using UnityEngine;
using System.Collections;
public class InputX : MonoBehaviour
{
public enum ButtonCode
{
FaceA, // PS4: Square Xbox360: X
FaceB, // PS4: X XBox360: B
FaceX, // PS4: Cricle XBox360: A
FaceY, // PS4: Triangle XBox360: Y
LeftBumper,
RightBumper,
LeftStickButton,
RightStickButton,
Start,
}
public enum AxisCode
{
LeftStickX,
LeftStickY,
RightStickX,
RightStickY,
LeftTrigger,
RightTrigger,
DPadX,
DPadY,
}
[System.Serializable]
public class ButtonEntry
{
public string name;
public ButtonCode[] buttonCodes;
public KeyCode[] keyCodes;
}
[System.Serializable]
public class AxisEntry
{
public string name;
public AxisCode[] axisCodes;
public KeyCode[] keyCodes;
}
public bool diagnosticMode;
public bool invertY;
public float deadZone;
public ButtonEntry[] buttonEntries;
public AxisEntry[] axisEntries;
static InputX instance;
enum Platform
{
Unknown,
Windows,
Mac,
}
static Platform platform;
static KeyCode faceAKeyCode;
static KeyCode faceBKeyCode;
static KeyCode faceXKeyCode;
static KeyCode faceYKeyCode;
static KeyCode leftBumperKeyCode;
static KeyCode rightBumperKeyCode;
static KeyCode leftStickButtonKeyCode;
static KeyCode rightStickButtonKeyCode;
static KeyCode startKeyCode;
static string leftStickXName;
static string leftStickYName;
static string rightStickXName;
static string rightStickYName;
static string triggerLeftName;
static string triggerRightName;
static string dPadXName;
static string dPadYName;
public enum ControllerType
{
XBox360,
PS4,
SNESBuffalo,
}
void Awake()
{
if (instance)
{
Destroy(gameObject);
return;
}
instance = this;
if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer)
{
platform = Platform.Mac;
ControllerType controllerType = ControllerType.XBox360;
string[] names = Input.GetJoystickNames();
foreach (string name in names)
{
Debug.Log("joystick name: " + name);
if (name == "Sony Computer Entertainment Wireless Controller")
{
controllerType = ControllerType.PS4;
}
else if (name.IndexOf("USB,2-axis 8-button gamepad") != -1)
{
controllerType = ControllerType.SNESBuffalo;
}
}
switch (controllerType)
{
case ControllerType.SNESBuffalo:
faceAKeyCode = KeyCode.JoystickButton1;
faceBKeyCode = KeyCode.JoystickButton0;
faceXKeyCode = KeyCode.JoystickButton3;
faceYKeyCode = KeyCode.JoystickButton2;
leftBumperKeyCode = KeyCode.JoystickButton4;
rightBumperKeyCode = KeyCode.JoystickButton5;
leftStickButtonKeyCode = KeyCode.JoystickButton11;
rightStickButtonKeyCode = KeyCode.JoystickButton12;
break;
case ControllerType.PS4:
faceAKeyCode = KeyCode.JoystickButton1;
faceBKeyCode = KeyCode.JoystickButton2;
faceXKeyCode = KeyCode.JoystickButton0;
faceYKeyCode = KeyCode.JoystickButton3;
leftBumperKeyCode = KeyCode.JoystickButton4;
rightBumperKeyCode = KeyCode.JoystickButton5;
leftStickButtonKeyCode = KeyCode.JoystickButton11;
rightStickButtonKeyCode = KeyCode.JoystickButton12;
startKeyCode = KeyCode.JoystickButton13;
break;
case ControllerType.XBox360:
faceAKeyCode = KeyCode.JoystickButton16;
faceBKeyCode = KeyCode.JoystickButton17;
faceXKeyCode = KeyCode.JoystickButton18;
faceYKeyCode = KeyCode.JoystickButton19;
leftBumperKeyCode = KeyCode.JoystickButton13;
rightBumperKeyCode = KeyCode.JoystickButton14;
leftStickButtonKeyCode = KeyCode.JoystickButton11;
rightStickButtonKeyCode = KeyCode.JoystickButton12;
startKeyCode = KeyCode.JoystickButton9;
break;
}
leftStickXName = "joystick 1 analog 0";
leftStickYName = "joystick 1 analog 1";
rightStickXName = "joystick 1 analog 2";
rightStickYName = "joystick 1 analog 3";
triggerLeftName = "joystick 1 analog 4";
triggerRightName = "joystick 1 analog 5";
dPadXName = "joystick 1 analog 6";
dPadYName = "joystick 1 analog 7";
}
else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
{
platform = Platform.Windows;
faceAKeyCode = KeyCode.JoystickButton0;
faceBKeyCode = KeyCode.JoystickButton1;
faceXKeyCode = KeyCode.JoystickButton2;
faceYKeyCode = KeyCode.JoystickButton3;
leftBumperKeyCode = KeyCode.JoystickButton4;
rightBumperKeyCode = KeyCode.JoystickButton5;
leftStickButtonKeyCode = KeyCode.JoystickButton8;
rightStickButtonKeyCode = KeyCode.JoystickButton9;
startKeyCode = KeyCode.JoystickButton7;
leftStickXName = "joystick 1 analog 0";
leftStickYName = "joystick 1 analog 1";
rightStickXName = "joystick 1 analog 3";
rightStickYName = "joystick 1 analog 4";
triggerLeftName = "joystick 1 analog 2";
triggerRightName = "joystick 1 analog 2";
dPadXName = "joystick 1 analog 6";
dPadYName = "joystick 1 analog 7";
}
}
public static bool GetButton(ButtonCode buttonCode)
{
switch (buttonCode)
{
case ButtonCode.FaceA:
return Input.GetKey(faceAKeyCode);
case ButtonCode.FaceB:
return Input.GetKey(faceBKeyCode);
case ButtonCode.FaceX:
return Input.GetKey(faceXKeyCode);
case ButtonCode.FaceY:
return Input.GetKey(faceYKeyCode);
case ButtonCode.LeftBumper:
return Input.GetKey(leftBumperKeyCode);
case ButtonCode.RightBumper:
return Input.GetKey(rightBumperKeyCode);
case ButtonCode.LeftStickButton:
return Input.GetKey(leftStickButtonKeyCode);
case ButtonCode.RightStickButton:
return Input.GetKey(rightStickButtonKeyCode);
case ButtonCode.Start:
return Input.GetKey(startKeyCode);
}
return false;
}
public static bool GetButtonDown(ButtonCode buttonCode)
{
switch (buttonCode)
{
case ButtonCode.FaceA:
return Input.GetKeyDown(faceAKeyCode);
case ButtonCode.FaceB:
return Input.GetKeyDown(faceBKeyCode);
case ButtonCode.FaceX:
return Input.GetKeyDown(faceXKeyCode);
case ButtonCode.FaceY:
return Input.GetKeyDown(faceYKeyCode);
case ButtonCode.LeftBumper:
return Input.GetKeyDown(leftBumperKeyCode);
case ButtonCode.RightBumper:
return Input.GetKeyDown(rightBumperKeyCode);
case ButtonCode.LeftStickButton:
return Input.GetKeyDown(leftStickButtonKeyCode);
case ButtonCode.RightStickButton:
return Input.GetKeyDown(rightStickButtonKeyCode);
case ButtonCode.Start:
return Input.GetKeyDown(startKeyCode);
}
return false;
}
public static bool GetButtonUp(ButtonCode buttonCode)
{
switch (buttonCode)
{
case ButtonCode.FaceA:
return Input.GetKeyUp(faceAKeyCode);
case ButtonCode.FaceB:
return Input.GetKeyUp(faceBKeyCode);
case ButtonCode.FaceX:
return Input.GetKeyUp(faceXKeyCode);
case ButtonCode.FaceY:
return Input.GetKeyUp(faceYKeyCode);
case ButtonCode.LeftBumper:
return Input.GetKeyUp(leftBumperKeyCode);
case ButtonCode.RightBumper:
return Input.GetKeyUp(rightBumperKeyCode);
case ButtonCode.LeftStickButton:
return Input.GetKeyUp(leftStickButtonKeyCode);
case ButtonCode.RightStickButton:
return Input.GetKeyUp(rightStickButtonKeyCode);
case ButtonCode.Start:
return Input.GetKeyUp(startKeyCode);
}
return false;
}
public static float GetAxis(AxisCode axisCode)
{
switch (axisCode)
{
case AxisCode.LeftStickX:
return Input.GetAxis(leftStickXName);
case AxisCode.LeftStickY:
return Input.GetAxis(leftStickYName) * (instance.invertY?-1f:1f);
case AxisCode.RightStickX:
return Input.GetAxis(rightStickXName);
case AxisCode.RightStickY:
return Input.GetAxis(rightStickYName) * (instance.invertY?-1f:1f);
case AxisCode.LeftTrigger:
{
if (platform == Platform.Mac)
return (Input.GetAxis(triggerLeftName) + 1f) / 2f;
else if (platform == Platform.Windows)
{
float v = Input.GetAxis(triggerLeftName);
if (v > 0f)
return v;
}
else
return Input.GetAxis(triggerLeftName);
}
break;
case AxisCode.RightTrigger:
{
if (platform == Platform.Mac)
return (Input.GetAxis(triggerRightName) + 1f) / 2f;
else if (platform == Platform.Windows)
{
float v = Input.GetAxis(triggerRightName);
if (v < 0f)
return -v;
}
else
return Input.GetAxis(triggerRightName);
}
break;
case AxisCode.DPadX:
return Input.GetAxis(dPadXName);
case AxisCode.DPadY:
return Input.GetAxis(dPadYName) * (instance.invertY?-1f:1f);
}
return 0f;
}
public static bool GetButtonDown(string buttonName)
{
ButtonEntry buttonEntry = instance.GetButtonEntryByName(buttonName);
if (buttonEntry != null)
{
foreach (KeyCode keyCode in buttonEntry.keyCodes)
{
if (Input.GetKeyDown(keyCode))
{
return true;
}
}
foreach (ButtonCode buttonCode in buttonEntry.buttonCodes)
{
if (GetButtonDown(buttonCode))
{
return true;
}
}
}
return false;
}
public static bool GetButtonUp(string buttonName)
{
ButtonEntry buttonEntry = instance.GetButtonEntryByName(buttonName);
if (buttonEntry != null)
{
foreach (KeyCode keyCode in buttonEntry.keyCodes)
{
if (Input.GetKeyUp(keyCode))
{
return true;
}
}
foreach (ButtonCode buttonCode in buttonEntry.buttonCodes)
{
if (GetButtonUp(buttonCode))
{
return true;
}
}
}
return false;
}
public static bool GetButton(string buttonName)
{
ButtonEntry buttonEntry = instance.GetButtonEntryByName(buttonName);
if (buttonEntry != null)
{
foreach (KeyCode keyCode in buttonEntry.keyCodes)
{
if (Input.GetKey(keyCode))
{
return true;
}
}
foreach (ButtonCode buttonCode in buttonEntry.buttonCodes)
{
if (GetButton(buttonCode))
{
return true;
}
}
}
return false;
}
public static float GetAxis(string axisName)
{
float axisValue = 0f;
AxisEntry axisEntry = instance.GetAxisEntryByName(axisName);
if (axisEntry != null)
{
foreach (AxisCode axisCode in axisEntry.axisCodes)
{
axisValue += GetAxis(axisCode);
}
for (int i = 0; i < axisEntry.keyCodes.Length; i++)
{
if (Input.GetKey(axisEntry.keyCodes[i]))
{
axisValue += ((i % 2 == 0)?-1f:1f);
}
}
}
if (Mathf.Abs(axisValue) < instance.deadZone)
{
axisValue = 0f;
}
return axisValue;
}
public static float GetAxisRaw(string axisName)
{
return GetAxis(axisName);
}
ButtonEntry GetButtonEntryByName(string name)
{
foreach (ButtonEntry buttonEntry in buttonEntries)
{
if (buttonEntry.name == name)
{
return buttonEntry;
}
}
Debug.Log("Could not find ButtonEntry named: " + name);
return null;
}
AxisEntry GetAxisEntryByName(string name)
{
foreach (AxisEntry axisEntry in axisEntries)
{
if (axisEntry.name == name)
{
return axisEntry;
}
}
Debug.Log("Could not find AxisEntry named: " + name);
return null;
}
void Update()
{
if (diagnosticMode)
{
for (int i = 0; i < 16; i++)
{
if (Input.GetKeyDown((KeyCode)((int)KeyCode.JoystickButton0 + i)))
{
Debug.Log ("Button " + i);
}
}
for (int i = 0; i < 10; i++)
{
float axisValue = Input.GetAxisRaw("joystick 1 analog " + i);
if (Mathf.Abs(axisValue) > .1f)
{
Debug.LogWarning("joystick 1 analog " + i + ": " + axisValue);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment