05-11-2020 09:02 AM
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public static class EditorOVRCrashCatcher
{
private const bool DestroyRig = true;
private const bool DestroyAvatar = true;
// register an event handler when the class is initialized
static EditorOVRCrashCatcher()
{
AssemblyReloadEvents.beforeAssemblyReload += DestroyOVR;
}
private static void DestroyOVR()
{
if (!EditorApplication.isPlaying)
return;
//Debug.Log("Before Assembly Reload");
GameObject rig = Object.FindObjectOfType<OVRCameraRig>()?.transform.root.gameObject;
GameObject avatar = Object.FindObjectOfType<OvrAvatarSDKManager>()?.gameObject;
if (DestroyRig && rig)
{
Debug.Log("Destroying OVR Instance");
Object.Destroy(rig);
}
if (DestroyAvatar && avatar)
{
Debug.Log("Destroying OVR Instance");
Object.Destroy(avatar); //Throws errors once ovr is destroyed.
}
EditorApplication.ExitPlaymode();
//Haven't figured out a way to restart play mode so I'll just stop here.
}
}
#endif