Skip to content

Instantly share code, notes, and snippets.

@Kroporo
Created November 15, 2022 15:10
Show Gist options
  • Save Kroporo/f7201d7c9ce6dd015a461992c62cb946 to your computer and use it in GitHub Desktop.
Save Kroporo/f7201d7c9ce6dd015a461992c62cb946 to your computer and use it in GitHub Desktop.
Workaround to console spam caused by `OpenXRRestarter` introduced in `com.unity.xr.openxr@1.5.1` -> "Failure to restart OpenXRLoader after shutdown"
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;
// This class finds and kills a coroutine that throws errors inside the editor every 5 seconds if no headset is connected
// The coroutine was introduced in the OpenXR Plugin [1.5.1] - 2022-08-11
// There absolutely has to be a better way, and this code should NOT be maintained incase the issue is resolved
public class OpenXRRestarterKiller : MonoBehaviour {
private static bool isHookedIntoUpdate = false;
private static object restarterInstance = null;
private static Type restarterType = null;
private static FieldInfo singletonInstanceField = null;
private static FieldInfo restarterCoroutine = null;
private static MethodInfo stopMethod = null;
[InitializeOnLoadMethod]
[ExecuteInEditMode]
private static void Init() {
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}
private static void OnPlayModeStateChanged(PlayModeStateChange change) {
switch (change) {
case PlayModeStateChange.EnteredPlayMode:
GatherReflectionData();
SetHooked(true);
break;
case PlayModeStateChange.ExitingPlayMode:
SetHooked(false);
break;
}
}
private static void GatherReflectionData() {
restarterType = Type.GetType("UnityEngine.XR.OpenXR.OpenXRRestarter, Unity.XR.OpenXR, Version=0.0.0.0, Culture=neutral, PublicKeyToken=nul", true);
singletonInstanceField = restarterType.GetField("s_Instance", BindingFlags.NonPublic | BindingFlags.Static);
restarterCoroutine = restarterType.GetField("m_pauseAndRestartCoroutine", BindingFlags.NonPublic | BindingFlags.Instance);
stopMethod = typeof(MonoBehaviour).GetMethod("StopCoroutine", new Type[] { typeof(Coroutine) });
}
// Enables/Disables looking for the restarter coroutine
private static void SetHooked(bool isHooked) {
if (isHookedIntoUpdate == isHooked)
return; // Already have the desired state
isHookedIntoUpdate = isHooked;
if (isHooked) {
EditorApplication.update += OnUpdate;
} else {
EditorApplication.update -= OnUpdate;
}
}
private static void OnUpdate() {
// Search for the singleton instance, we run before it initializes
if (restarterInstance == null && singletonInstanceField.GetValue(null) != null) {
restarterInstance = singletonInstanceField.GetValue(null);
}
// Check if we have an instance with an active restarter coroutine.
if (restarterInstance != null && restarterCoroutine.GetValue(singletonInstanceField.GetValue(null)) != null) {
Debug.Log("Killing internal OpenXR restart coroutine after connection failure! This is an editor hack to avoid reoccuring reconnection errors!");
stopMethod.Invoke(restarterInstance, new object[] { restarterCoroutine.GetValue(restarterInstance) });
restarterCoroutine.SetValue(restarterInstance, null);
// Our job is done, no reason to linger anymore
SetHooked(false);
}
}
}
@fabio1955
Copy link

Warning:: Copy this code to the Assset/Editor folder

@Kroporo
Copy link
Author

Kroporo commented Mar 14, 2023

Warning:: Copy this code to the Assset/Editor folder

As you should with every editor script! Though the issue this solves has been resolved in a recent update to the OpenXR package, so this shouldn't be needed anymore.

@fabio1955
Copy link

Warning:: Copy this code to the Assset/Editor folder

As you should with every editor script! Though the issue this solves has been resolved in a recent update to the OpenXR package, so this shouldn't be needed anymore.

I still have the problem with Version 4.3.3 - February 16, 2023 Unity 2021.3.15f1 Personal. I could not build without your script

@Kroporo
Copy link
Author

Kroporo commented Mar 16, 2023

I still have the problem with Version 4.3.3 - February 16, 2023 Unity 2021.3.15f1 Personal. I could not build without your script

According to the OpenXR 1.6.0 (2022-11-29) changelog, this has been fixed. We personally don't use this workaround anymore, but I am glad to hear that the script helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment