Skip to content

Commit bcb0928

Browse files
committed
Don't provide hand joints or input viz when unfocused (outside the editor)
1 parent c1f40a1 commit bcb0928

File tree

6 files changed

+58
-21
lines changed

6 files changed

+58
-21
lines changed

org.mixedrealitytoolkit.core/Subsystems/MRTKLifecycleManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class MRTKLifecycleManager :
2727
/// <summary>
2828
/// The list of <see cref="IMRTKManagedSubsystem"/> objects being managed by this class.
2929
/// </summary>
30-
protected List<IMRTKManagedSubsystem> ManagedSubsystems
30+
protected List<IMRTKManagedSubsystem> ManagedSubsystems
3131
{
3232
get => managedSubsystems;
3333
set => managedSubsystems = value;
@@ -180,6 +180,7 @@ private void LateUpdate()
180180
}
181181
}
182182

183+
#if !UNITY_EDITOR
183184
/// <summary>
184185
/// Sent to all GameObjects when the player gets or loses focus.
185186
/// </summary>
@@ -203,6 +204,7 @@ protected void OnApplicationFocus(bool focus)
203204
}
204205
}
205206
}
207+
#endif // !UNITY_EDITOR
206208

207209
#endregion MonoBehaviour
208210

org.mixedrealitytoolkit.input/Controllers/HandModel.cs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,31 @@ public Transform ModelPrefab
6060

6161
#endregion Associated hand select values
6262

63+
/// <summary>
64+
/// See <see cref="MonoBehaviour"/>.
65+
/// </summary>
66+
protected virtual void Awake()
67+
{
68+
// Create empty container transform for the model if none specified.
69+
// This is not strictly necessary to create since this GameObject could be used
70+
// as the parent for the instantiated prefab, but doing so anyway for backwards compatibility.
71+
if (modelParent == null)
72+
{
73+
modelParent = new GameObject($"[{gameObject.name}] Model Parent").transform;
74+
modelParent.SetParent(transform, false);
75+
modelParent.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
76+
}
77+
}
78+
6379
/// <summary>
6480
/// A Unity event function that is called on the frame when a script is enabled just before any of the update methods are called the first time.
6581
/// </summary>
6682
protected virtual void Start()
6783
{
6884
// Instantiate the model prefab if it is set
69-
if (ModelPrefab != null)
85+
if (modelPrefab != null)
7086
{
71-
model = Instantiate(ModelPrefab, ModelParent);
87+
model = Instantiate(modelPrefab, modelParent);
7288

7389
Debug.Assert(selectInput != null, $"The Select Input reader for {name} is not set and will not be used with the instantiated hand model.");
7490

@@ -80,20 +96,23 @@ protected virtual void Start()
8096
}
8197
}
8298

99+
#if !UNITY_EDITOR
83100
/// <summary>
84-
/// See <see cref="MonoBehaviour"/>.
101+
/// Sent to all GameObjects when the player gets or loses focus.
85102
/// </summary>
86-
protected virtual void Awake()
103+
/// <param name="focus"><see langword="true"/> if the GameObjects have focus, else <see langword="false"/>.</param>
104+
protected void OnApplicationFocus(bool focus)
87105
{
88-
// Create empty container transform for the model if none specified.
89-
// This is not strictly necessary to create since this GameObject could be used
90-
// as the parent for the instantiated prefab, but doing so anyway for backwards compatibility.
91-
if (modelParent == null)
106+
// We want to ensure we're focused for input visualization, as some runtimes continue reporting "tracked" while pose updates are paused.
107+
// This is allowed, per-spec, as a "should": "Runtimes should make input actions inactive while the application is unfocused,
108+
// and applications should react to an inactive input action by skipping rendering of that action's input avatar
109+
// (depictions of hands or other tracked objects controlled by the user)."
110+
111+
if (modelParent != null)
92112
{
93-
modelParent = new GameObject($"[{gameObject.name}] Model Parent").transform;
94-
modelParent.SetParent(transform, false);
95-
modelParent.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
113+
modelParent.gameObject.SetActive(focus);
96114
}
97115
}
116+
#endif // !UNITY_EDITOR
98117
}
99118
}

org.mixedrealitytoolkit.input/Subsystems/Hands/HandsProvider.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,31 @@ private void ResetHands()
6060
public override bool TryGetEntireHand(XRNode handNode, out IReadOnlyList<HandJointPose> jointPoses)
6161
{
6262
Debug.Assert(handNode == XRNode.LeftHand || handNode == XRNode.RightHand, "Non-hand XRNode used in TryGetEntireHand query.");
63+
64+
#if !UNITY_EDITOR
65+
if (!Application.isFocused)
66+
{
67+
jointPoses = Array.Empty<HandJointPose>();
68+
return false;
69+
}
70+
#endif // !UNITY_EDITOR
71+
6372
return hands[handNode].TryGetEntireHand(out jointPoses);
6473
}
6574

6675
/// <inheritdoc/>
6776
public override bool TryGetJoint(TrackedHandJoint joint, XRNode handNode, out HandJointPose jointPose)
6877
{
6978
Debug.Assert(handNode == XRNode.LeftHand || handNode == XRNode.RightHand, "Non-hand XRNode used in TryGetJoint query.");
79+
80+
#if !UNITY_EDITOR
81+
if (!Application.isFocused)
82+
{
83+
jointPose = default;
84+
return false;
85+
}
86+
#endif // !UNITY_EDITOR
87+
7088
return hands[handNode].TryGetJoint(joint, out jointPose);
7189
}
7290

org.mixedrealitytoolkit.input/Tracking/HandPoseDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ private bool IsTrackingNone()
125125
private bool TryGetPolyfillDevicePose(out Pose devicePose)
126126
{
127127
bool poseRetrieved = false;
128-
Handedness handedness = HandNode.ToHandedness();
129128

130129
// palmPose retrieved in global space.
131130
if (XRSubsystemHelpers.HandsAggregator != null &&
@@ -135,6 +134,7 @@ private bool TryGetPolyfillDevicePose(out Pose devicePose)
135134
// our global palm pose back into scene-origin-space.
136135
devicePose = PlayspaceUtilities.InverseTransformPose(palmPose.Pose);
137136

137+
Handedness handedness = HandNode.ToHandedness();
138138
switch (handedness)
139139
{
140140
case Handedness.Left:

org.mixedrealitytoolkit.input/Utilities/HandDataContainer.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class HandDataContainer
2222
public bool AlreadyFullQueried { get; protected set; }
2323

2424
/// <summary>
25-
/// Will be <see langword="true"/> if the the hand joint query as successful.
25+
/// Will be <see langword="true"/> if the hand joint query as successful.
2626
/// </summary>
2727
public bool FullQueryValid { get; protected set; }
2828

@@ -41,24 +41,23 @@ public HandDataContainer(XRNode handNode)
4141
}
4242

4343
/// <summary>
44-
/// Reset the hand data query status
44+
/// Reset the hand data query status.
4545
/// </summary>
4646
public void Reset()
4747
{
4848
AlreadyFullQueried = false;
4949
FullQueryValid = false;
5050
}
5151

52-
53-
/// <summary>
54-
/// Implemented in derived classes. This method gets all of the joint poses for the hand.
52+
/// <summary>
53+
/// Implemented in derived classes. This method gets all of the joint poses for the hand.
5554
/// </summary>
5655
/// <param name="joints"> The returned list of HandJointPoses</param>
5756
/// <returns><see langword="true"/> if the query was successful, otherwise <see langword="false"/>.</returns>
5857
public abstract bool TryGetEntireHand(out IReadOnlyList<HandJointPose> joints);
5958

60-
/// <summary>
61-
/// Implemented in derived classes. This method gets the specified joint pose.
59+
/// <summary>
60+
/// Implemented in derived classes. This method gets the specified joint pose.
6261
/// </summary>
6362
/// <param name="joint">The TrackedHandJoint to retrieve the post for.</param>
6463
/// <param name="pose">The returned HandJointPose.</param>

org.mixedrealitytoolkit.input/Visualizers/ControllerVisualizer/ControllerVisualizer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the BSD 3-Clause
33

44
using MixedReality.Toolkit.Input.Simulation;
5-
using MixedReality.Toolkit.Subsystems;
65
using System;
76
using System.Threading.Tasks;
87
using UnityEngine;

0 commit comments

Comments
 (0)