Skip to content

Commit bedf131

Browse files
committed
Handle unavailable native QR bounds and clean up overlay UI lifecycle
1 parent d802117 commit bedf131

4 files changed

Lines changed: 50 additions & 39 deletions

File tree

‎Unity-QuestVisionKit/Assets/Editor/MaintenanceChecks.cs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static void Run()
4242
Require(SampleMenu.Label("Assets/Samples/3 QRCodeDetection/QRCodeDetection.unity") == "QR tracking (Meta native)",
4343
"Native QR must be clearly distinguished from the raw-camera sample.");
4444
Require(SampleMenu.AvailableScenes().Length == 6 && !SampleMenu.AvailableScenes().Any(path => path.Contains("WebRTC-SingleClient")), "The combined build must contain all six headset samples.");
45+
CheckNativeQrBounds();
4546
CheckWavStereo();
4647
CheckModelContract();
4748
var escape = typeof(ImageOpenAIConnector).GetMethod("EscapeJson", BindingFlags.NonPublic | BindingFlags.Instance);
@@ -60,6 +61,34 @@ public static void Run()
6061
Debug.Log("PASS: QuestCameraKit maintenance regression checks.");
6162
}
6263

64+
private static void CheckNativeQrBounds()
65+
{
66+
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(
67+
"Assets/Samples/3 QRCodeDetection/Prefabs/QRCodePrefab.prefab");
68+
Require(prefab, "Native QR overlay prefab must exist.");
69+
var instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
70+
try
71+
{
72+
var visualizer = instance.GetComponent<Meta.XR.MRUtilityKitSamples.QRCodeDetection.Bounded2DVisualizer>();
73+
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
74+
var type = visualizer.GetType();
75+
var line = (LineRenderer)type.GetField("_lineRenderer", flags).GetValue(visualizer);
76+
var canvas = (RectTransform)type.GetField("_canvasRect", flags).GetValue(visualizer);
77+
var update = type.GetMethod("SetBounds", flags);
78+
Require(line && canvas, "Native QR prefab needs its boundary and payload canvas.");
79+
update.Invoke(visualizer, new object[] { null });
80+
Require(!line.enabled && !canvas.gameObject.activeSelf, "Missing native bounds must hide the overlay.");
81+
update.Invoke(visualizer, new object[] { new Rect(1, 2, 3, 4) });
82+
Require(line.enabled && canvas.gameObject.activeSelf && line.positionCount == 4 &&
83+
line.GetPosition(0) == new Vector3(1, 2, 0) && line.GetPosition(2) == new Vector3(4, 6, 0),
84+
"Native bounds must draw the correct rectangle immediately after becoming available.");
85+
update.Invoke(visualizer, new object[] { null });
86+
Require(!line.enabled && !canvas.gameObject.activeSelf, "Losing native bounds must hide stale geometry.");
87+
Debug.Log("PASS: native QR overlay handles missing, available, and lost bounds.");
88+
}
89+
finally { Object.DestroyImmediate(instance); }
90+
}
91+
6392
private static void CheckModelContract()
6493
{
6594
var asset = AssetDatabase.LoadAssetAtPath<Unity.InferenceEngine.ModelAsset>(

‎Unity-QuestVisionKit/Assets/Samples/3 QRCodeDetection/Scripts/Bounded2DVisualizer.cs‎

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,53 +38,32 @@ public class Bounded2DVisualizer : MonoBehaviour
3838

3939
MRUKTrackable _trackable;
4040

41-
Rect _box;
42-
4341
public void Initialize(MRUKTrackable trackable)
4442
{
4543
_trackable = trackable;
46-
47-
if (trackable.PlaneBoundary2D == null && trackable.PlaneRect == null)
48-
{
49-
Debug.LogWarning($"{trackable} is missing a plane component.");
50-
}
51-
else
52-
{
53-
UpdateBoundingBox();
54-
}
44+
Update();
5545
}
5646

57-
void Update()
58-
{
59-
if (!_trackable)
60-
{
61-
return;
62-
}
63-
64-
UnityEngine.Assertions.Assert.IsTrue(_trackable.PlaneRect.HasValue);
65-
_box = _trackable.PlaneRect.Value;
66-
67-
UpdateBoundingBox();
68-
69-
if (!_canvasRect)
70-
{
71-
return;
72-
}
47+
void Update() => SetBounds(_trackable ? _trackable.PlaneRect : null);
7348

74-
_canvasRect.localPosition = new Vector3(
75-
x: _box.center.x + _canvasOffset.x * _canvasRect.localScale.x,
76-
y: _box.yMin + _canvasOffset.y * _canvasRect.localScale.y,
77-
z: _canvasOffset.z * _canvasRect.localScale.z
78-
);
79-
}
80-
81-
void UpdateBoundingBox()
49+
internal void SetBounds(Rect? bounds)
8250
{
51+
var visible = bounds.HasValue;
52+
if (_lineRenderer) _lineRenderer.enabled = visible;
53+
if (_canvasRect) _canvasRect.gameObject.SetActive(visible);
54+
if (!visible || !_lineRenderer) return;
55+
56+
var box = bounds.Value;
8357
_lineRenderer.positionCount = 4;
84-
_lineRenderer.SetPosition(0, new Vector3(_box.x, _box.y, 0));
85-
_lineRenderer.SetPosition(1, new Vector3(_box.x + _box.width, _box.y, 0));
86-
_lineRenderer.SetPosition(2, new Vector3(_box.x + _box.width, _box.y + _box.height, 0));
87-
_lineRenderer.SetPosition(3, new Vector3(_box.x, _box.y + _box.height, 0));
58+
_lineRenderer.SetPosition(0, new Vector3(box.xMin, box.yMin, 0));
59+
_lineRenderer.SetPosition(1, new Vector3(box.xMax, box.yMin, 0));
60+
_lineRenderer.SetPosition(2, new Vector3(box.xMax, box.yMax, 0));
61+
_lineRenderer.SetPosition(3, new Vector3(box.xMin, box.yMax, 0));
62+
if (!_canvasRect) return;
63+
_canvasRect.localPosition = new Vector3(
64+
box.center.x + _canvasOffset.x * _canvasRect.localScale.x,
65+
box.yMin + _canvasOffset.y * _canvasRect.localScale.y,
66+
_canvasOffset.z * _canvasRect.localScale.z);
8867
}
8968
}
9069
}

‎Unity-QuestVisionKit/Assets/Samples/3 QRCodeDetection/Scripts/QRCode.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public sealed class QRCode : MonoBehaviour
4343

4444
public void Initialize(MRUKTrackable trackable)
4545
{
46+
_text.richText = false;
4647
if (trackable.MarkerPayloadString is { } str)
4748
{
4849
_text.text = $"\"{str}\"";

‎Unity-QuestVisionKit/Assets/Samples/3 QRCodeDetection/Scripts/QRCodeSampleUI.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ void UpdateUI()
168168
_txtLogs.pageToDisplay = _txtLogs.textInfo?.pageCount ?? 1;
169169
}
170170

171+
void OnDisable() => StopAllCoroutines();
172+
171173
void OnEnable()
172174
{
173175
UpdateUI();

0 commit comments

Comments
 (0)