Skip to content

Commit d23bfff

Browse files
authored
[Samples] Add a build flag for building with hand joint debugging hands (#1004)
* Add debug variants of hand models * Add code to actually patch Update BuildApp.cs Update BuildApp.cs * Update BuildApp.cs * Update ComponentExtensions.cs * Instead, build it on the fly Update BuildApp.cs Update BuildApp.cs Update BuildApp.cs * Add region and organize using statements
1 parent b9b827c commit d23bfff

File tree

3 files changed

+112
-6
lines changed

3 files changed

+112
-6
lines changed

UnityProjects/MRTKDevTemplate/Assets/BuildAssets/BuildApp.cs

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Copyright (c) Mixed Reality Toolkit Contributors
22
// Licensed under the BSD 3-Clause
33

4-
// Disable "missing XML comment" warning for samples. While nice to have, this XML documentation is not required for samples.
5-
#pragma warning disable CS1591
6-
4+
using MixedReality.Toolkit.Input;
75
using System;
86
using System.IO;
97
using System.Linq;
@@ -18,7 +16,7 @@ namespace MixedReality.Toolkit.Examples.Build
1816
/// </summary>
1917
public static class BuildApp
2018
{
21-
private static string[] scenes =
19+
private static string[] scenes =
2220
{
2321
"Assets/Scenes/BoundsControlExamples.unity",
2422
"Assets/Scenes/CanvasExample.unity",
@@ -151,6 +149,10 @@ private static void ParseBuildCommandLine()
151149
case "-buildOutput":
152150
buildPath = arguments[++i];
153151
break;
152+
case "-debug":
153+
// Add hand joints to hand visualization for debugging purposes
154+
PatchDebugHands();
155+
break;
154156
}
155157
}
156158
}
@@ -160,6 +162,107 @@ private static string[] SplitSceneList(string sceneList)
160162
return (from scene in sceneList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
161163
select scene.Trim()).ToArray();
162164
}
165+
166+
#region Hand Debug Patching
167+
168+
private const string LeftHandVisualizerGuid = "2b468cc4fe6d2b44ebc53b958b38b91a";
169+
private const string RightHandVisualizerGuid = "da93d751ddc0f64468dfc02f18d02d00";
170+
private const string HandJointMaterialGuid = "f115122e8379c044faecfec013fda057";
171+
172+
[MenuItem("Mixed Reality/MRTK3/Examples/Patch debug hand visualization...")]
173+
private static void PatchDebugHands() => PatchHands(true);
174+
175+
[MenuItem("Mixed Reality/MRTK3/Examples/Patch debug hand visualization...", true)]
176+
private static bool ValidatePatchDebugHands() => !AreHandsPatched();
177+
178+
[MenuItem("Mixed Reality/MRTK3/Examples/Unpatch debug hand visualization...")]
179+
private static void UnpatchDebugHands() => PatchHands(false);
180+
181+
[MenuItem("Mixed Reality/MRTK3/Examples/Unpatch debug hand visualization...", true)]
182+
private static bool ValidateUnpatchDebugHands() => AreHandsPatched();
183+
184+
/// <summary>
185+
/// Checks both hand prefabs for a <see cref="HandJointVisualizer"/>.
186+
/// </summary>
187+
/// <returns>Whether the left and right hands both have <see cref="HandJointVisualizer"/> scripts.</returns>
188+
private static bool AreHandsPatched()
189+
{
190+
bool isPatched = true;
191+
192+
string rightHandPath = AssetDatabase.GUIDToAssetPath(RightHandVisualizerGuid);
193+
{
194+
GameObject rightHandVisualizer = PrefabUtility.LoadPrefabContents(rightHandPath);
195+
if (rightHandVisualizer != null)
196+
{
197+
isPatched &= rightHandVisualizer.TryGetComponent<HandJointVisualizer>(out _);
198+
}
199+
PrefabUtility.UnloadPrefabContents(rightHandVisualizer);
200+
}
201+
202+
string leftHandPath = AssetDatabase.GUIDToAssetPath(LeftHandVisualizerGuid);
203+
{
204+
GameObject leftHandVisualizer = PrefabUtility.LoadPrefabContents(leftHandPath);
205+
if (leftHandVisualizer != null)
206+
{
207+
isPatched &= leftHandVisualizer.TryGetComponent<HandJointVisualizer>(out _);
208+
}
209+
PrefabUtility.UnloadPrefabContents(leftHandVisualizer);
210+
}
211+
212+
return isPatched;
213+
}
214+
215+
/// <summary>
216+
/// Updates both the left and right hand prefabs with <see cref="HandJointVisualizer"/> scripts.
217+
/// </summary>
218+
/// <param name="addDebug">If <see langword="true"/>, <see cref="HandJointVisualizer"/> will be added. If <see langword="false"/>, it'll be removed.</param>
219+
private static void PatchHands(bool addDebug)
220+
{
221+
string rightHandPath = AssetDatabase.GUIDToAssetPath(RightHandVisualizerGuid);
222+
{
223+
GameObject rightHandVisualizer = PrefabUtility.LoadPrefabContents(rightHandPath);
224+
if (rightHandVisualizer != null)
225+
{
226+
if (addDebug)
227+
{
228+
HandJointVisualizer visualizer = rightHandVisualizer.EnsureComponent<HandJointVisualizer>();
229+
visualizer.HandNode = UnityEngine.XR.XRNode.RightHand;
230+
visualizer.JointMaterial = AssetDatabase.LoadAssetAtPath<Material>(AssetDatabase.GUIDToAssetPath(HandJointMaterialGuid));
231+
visualizer.JointMesh = Resources.GetBuiltinResource<Mesh>("Cube.fbx");
232+
}
233+
else if (rightHandVisualizer.TryGetComponent(out HandJointVisualizer handJointVisualizer))
234+
{
235+
UnityEngine.Object.DestroyImmediate(handJointVisualizer);
236+
}
237+
PrefabUtility.SaveAsPrefabAsset(rightHandVisualizer, rightHandPath);
238+
}
239+
PrefabUtility.UnloadPrefabContents(rightHandVisualizer);
240+
}
241+
242+
string leftHandPath = AssetDatabase.GUIDToAssetPath(LeftHandVisualizerGuid);
243+
{
244+
GameObject leftHandVisualizer = PrefabUtility.LoadPrefabContents(leftHandPath);
245+
if (leftHandVisualizer != null)
246+
{
247+
if (addDebug)
248+
{
249+
HandJointVisualizer visualizer = leftHandVisualizer.EnsureComponent<HandJointVisualizer>();
250+
visualizer.HandNode = UnityEngine.XR.XRNode.LeftHand;
251+
visualizer.JointMaterial = AssetDatabase.LoadAssetAtPath<Material>(AssetDatabase.GUIDToAssetPath(HandJointMaterialGuid));
252+
visualizer.JointMesh = Resources.GetBuiltinResource<Mesh>("Cube.fbx");
253+
}
254+
#if UNITY_6000_0_OR_NEWER
255+
else
256+
{
257+
PrefabUtility.RemoveUnusedOverrides(new[] { leftHandVisualizer }, UnityEditor.InteractionMode.UserAction);
258+
}
259+
#endif
260+
PrefabUtility.SaveAsPrefabAsset(leftHandVisualizer, leftHandPath);
261+
}
262+
PrefabUtility.UnloadPrefabContents(leftHandVisualizer);
263+
}
264+
}
265+
266+
#endregion Hand Debug Patching
163267
}
164268
}
165-
#pragma warning restore CS1591

UnityProjects/MRTKDevTemplate/Assets/BuildAssets/MRTK.Examples.Build.asmdef

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "MixedReality.Toolkit.Examples.Build",
33
"rootNamespace": "",
44
"references": [
5+
"GUID:56255bd5d851a6243b63cb370cfc40b1",
6+
"GUID:d59347cf3d47ac148925927618efb1b5",
7+
"GUID:fe685ec1767f73d42b749ea8045bfe43",
58
"GUID:f9fe0089ec81f4079af78eb2287a6163",
69
"GUID:4847341ff46394e83bb78fbd0652937e"
710
],

org.mixedrealitytoolkit.core/Utilities/Extensions/ComponentExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ public static Component EnsureComponent(this GameObject gameObject, Type compone
6565
return gameObject.TryGetComponent(component, out Component foundComponent) ? foundComponent : gameObject.AddComponent(component);
6666
}
6767
}
68-
}
68+
}

0 commit comments

Comments
 (0)