5 using System.Collections.Generic;
8 #if UNITY_WSA && !UNITY_2017_2_OR_NEWER 24 [Obsolete(
"Will be removed in a future release")]
30 private class SourceData
32 public SourceData(uint sourceId)
36 SourcePosition = Vector3.zero;
38 IsSourceDownPending =
false;
39 SourceStateChanged =
false;
40 SourceStateUpdateTimer = -1;
43 public readonly uint SourceId;
44 public bool HasPosition;
45 public Vector3 SourcePosition;
46 public bool IsSourceDown;
47 public bool IsSourceDownPending;
48 public bool SourceStateChanged;
49 public float SourceStateUpdateTimer;
56 private const float SourcePressDelay = 0.07f;
58 [Tooltip(
"Use unscaled time. This is useful for games that have a pause mechanism or otherwise adjust the game timescale.")]
59 public bool UseUnscaledTime =
true;
64 private readonly Dictionary<uint, SourceData> sourceIdToData =
new Dictionary<uint, SourceData>(4);
65 private readonly List<uint> pendingSourceIdDeletes =
new List<uint>();
68 private readonly HashSet<uint> currentSources =
new HashSet<uint>();
69 private readonly HashSet<uint> newSources =
new HashSet<uint>();
75 SourceData sourceData;
76 if (sourceIdToData.TryGetValue(sourceId, out sourceData))
78 if (sourceData.HasPosition)
87 public override bool TryGetMenu(uint sourceId, out
bool isPressed)
89 throw new NotImplementedException();
94 SourceData sourceData;
95 if (sourceIdToData.TryGetValue(sourceId, out sourceData))
97 if (sourceData.HasPosition)
99 position = sourceData.SourcePosition;
105 position = Vector3.zero;
112 orientation = Quaternion.identity;
118 throw new NotImplementedException();
123 throw new NotImplementedException();
128 throw new NotImplementedException();
131 public override bool TryGetThumbstick(uint sourceId, out
bool isPressed, out Vector2 position)
133 throw new NotImplementedException();
136 public override bool TryGetTouchpad(uint sourceId, out
bool isPressed, out
bool isTouched, out Vector2 position)
138 throw new NotImplementedException();
141 public override bool TryGetSelect(uint sourceId, out
bool isPressed, out
double pressedValue)
143 throw new NotImplementedException();
146 public override bool TryGetGrasp(uint sourceId, out
bool isPressed)
148 throw new NotImplementedException();
153 throw new NotImplementedException();
156 private void Update()
159 currentSources.Clear();
162 SendSourceVisibilityEvents();
168 private void UpdateSourceData()
170 #if UNITY_WSA && !UNITY_2017_2_OR_NEWER 172 InteractionSourceState[] sourceStates = InteractionManager.GetCurrentReading();
173 if (sourceStates != null)
175 for (var i = 0; i < sourceStates.Length; ++i)
177 InteractionSourceState handSource = sourceStates[i];
178 SourceData sourceData = GetOrAddSourceData(handSource.source);
179 currentSources.Add(handSource.source.id);
181 UpdateSourceState(handSource, sourceData);
187 #if UNITY_WSA && !UNITY_2017_2_OR_NEWER 188 private SourceData GetOrAddSourceData(InteractionSource interactionSource)
195 SourceData sourceData;
196 if (!sourceIdToData.TryGetValue(interactionSource.id, out sourceData))
198 sourceData =
new SourceData(interactionSource.id);
199 sourceIdToData.Add(sourceData.SourceId, sourceData);
200 newSources.Add(sourceData.SourceId);
211 private void UpdateSourceState(InteractionSourceState interactionSource, SourceData sourceData)
214 Vector3 sourcePosition;
215 if (interactionSource.properties.location.TryGetPosition(out sourcePosition))
217 sourceData.HasPosition =
true;
218 sourceData.SourcePosition = sourcePosition;
222 if (interactionSource.pressed != sourceData.IsSourceDownPending)
224 sourceData.IsSourceDownPending = interactionSource.pressed;
225 sourceData.SourceStateUpdateTimer = SourcePressDelay;
229 sourceData.SourceStateChanged =
false;
230 if (sourceData.SourceStateUpdateTimer >= 0)
232 float deltaTime = UseUnscaledTime
233 ? Time.unscaledDeltaTime
236 sourceData.SourceStateUpdateTimer -= deltaTime;
237 if (sourceData.SourceStateUpdateTimer < 0)
239 sourceData.IsSourceDown = sourceData.IsSourceDownPending;
240 sourceData.SourceStateChanged =
true;
244 SendSourceStateEvents(sourceData);
252 private void SendSourceStateEvents(SourceData sourceData)
255 if (sourceData.SourceStateChanged)
257 if (sourceData.IsSourceDown)
271 private void SendSourceVisibilityEvents()
274 foreach (uint newSource
in newSources)
280 foreach (uint existingSource
in sourceIdToData.Keys)
282 if (!currentSources.Contains(existingSource))
284 pendingSourceIdDeletes.Add(existingSource);
290 for (
int i = 0; i < pendingSourceIdDeletes.Count; ++i)
292 sourceIdToData.Remove(pendingSourceIdDeletes[i]);
294 pendingSourceIdDeletes.Clear();