File size: 13,698 Bytes
18a519f | 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | using System;
using System.Collections.Generic;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.LowLevel;
////TODO: add way to plot values over time
// Goal is to build this out into something that can visualize a large number of
// aspects about an InputControl/InputDevice especially with an eye towards making
// it a good deal to debug any input collection/processing irregularities that may
// be seen in players (or the editor, for that matter).
// Some fields assigned through only through serialization.
#pragma warning disable CS0649
namespace UnityEngine.InputSystem.Samples
{
/// <summary>
/// A component for debugging purposes that adds an on-screen display which shows
/// activity on an input control over time.
/// </summary>
/// <remarks>
/// This component is most useful for debugging input directly on the source device.
/// </remarks>
/// <seealso cref="InputActionVisualizer"/>
[AddComponentMenu("Input/Debug/Input Control Visualizer")]
[ExecuteInEditMode]
public class InputControlVisualizer : InputVisualizer
{
/// <summary>
/// What kind of visualization to show.
/// </summary>
public Mode visualization
{
get => m_Visualization;
set
{
if (m_Visualization == value)
return;
m_Visualization = value;
SetupVisualizer();
}
}
/// <summary>
/// Path of the control that is to be visualized.
/// </summary>
/// <seealso cref="InputControlPath"/>
/// <seealso cref="InputControl.path"/>
public string controlPath
{
get => m_ControlPath;
set
{
m_ControlPath = value;
if (m_Control != null)
ResolveControl();
}
}
/// <summary>
/// If, at runtime, multiple controls are matching <see cref="controlPath"/>, this property
/// determines the index of the control that is retrieved from the possible options.
/// </summary>
public int controlIndex
{
get => m_ControlIndex;
set
{
m_ControlIndex = value;
if (m_Control != null)
ResolveControl();
}
}
/// <summary>
/// The control resolved from <see cref="controlPath"/> at runtime. May be null.
/// </summary>
public InputControl control => m_Control;
protected new void OnEnable()
{
if (m_Visualization == Mode.None)
return;
if (s_EnabledInstances == null)
s_EnabledInstances = new List<InputControlVisualizer>();
if (s_EnabledInstances.Count == 0)
{
InputSystem.onDeviceChange += OnDeviceChange;
InputSystem.onEvent += OnEvent;
}
s_EnabledInstances.Add(this);
ResolveControl();
base.OnEnable();
}
protected new void OnDisable()
{
if (m_Visualization == Mode.None)
return;
s_EnabledInstances.Remove(this);
if (s_EnabledInstances.Count == 0)
{
InputSystem.onDeviceChange -= OnDeviceChange;
InputSystem.onEvent -= OnEvent;
}
m_Control = null;
base.OnDisable();
}
protected new void OnGUI()
{
if (m_Visualization == Mode.None)
return;
base.OnGUI();
}
protected new void OnValidate()
{
ResolveControl();
base.OnValidate();
}
[Tooltip("The type of visualization to perform for the control.")]
[SerializeField] private Mode m_Visualization;
[Tooltip("Path of the control that should be visualized. If at runtime, multiple "
+ "controls match the given path, the 'Control Index' property can be used to decide "
+ "which of the controls to visualize.")]
[InputControl, SerializeField] private string m_ControlPath;
[Tooltip("If multiple controls match 'Control Path' at runtime, this property decides "
+ "which control to visualize from the list of candidates. It is a zero-based index.")]
[SerializeField] private int m_ControlIndex;
[NonSerialized] private InputControl m_Control;
private static List<InputControlVisualizer> s_EnabledInstances;
private void ResolveControl()
{
m_Control = null;
if (string.IsNullOrEmpty(m_ControlPath))
return;
using (var candidates = InputSystem.FindControls(m_ControlPath))
{
var numCandidates = candidates.Count;
if (numCandidates > 1 && m_ControlIndex < numCandidates && m_ControlIndex >= 0)
m_Control = candidates[m_ControlIndex];
else if (numCandidates > 0)
m_Control = candidates[0];
}
SetupVisualizer();
}
private void SetupVisualizer()
{
if (m_Control == null)
{
m_Visualizer = null;
return;
}
switch (m_Visualization)
{
case Mode.Value:
{
var valueType = m_Control.valueType;
if (valueType == typeof(Vector2))
m_Visualizer = new VisualizationHelpers.Vector2Visualizer(m_HistorySamples);
else if (valueType == typeof(float))
m_Visualizer = new VisualizationHelpers.ScalarVisualizer<float>(m_HistorySamples)
{
////TODO: pass actual min/max limits of control
limitMax = 1,
limitMin = 0
};
else if (valueType == typeof(int))
m_Visualizer = new VisualizationHelpers.ScalarVisualizer<int>(m_HistorySamples)
{
////TODO: pass actual min/max limits of control
limitMax = 1,
limitMin = 0
};
else
{
////TODO: generic visualizer
}
break;
}
case Mode.Events:
{
var visualizer = new VisualizationHelpers.TimelineVisualizer(m_HistorySamples)
{
timeUnit = VisualizationHelpers.TimelineVisualizer.TimeUnit.Frames,
historyDepth = m_HistorySamples,
showLimits = true,
limitsY = new Vector2(0, 5) // Will expand upward automatically
};
m_Visualizer = visualizer;
visualizer.AddTimeline("Events", Color.green,
VisualizationHelpers.TimelineVisualizer.PlotType.BarChart);
break;
}
case Mode.MaximumLag:
{
var visualizer = new VisualizationHelpers.TimelineVisualizer(m_HistorySamples)
{
timeUnit = VisualizationHelpers.TimelineVisualizer.TimeUnit.Frames,
historyDepth = m_HistorySamples,
valueUnit = new GUIContent("ms"),
showLimits = true,
limitsY = new Vector2(0, 6)
};
m_Visualizer = visualizer;
visualizer.AddTimeline("MaxLag", Color.red,
VisualizationHelpers.TimelineVisualizer.PlotType.BarChart);
break;
}
case Mode.Bytes:
{
var visualizer = new VisualizationHelpers.TimelineVisualizer(m_HistorySamples)
{
timeUnit = VisualizationHelpers.TimelineVisualizer.TimeUnit.Frames,
valueUnit = new GUIContent("bytes"),
historyDepth = m_HistorySamples,
showLimits = true,
limitsY = new Vector2(0, 64)
};
m_Visualizer = visualizer;
visualizer.AddTimeline("Bytes", Color.red,
VisualizationHelpers.TimelineVisualizer.PlotType.BarChart);
break;
}
case Mode.DeviceCurrent:
{
m_Visualizer = new VisualizationHelpers.CurrentDeviceVisualizer();
break;
}
default:
throw new NotImplementedException();
}
}
private static void OnDeviceChange(InputDevice device, InputDeviceChange change)
{
if (change != InputDeviceChange.Added && change != InputDeviceChange.Removed)
return;
for (var i = 0; i < s_EnabledInstances.Count; ++i)
{
var component = s_EnabledInstances[i];
if (change == InputDeviceChange.Removed && component.m_Control != null &&
component.m_Control.device == device)
component.ResolveControl();
else if (change == InputDeviceChange.Added)
component.ResolveControl();
}
}
private static void OnEvent(InputEventPtr eventPtr, InputDevice device)
{
// Ignore very first update as we usually get huge lag spikes and event count
// spikes in it from stuff that has accumulated while going into play mode or
// starting up the player.
if (InputState.updateCount <= 1)
return;
if (InputState.currentUpdateType == InputUpdateType.Editor)
return;
if (!eventPtr.IsA<StateEvent>() && !eventPtr.IsA<DeltaStateEvent>())
return;
for (var i = 0; i < s_EnabledInstances.Count; ++i)
{
var component = s_EnabledInstances[i];
if (component.m_Control?.device != device || component.m_Visualizer == null)
continue;
component.OnEventImpl(eventPtr, device);
}
}
private unsafe void OnEventImpl(InputEventPtr eventPtr, InputDevice device)
{
switch (m_Visualization)
{
case Mode.Value:
{
var statePtr = m_Control.GetStatePtrFromStateEvent(eventPtr);
if (statePtr == null)
return; // No value for control in event.
var value = m_Control.ReadValueFromStateAsObject(statePtr);
m_Visualizer.AddSample(value, eventPtr.time);
break;
}
case Mode.Events:
{
var visualizer = (VisualizationHelpers.TimelineVisualizer)m_Visualizer;
var frame = (int)InputState.updateCount;
ref var valueRef = ref visualizer.GetOrCreateSample(0, frame);
var value = valueRef.ToInt32() + 1;
valueRef = value;
visualizer.limitsY =
new Vector2(0, Mathf.Max(value, visualizer.limitsY.y));
break;
}
case Mode.MaximumLag:
{
var visualizer = (VisualizationHelpers.TimelineVisualizer)m_Visualizer;
var lag = (Time.realtimeSinceStartup - eventPtr.time) * 1000; // In milliseconds.
var frame = (int)InputState.updateCount;
ref var valueRef = ref visualizer.GetOrCreateSample(0, frame);
if (lag > valueRef.ToDouble())
{
valueRef = lag;
if (lag > visualizer.limitsY.y)
visualizer.limitsY = new Vector2(0, Mathf.Ceil((float)lag));
}
break;
}
case Mode.Bytes:
{
var visualizer = (VisualizationHelpers.TimelineVisualizer)m_Visualizer;
var frame = (int)InputState.updateCount;
ref var valueRef = ref visualizer.GetOrCreateSample(0, frame);
var value = valueRef.ToInt32() + eventPtr.sizeInBytes;
valueRef = value;
visualizer.limitsY =
new Vector2(0, Mathf.Max(value, visualizer.limitsY.y));
break;
}
case Mode.DeviceCurrent:
{
m_Visualizer.AddSample(device, eventPtr.time);
break;
}
}
}
/// <summary>
/// Determines which aspect of the control should be visualized.
/// </summary>
public enum Mode
{
None = 0,
Value = 1,
Events = 4,
MaximumLag = 6,
Bytes = 7,
DeviceCurrent = 8,
}
}
}
|