Skip to content

Instantly share code, notes, and snippets.

@mathiassoeholm
Created February 12, 2014 22:56
Show Gist options
  • Save mathiassoeholm/8966257 to your computer and use it in GitHub Desktop.
Save mathiassoeholm/8966257 to your computer and use it in GitHub Desktop.
Unity dance pad input
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);
}
}
}
}
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