InputControlVisualizer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.InputSystem.Layouts;
  4. using UnityEngine.InputSystem.LowLevel;
  5. ////TODO: add way to plot values over time
  6. // Goal is to build this out into something that can visualize a large number of
  7. // aspects about an InputControl/InputDevice especially with an eye towards making
  8. // it a good deal to debug any input collection/processing irregularities that may
  9. // be seen in players (or the editor, for that matter).
  10. // Some fields assigned through only through serialization.
  11. #pragma warning disable CS0649
  12. namespace UnityEngine.InputSystem.Samples
  13. {
  14. /// <summary>
  15. /// A component for debugging purposes that adds an on-screen display which shows
  16. /// activity on an input control over time.
  17. /// </summary>
  18. /// <remarks>
  19. /// This component is most useful for debugging input directly on the source device.
  20. /// </remarks>
  21. /// <seealso cref="InputActionVisualizer"/>
  22. [AddComponentMenu("Input/Debug/Input Control Visualizer")]
  23. [ExecuteInEditMode]
  24. public class InputControlVisualizer : InputVisualizer
  25. {
  26. /// <summary>
  27. /// What kind of visualization to show.
  28. /// </summary>
  29. public Mode visualization
  30. {
  31. get => m_Visualization;
  32. set
  33. {
  34. if (m_Visualization == value)
  35. return;
  36. m_Visualization = value;
  37. SetupVisualizer();
  38. }
  39. }
  40. /// <summary>
  41. /// Path of the control that is to be visualized.
  42. /// </summary>
  43. /// <seealso cref="InputControlPath"/>
  44. /// <seealso cref="InputControl.path"/>
  45. public string controlPath
  46. {
  47. get => m_ControlPath;
  48. set
  49. {
  50. m_ControlPath = value;
  51. if (m_Control != null)
  52. ResolveControl();
  53. }
  54. }
  55. /// <summary>
  56. /// If, at runtime, multiple controls are matching <see cref="controlPath"/>, this property
  57. /// determines the index of the control that is retrieved from the possible options.
  58. /// </summary>
  59. public int controlIndex
  60. {
  61. get => m_ControlIndex;
  62. set
  63. {
  64. m_ControlIndex = value;
  65. if (m_Control != null)
  66. ResolveControl();
  67. }
  68. }
  69. /// <summary>
  70. /// The control resolved from <see cref="controlPath"/> at runtime. May be null.
  71. /// </summary>
  72. public InputControl control => m_Control;
  73. protected new void OnEnable()
  74. {
  75. if (m_Visualization == Mode.None)
  76. return;
  77. if (s_EnabledInstances == null)
  78. s_EnabledInstances = new List<InputControlVisualizer>();
  79. if (s_EnabledInstances.Count == 0)
  80. {
  81. InputSystem.onDeviceChange += OnDeviceChange;
  82. InputSystem.onEvent += OnEvent;
  83. }
  84. s_EnabledInstances.Add(this);
  85. ResolveControl();
  86. base.OnEnable();
  87. }
  88. protected new void OnDisable()
  89. {
  90. if (m_Visualization == Mode.None)
  91. return;
  92. s_EnabledInstances.Remove(this);
  93. if (s_EnabledInstances.Count == 0)
  94. {
  95. InputSystem.onDeviceChange -= OnDeviceChange;
  96. InputSystem.onEvent -= OnEvent;
  97. }
  98. m_Control = null;
  99. base.OnDisable();
  100. }
  101. protected new void OnGUI()
  102. {
  103. if (m_Visualization == Mode.None)
  104. return;
  105. base.OnGUI();
  106. }
  107. protected new void OnValidate()
  108. {
  109. ResolveControl();
  110. base.OnValidate();
  111. }
  112. [Tooltip("The type of visualization to perform for the control.")]
  113. [SerializeField] private Mode m_Visualization;
  114. [Tooltip("Path of the control that should be visualized. If at runtime, multiple "
  115. + "controls match the given path, the 'Control Index' property can be used to decide "
  116. + "which of the controls to visualize.")]
  117. [InputControl, SerializeField] private string m_ControlPath;
  118. [Tooltip("If multiple controls match 'Control Path' at runtime, this property decides "
  119. + "which control to visualize from the list of candidates. It is a zero-based index.")]
  120. [SerializeField] private int m_ControlIndex;
  121. [NonSerialized] private InputControl m_Control;
  122. private static List<InputControlVisualizer> s_EnabledInstances;
  123. private void ResolveControl()
  124. {
  125. m_Control = null;
  126. if (string.IsNullOrEmpty(m_ControlPath))
  127. return;
  128. using (var candidates = InputSystem.FindControls(m_ControlPath))
  129. {
  130. var numCandidates = candidates.Count;
  131. if (numCandidates > 1 && m_ControlIndex < numCandidates && m_ControlIndex >= 0)
  132. m_Control = candidates[m_ControlIndex];
  133. else if (numCandidates > 0)
  134. m_Control = candidates[0];
  135. }
  136. SetupVisualizer();
  137. }
  138. private void SetupVisualizer()
  139. {
  140. if (m_Control == null)
  141. {
  142. m_Visualizer = null;
  143. return;
  144. }
  145. switch (m_Visualization)
  146. {
  147. case Mode.Value:
  148. {
  149. var valueType = m_Control.valueType;
  150. if (valueType == typeof(Vector2))
  151. m_Visualizer = new VisualizationHelpers.Vector2Visualizer(m_HistorySamples);
  152. else if (valueType == typeof(float))
  153. m_Visualizer = new VisualizationHelpers.ScalarVisualizer<float>(m_HistorySamples)
  154. {
  155. ////TODO: pass actual min/max limits of control
  156. limitMax = 1,
  157. limitMin = 0
  158. };
  159. else if (valueType == typeof(int))
  160. m_Visualizer = new VisualizationHelpers.ScalarVisualizer<int>(m_HistorySamples)
  161. {
  162. ////TODO: pass actual min/max limits of control
  163. limitMax = 1,
  164. limitMin = 0
  165. };
  166. else
  167. {
  168. ////TODO: generic visualizer
  169. }
  170. break;
  171. }
  172. case Mode.Events:
  173. {
  174. var visualizer = new VisualizationHelpers.TimelineVisualizer(m_HistorySamples)
  175. {
  176. timeUnit = VisualizationHelpers.TimelineVisualizer.TimeUnit.Frames,
  177. historyDepth = m_HistorySamples,
  178. showLimits = true,
  179. limitsY = new Vector2(0, 5) // Will expand upward automatically
  180. };
  181. m_Visualizer = visualizer;
  182. visualizer.AddTimeline("Events", Color.green,
  183. VisualizationHelpers.TimelineVisualizer.PlotType.BarChart);
  184. break;
  185. }
  186. case Mode.MaximumLag:
  187. {
  188. var visualizer = new VisualizationHelpers.TimelineVisualizer(m_HistorySamples)
  189. {
  190. timeUnit = VisualizationHelpers.TimelineVisualizer.TimeUnit.Frames,
  191. historyDepth = m_HistorySamples,
  192. valueUnit = new GUIContent("ms"),
  193. showLimits = true,
  194. limitsY = new Vector2(0, 6)
  195. };
  196. m_Visualizer = visualizer;
  197. visualizer.AddTimeline("MaxLag", Color.red,
  198. VisualizationHelpers.TimelineVisualizer.PlotType.BarChart);
  199. break;
  200. }
  201. case Mode.Bytes:
  202. {
  203. var visualizer = new VisualizationHelpers.TimelineVisualizer(m_HistorySamples)
  204. {
  205. timeUnit = VisualizationHelpers.TimelineVisualizer.TimeUnit.Frames,
  206. valueUnit = new GUIContent("bytes"),
  207. historyDepth = m_HistorySamples,
  208. showLimits = true,
  209. limitsY = new Vector2(0, 64)
  210. };
  211. m_Visualizer = visualizer;
  212. visualizer.AddTimeline("Bytes", Color.red,
  213. VisualizationHelpers.TimelineVisualizer.PlotType.BarChart);
  214. break;
  215. }
  216. default:
  217. throw new NotImplementedException();
  218. }
  219. }
  220. private static void OnDeviceChange(InputDevice device, InputDeviceChange change)
  221. {
  222. if (change != InputDeviceChange.Added && change != InputDeviceChange.Removed)
  223. return;
  224. for (var i = 0; i < s_EnabledInstances.Count; ++i)
  225. {
  226. var component = s_EnabledInstances[i];
  227. if (change == InputDeviceChange.Removed && component.m_Control != null &&
  228. component.m_Control.device == device)
  229. component.ResolveControl();
  230. else if (change == InputDeviceChange.Added)
  231. component.ResolveControl();
  232. }
  233. }
  234. private static void OnEvent(InputEventPtr eventPtr, InputDevice device)
  235. {
  236. // Ignore very first update as we usually get huge lag spikes and event count
  237. // spikes in it from stuff that has accumulated while going into play mode or
  238. // starting up the player.
  239. if (InputState.updateCount <= 1)
  240. return;
  241. if (InputState.currentUpdateType == InputUpdateType.Editor)
  242. return;
  243. if (!eventPtr.IsA<StateEvent>() && !eventPtr.IsA<DeltaStateEvent>())
  244. return;
  245. for (var i = 0; i < s_EnabledInstances.Count; ++i)
  246. {
  247. var component = s_EnabledInstances[i];
  248. if (component.m_Control?.device != device || component.m_Visualizer == null)
  249. continue;
  250. component.OnEventImpl(eventPtr);
  251. }
  252. }
  253. private unsafe void OnEventImpl(InputEventPtr eventPtr)
  254. {
  255. switch (m_Visualization)
  256. {
  257. case Mode.Value:
  258. {
  259. var statePtr = m_Control.GetStatePtrFromStateEvent(eventPtr);
  260. if (statePtr == null)
  261. return; // No value for control in event.
  262. var value = m_Control.ReadValueFromStateAsObject(statePtr);
  263. m_Visualizer.AddSample(value, eventPtr.time);
  264. break;
  265. }
  266. case Mode.Events:
  267. {
  268. var visualizer = (VisualizationHelpers.TimelineVisualizer)m_Visualizer;
  269. var frame = (int)InputState.updateCount;
  270. ref var valueRef = ref visualizer.GetOrCreateSample(0, frame);
  271. var value = valueRef.ToInt32() + 1;
  272. valueRef = value;
  273. visualizer.limitsY =
  274. new Vector2(0, Mathf.Max(value, visualizer.limitsY.y));
  275. break;
  276. }
  277. case Mode.MaximumLag:
  278. {
  279. var visualizer = (VisualizationHelpers.TimelineVisualizer)m_Visualizer;
  280. var lag = (Time.realtimeSinceStartup - eventPtr.time) * 1000; // In milliseconds.
  281. var frame = (int)InputState.updateCount;
  282. ref var valueRef = ref visualizer.GetOrCreateSample(0, frame);
  283. if (lag > valueRef.ToDouble())
  284. {
  285. valueRef = lag;
  286. if (lag > visualizer.limitsY.y)
  287. visualizer.limitsY = new Vector2(0, Mathf.Ceil((float)lag));
  288. }
  289. break;
  290. }
  291. case Mode.Bytes:
  292. {
  293. var visualizer = (VisualizationHelpers.TimelineVisualizer)m_Visualizer;
  294. var frame = (int)InputState.updateCount;
  295. ref var valueRef = ref visualizer.GetOrCreateSample(0, frame);
  296. var value = valueRef.ToInt32() + eventPtr.sizeInBytes;
  297. valueRef = value;
  298. visualizer.limitsY =
  299. new Vector2(0, Mathf.Max(value, visualizer.limitsY.y));
  300. break;
  301. }
  302. }
  303. }
  304. /// <summary>
  305. /// Determines which aspect of the control should be visualized.
  306. /// </summary>
  307. public enum Mode
  308. {
  309. None = 0,
  310. Value = 1,
  311. Events = 4,
  312. MaximumLag = 6,
  313. Bytes = 7,
  314. }
  315. }
  316. }