Created
February 12, 2014 22:56
-
-
Save mathiassoeholm/8966257 to your computer and use it in GitHub Desktop.
Unity dance pad input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Object = UnityEngine.Object; | |
namespace DancePadInput | |
{ | |
public enum Player | |
{ | |
Any, | |
One = 1, | |
Two = 2, | |
} | |
public enum DancePadButton | |
{ | |
Left = 0, | |
Down = 1, | |
Up = 2, | |
Right = 3, | |
Triangle = 4, | |
Square = 5, | |
X = 6, | |
Circle = 7, | |
Start = 8, | |
Select = 9 | |
} | |
public static class DancePad | |
{ | |
private static DancePadState _stateUpdater; | |
private static void Initialize() | |
{ | |
_stateUpdater = new GameObject("Dance pad state updater").AddComponent<DancePadState>(); | |
Object.DontDestroyOnLoad(_stateUpdater.gameObject); | |
} | |
public static bool GetButton(DancePadButton button, Player player) | |
{ | |
if (_stateUpdater == null) | |
{ | |
Initialize(); | |
} | |
string inputKey = player == Player.Any | |
? "joystick button " + (int)button | |
: "joystick " + (int)player + " button " + (int)button; | |
return Input.GetButton(inputKey); | |
} | |
public static bool GetButtonDown(DancePadButton button, Player player) | |
{ | |
if (_stateUpdater == null) | |
{ | |
Initialize(); | |
return GetButton(button, player); | |
} | |
else | |
{ | |
return GetButton(button, player) && !_stateUpdater.GetButton(button, player); | |
} | |
} | |
public static bool GetButtonUp(DancePadButton button, Player player) | |
{ | |
if (_stateUpdater == null) | |
{ | |
Initialize(); | |
return false; | |
} | |
else | |
{ | |
return !GetButton(button, player) && _stateUpdater.GetButton(button, player); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using DancePadInput; | |
using UnityEngine; | |
public class DancePadState : MonoBehaviour | |
{ | |
private Dictionary<Player, Dictionary<DancePadButton, bool>> _playerToButtonToState; | |
void Awake() | |
{ | |
_playerToButtonToState = new Dictionary<Player, Dictionary<DancePadButton, bool>>(); | |
int playersLength = Enum.GetNames(typeof(Player)).Length; | |
int buttonsLength = Enum.GetNames(typeof(DancePadButton)).Length; | |
for (int p = 0; p < playersLength; p++) | |
{ | |
_playerToButtonToState.Add((Player)p, new Dictionary<DancePadButton, bool>()); | |
for (int b = 0; b < buttonsLength; b++) | |
{ | |
_playerToButtonToState[(Player)p].Add((DancePadButton)b, false); | |
} | |
} | |
} | |
void LateUpdate() | |
{ | |
SaveState(); | |
} | |
public bool GetButton(DancePadButton button, Player player) | |
{ | |
return _playerToButtonToState[player][button]; | |
} | |
private void SaveState() | |
{ | |
int playersLength = Enum.GetNames(typeof(Player)).Length; | |
int buttonsLength = Enum.GetNames(typeof(DancePadButton)).Length; | |
for (int p = 0; p < playersLength; p++) | |
{ | |
for (int b = 0; b < buttonsLength; b++) | |
{ | |
_playerToButtonToState[(Player)p][(DancePadButton)b] = DancePad.GetButton((DancePadButton)b, (Player)p); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment