using UnityEngine; using Valve.VR; using System; public class WatchOverlay : MonoBehaviour { private ulong overlayHandle = OpenVR.k_ulOverlayHandleInvalid; private void Start() { InitOpenVR(); var key = "WatchOverlayKey"; var name = "WatchOverlay"; var error = OpenVR.Overlay.CreateOverlay(key, name, ref overlayHandle); if (error != EVROverlayError.None) { throw new Exception("Failed to create overlay: " + error); } } private void OnApplicationQuit() { if (overlayHandle != OpenVR.k_ulOverlayHandleInvalid) { var error = OpenVR.Overlay.DestroyOverlay(overlayHandle); if (error != EVROverlayError.None) { throw new Exception("Failed to dispose overlay: " + error); } } } private void OnDestroy() { ShutdownOpenVR(); } private void InitOpenVR() { if (OpenVR.System != null) return; var error = EVRInitError.None; OpenVR.Init(ref error, EVRApplicationType.VRApplication_Overlay); if (error != EVRInitError.None) { throw new Exception("Failed to initialize OpenVR: " + error); } } private void ShutdownOpenVR() { if (OpenVR.System != null) { OpenVR.Shutdown(); } } }