Created
September 23, 2025 08:42
-
-
Save camnewnham/f460e97e4a7549cbc1aa9cd33b1c531f to your computer and use it in GitHub Desktop.
Shim class to allow FindObjectOfType to work in MRUK trackers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Reflection; | |
| using Unity.XR.CoreUtils; | |
| using UnityEngine; | |
| using UnityEngine.XR.OpenXR; | |
| /// <summary> | |
| /// Shim to make FindObjectOfType work | |
| /// </summary> | |
| [DefaultExecutionOrder(-1)] | |
| public class OVRShim : OVRCameraRig | |
| { | |
| protected override void Start() | |
| { | |
| } | |
| protected override void Awake() | |
| { | |
| if (!Application.isPlaying) return; | |
| var origin = FindFirstObjectByType<XROrigin>(); | |
| // "Move" the OVRShim to the XROrigin if it is not already there | |
| if (gameObject != origin.gameObject) | |
| { | |
| origin.gameObject.AddComponent<OVRShim>(); | |
| Destroy(this); | |
| return; | |
| } | |
| OverrideTrackingSpaceTransform(Camera.main.transform.parent.transform, this); | |
| OverrideCenterEyeAnchor(this); | |
| OpenXRSettings.SetAllowRecentering(true); | |
| OpenXRSettings.RefreshRecenterSpace(); | |
| } | |
| protected override void FixedUpdate() | |
| { | |
| } | |
| protected override void OnDestroy() | |
| { | |
| } | |
| private static void OverrideCenterEyeAnchor(OVRShim rig) | |
| { | |
| PropertyInfo property = typeof(OVRShim).GetProperty(nameof(centerEyeAnchor), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); | |
| Debug.Assert(property != null, "Property \"centerEyeAnchor\"was null"); | |
| property.SetValue(rig, Camera.main.transform); | |
| } | |
| private static void OverrideTrackingSpaceTransform(Transform xfm, OVRShim rig) | |
| { | |
| PropertyInfo property = typeof(OVRShim).GetProperty(nameof(trackingSpace), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); | |
| Debug.Assert(property != null, "Property \"trackingSpace\" was null"); | |
| property.SetValue(rig, xfm.transform); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment