private void OnDestroy() { #if UNITY_EDITOR UnityEditor.EditorApplication.playModeStateChanged -= EditorApplication_playModeStateChanged; #endif if (disposeAPI && api != null) { api.Dispose(); api = null; if (DisposedCallback != null) { DisposedCallback(); } } }
private IEnumerator Start() { // only one can exist in scene at a time if (singleton != null) { disposeAPI = false;// don't dispose apis if we're not the owner of possible native instances Destroy(gameObject); yield break; } DontDestroyOnLoad(gameObject); singleton = this; // print version Debug.Log("XRInput version: 1.0.11"); // wait for XR loader while (loader == null || !XRSettings.enabled) { try { if (loaderOverride != null) { loader = loaderOverride; } else { loader = XRGeneralSettings.Instance.Manager.activeLoader; } } catch { } yield return(new WaitForSeconds(1)); } // give XR some time to start var wait = new WaitForEndOfFrame(); yield return(wait); yield return(wait); yield return(wait); yield return(wait); yield return(wait); yield return(new WaitForSeconds(1)); try { // init controller instance IDs for (int i = 0; i != state_controllers.Length; ++i) { state_controllers[i].id = Guid.NewGuid(); } // get loader type var loaderType = loader.GetType(); string loaderTypeName = loaderType.Name; Debug.Log($"XR-Loader: '{loader.name}' TYPE:{loaderType}"); // auto set rumble channel if (autoSetRumbleChannel) { rumbleChannel = 0; } // auto detect if (apiType == XRInputAPIType.AutoDetect) { #if UNITY_STANDALONE if (loaderTypeName == "OpenVRLoader") { apiType = XRInputAPIType.OpenVR; } else { apiType = XRInputAPIType.UnityEngine_XR; } #else apiType = XRInputAPIType.UnityEngine_XR; #endif } // init api disposeAPI = true; switch (apiType) { case XRInputAPIType.UnityEngine_XR: api = new UnityEngine_XR(); break; case XRInputAPIType.OpenVR: api = new OpenVR_New(); break; case XRInputAPIType.OpenVR_Legacy: api = new OpenVR_Legacy(); break; default: throw new NotImplementedException(); } api.Init(); // ensure we dispose before stopping to avoid editor race-condition bugs #if UNITY_EDITOR UnityEditor.EditorApplication.playModeStateChanged += EditorApplication_playModeStateChanged; #endif apiInit = true; } catch (Exception e) { if (InitializedCallback != null) { InitializedCallback(false); } throw e; } if (InitializedCallback != null) { InitializedCallback(true); } }