Skip to content

Instantly share code, notes, and snippets.

@mildmojo
Last active August 29, 2015 14:08
Show Gist options
  • Save mildmojo/027cde9ca4663981cf67 to your computer and use it in GitHub Desktop.
Save mildmojo/027cde9ca4663981cf67 to your computer and use it in GitHub Desktop.
Bolt-on Lexitron arcade cabinet compatibility for Unity
/* LexitronCreateAxes.cs
*
* 1. Place this script in your project's Assets/Editor/ directory.
* 2. Open Edit -> Project Settings -> Input and verify that there are four "Lexitron" axes.
* 3. Delete this script.
*
* All of this code shamelessly swiped from:
* http://www.plyoung.com/blog/manipulating-input-manager-in-script.html
* http://answers.unity3d.com/questions/26994/running-a-script-when-unity-starts.html
*/
using UnityEditor;
public enum AxisType {
KeyOrMouseButton = 0,
MouseMovement = 1,
JoystickAxis = 2
};
public class InputAxis {
public string name;
public string descriptiveName;
public string descriptiveNegativeName;
public string negativeButton;
public string positiveButton;
public string altNegativeButton;
public string altPositiveButton;
public float gravity;
public float dead;
public float sensitivity;
public bool snap = false;
public bool invert = false;
public AxisType type;
public int axis;
public int joyNum;
}
[InitializeOnLoad]
public class SetupLexitronInputs {
static void SetupLexitronInputs() {
AddAxis(new InputAxis() {
name = "Lexitron Stick 1 Horizontal",
type = AxisType.JoystickAxis,
joyNum = 1,
axis = 6,
sensitivity = 1f,
dead = 0.19f
});
AddAxis(new InputAxis() {
name = "Lexitron Stick 1 Vertical",
type = AxisType.JoystickAxis,
joyNum = 1,
axis = 7,
sensitivity = 1f,
dead = 0.19f
});
AddAxis(new InputAxis() {
name = "Lexitron Stick 2 Horizontal",
type = AxisType.JoystickAxis,
joyNum = 2,
axis = 6,
sensitivity = 1f,
dead = 0.19f
});
AddAxis(new InputAxis() {
name = "Lexitron Stick 2 Vertical",
type = AxisType.JoystickAxis,
joyNum = 2,
axis = 7,
sensitivity = 1f,
dead = 0.19f
});
Debug.Log("Lexitron joystick axes created! See new entries in Edit -> Project Settings -> Input.");
}
static void AddAxis(InputAxis axis) {
if (AxisDefined(axis.name)) return;
SerializedObject serializedObject = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0]);
SerializedProperty axesProperty = serializedObject.FindProperty("m_Axes");
axesProperty.arraySize++;
serializedObject.ApplyModifiedProperties();
SerializedProperty axisProperty = axesProperty.GetArrayElementAtIndex(axesProperty.arraySize - 1);
GetChildProperty(axisProperty, "m_Name").stringValue = axis.name;
GetChildProperty(axisProperty, "descriptiveName").stringValue = axis.descriptiveName;
GetChildProperty(axisProperty, "descriptiveNegativeName").stringValue = axis.descriptiveNegativeName;
GetChildProperty(axisProperty, "negativeButton").stringValue = axis.negativeButton;
GetChildProperty(axisProperty, "positiveButton").stringValue = axis.positiveButton;
GetChildProperty(axisProperty, "altNegativeButton").stringValue = axis.altNegativeButton;
GetChildProperty(axisProperty, "altPositiveButton").stringValue = axis.altPositiveButton;
GetChildProperty(axisProperty, "gravity").floatValue = axis.gravity;
GetChildProperty(axisProperty, "dead").floatValue = axis.dead;
GetChildProperty(axisProperty, "sensitivity").floatValue = axis.sensitivity;
GetChildProperty(axisProperty, "snap").boolValue = axis.snap;
GetChildProperty(axisProperty, "invert").boolValue = axis.invert;
GetChildProperty(axisProperty, "type").intValue = (int)axis.type;
GetChildProperty(axisProperty, "axis").intValue = axis.axis - 1;
GetChildProperty(axisProperty, "joyNum").intValue = axis.joyNum;
serializedObject.ApplyModifiedProperties();
}
static bool AxisDefined(string axisName) {
SerializedObject serializedObject = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0]);
SerializedProperty axesProperty = serializedObject.FindProperty("m_Axes");
axesProperty.Next(true);
axesProperty.Next(true);
while (axesProperty.Next(false)) {
SerializedProperty axis = axesProperty.Copy();
axis.Next(true);
if (axis.stringValue == axisName) return true;
}
return false;
}
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;
}
}
/* LexitronManager.cs
*
* https://gist.github.com/mildmojo/027cde9ca4663981cf67
*
* Lexitron arcade cabinet compatibility for your game... in a can!
*
* Attach this script to any GameObject in your scene, preferably
* wherever you're attaching the rest of your global game state
* manager scripts. Either make sure the script is on an object in
* every scene or make the object with this script attached survive
* scene changes by calling DontDestroyOnLoad on it:
* http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
*
* Start the game with the "-lexitron" command line argument to switch
* on the following Lexitron-specific behaviors:
*
* - 1920x1080 fullscreen
* - Hides mouse
* - Coin insert '=' broadcasts OnCoinInsert (calls OnCoinInsert methods in any of your scripts automatically)
* - LEX button tap '[' broadcasts OnLexButton (calls OnLexButton methods in any of your scripts automatically)
* - LEX button long-press ']' quits
* - Quits after 2 minutes of inactivity (no sticks/buttons/keys triggered)
*
* Remember to listen for joystick movement on the D-pad (axes 6 and 7).
* Check Edit -> Project Settings -> Input for the names of the
* Lexitron-specific joystick axes you can use with `Input.GetAxis()`.
*
* See the full dev guidelines at: http://runjumpdev.org/lexitron-game-guidelines
*/
using UnityEngine;
using System;
using System.Linq;
public class LexitronManager : MonoBehaviour {
[HideInInspector] [NonSerialized]
public int InactivityTimeout = 120;
[HideInInspector]
public readonly string[] LEXITRON_AXES = {
"Lexitron Stick 1 Horizontal",
"Lexitron Stick 1 Vertical",
"Lexitron Stick 2 Horizontal",
"Lexitron Stick 2 Vertical"
};
private float timerInactivity;
public void ResetInactivityTimer() {
timerInactivity = 0f;
}
void Awake () {
// Enable this component if the "-lexitron" command line argument was given.
this.enabled = Environment.GetCommandLineArgs().Any(arg => arg.Equals("-lexitron"));
// If this isn't the Lexitron, give up.
if (!this.enabled) return;
// Start the inactivity timer.
timerInactivity = 0f;
// Hide the mouse cursor.
Screen.showCursor = false;
// Switch to full screen 1080p if not already there.
if (Screen.width != 1920 || Screen.height != 1080) {
Debug.Log("Going fullscreen at 1080p...");
Screen.SetResolution(1920, 1080, true);
}
}
void Update () {
// Update the timer and quit if it's expired.
timerInactivity += Time.deltaTime;
if (isAnyInputDown()) ResetInactivityTimer ();
if (timerInactivity > InactivityTimeout) Application.Quit();
// Coin insert: '='
if (Input.GetKeyDown(KeyCode.Equals)) {
BroadcastMessage("OnCoinInsert");
}
// LEX button tap: '['
if (Input.GetKeyDown(KeyCode.LeftBracket)) {
BroadcastMessage("OnLexButton");
}
// LEX button held down: ']'
if (Input.GetKeyDown(KeyCode.RightBracket)) {
// Handle quitting yourself with BroadcastMessage instead of Application.Quit().
// BroadcastMessage("OnLexQuitButton");
Application.Quit();
}
}
bool isAnyInputDown() {
return Input.anyKey || isAnyAxisHeld();
}
bool isAnyAxisHeld() {
return LEXITRON_AXES.Any(axis => roundAbs(Input.GetAxis(axis)) == 1);
}
// Take the absolute value of `num` and round to an integer.
int roundAbs(float num) {
return (int) Math.Round(Math.Abs(num), 0, MidpointRounding.AwayFromZero);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment