Skip to content

Commit

Permalink
Delay the initial search for detected sources
Browse files Browse the repository at this point in the history
to fix a race condition when running in the Editor
  • Loading branch information
keveleigh committed Nov 21, 2018
1 parent ba03a46 commit e59ff5a
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ protected override void Awake()
base.Awake();

#if UNITY_WSA && UNITY_2017_2_OR_NEWER
foreach (var sourceState in InteractionManager.GetCurrentReading())
{
if (sourceState.source.kind == InteractionSourceKind.Controller)
{
StartTrackingController(sourceState.source);
}
}

Application.onBeforeRender += Application_onBeforeRender;

if (GLTFMaterial == null)
Expand Down Expand Up @@ -123,16 +115,29 @@ private void Application_onBeforeRender()
{
// NOTE: This work is being done here to present the most correct rendered location of the controller each frame.
// Any app logic depending on the controller state should happen in Update() or using InteractionManager's events.
UpdateControllerState();
// We don't want to potentially start loading a new controller model in this call, since onBeforeRender shouldn't
// do much work for performance reasons.
UpdateControllerState(false);
}

private void UpdateControllerState()
private void UpdateControllerState(bool createNewControllerIfNeeded = true)
{
#if UNITY_WSA && UNITY_2017_2_OR_NEWER
foreach (var sourceState in InteractionManager.GetCurrentReading())
{
if (sourceState.source.kind != InteractionSourceKind.Controller)
{
continue;
}

string key = GenerateKey(sourceState.source);
if (createNewControllerIfNeeded && !controllerDictionary.ContainsKey(key) && !loadingControllers.Contains(key))
{
StartTrackingController(sourceState.source);
}

MotionControllerInfo currentController;
if (sourceState.source.kind == InteractionSourceKind.Controller && controllerDictionary.TryGetValue(GenerateKey(sourceState.source), out currentController))
if (controllerDictionary.TryGetValue(key, out currentController))
{
if (AnimateControllerModel)
{
Expand Down Expand Up @@ -412,7 +417,7 @@ private void FinishControllerSetup(GameObject controllerModelGameObject, Interac
{
var parentGameObject = new GameObject
{
name = handedness + "Controller"
name = dictionaryKey + "Controller"
};

parentGameObject.transform.parent = transform;
Expand Down

0 comments on commit e59ff5a

Please sign in to comment.