-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInputGUIDisplay.cs
115 lines (106 loc) · 4.73 KB
/
InputGUIDisplay.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
/// <summary>
/// Reads an input action asset from unitys new input system and shows it in a GUI update. Has not been tested extensively with various mappings so expect bugs.
/// </summary>
public class InputGUIDisplay : MonoBehaviour
{
/// <summary>
/// The input asset to read off of. Make sure to asign it in the inspector.
/// </summary>
[SerializeField] InputActionAsset input;
[SerializeField] bool showGUI;
[SerializeField] Vector3 GUI_Position;
[SerializeField] Color textBackGroundColor = new Color(0.2F, 0.2F, 0.2F, 0.7F);
[SerializeField] GUIStyle GUI_Style;
string text;
float previousHeight;
float previousWidth;
Color previousBackgroundColor;
/// <summary>
/// Dictionary<Action map name, Dictionary<action name, Dictionary<input source, List<control name>>>>
/// </summary>
Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>> bindingsDictionary;
//Builds a table out of the control map and then sets some text in the scene.
void Start()
{
text = "";
bindingsDictionary = new Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>>();
var actionMaps = input.actionMaps;
foreach (var actionMap in actionMaps)
{
if (!bindingsDictionary.ContainsKey(actionMap.name))
bindingsDictionary.Add(actionMap.name, new Dictionary<string, Dictionary<string, List<string>>>());
var bindings = actionMap.bindings;
foreach (var binding in bindings)
{
if (!bindingsDictionary[actionMap.name].ContainsKey(binding.action))
bindingsDictionary[actionMap.name].Add(binding.action, new Dictionary<string, List<string>>());
var parseInputArray = binding.effectivePath.Split('<', '>');
if (parseInputArray.Length > 2)
{
var inputSource = parseInputArray[1];
if (!bindingsDictionary[actionMap.name][binding.action].ContainsKey(inputSource))
bindingsDictionary[actionMap.name][binding.action].Add(inputSource, new List<string>());
var input = parseInputArray[2].Split('/');
if (input.Length == 2)//keyboard input
bindingsDictionary[actionMap.name][binding.action][inputSource].Add(input[1]);
else if (input.Length == 3)//controller
bindingsDictionary[actionMap.name][binding.action][inputSource].Add($"{input[1]}-{input[2]}");
}
}
foreach (var actionMapTypes in bindingsDictionary)
{
text += $"{actionMapTypes.Key}";
foreach (var actions in actionMapTypes.Value)
{
text += $"\n <color=yellow>{actions.Key}</color>";
foreach (var inputSource in actions.Value)
{
text += $"\n <color=blue>{inputSource.Key}</color> [";
int inputCount = 0;
foreach (var input in inputSource.Value)
{
if (inputCount >= inputSource.Value.Count-1)
text += $"{input}";
else
text += $"{input},";
inputCount++;
}
text += "]";
}
}
text += "\n";
}
}
}
private void OnGUI()
{
if (!showGUI) return;
float width = Screen.width / 1200.0f;
float height = Screen.height / 800.0f;
if (width != previousWidth || height != previousHeight || GUI_Style.normal.background == null || previousBackgroundColor != textBackGroundColor)
{
previousBackgroundColor = textBackGroundColor;
previousWidth = width;
previousHeight = height;
GUI_Style.normal.background =
MakeTex(Mathf.CeilToInt(width), Mathf.CeilToInt(height), textBackGroundColor);
}
GUI.matrix = Matrix4x4.TRS(GUI_Position, Quaternion.identity, new Vector3(width, height, 1.0f));
GUILayout.Label(text, GUI_Style);
}
Texture2D MakeTex(int width, int height, Color col)
{
var pix = new Color[width * height];
for (int i = 0; i < pix.Length; i++)
{
pix[i] = col;
}
var result = new Texture2D(width, height);
result.SetPixels(pix);
result.Apply();
return result;
}
}