DEV Community

KOGA Mitsuhiro
KOGA Mitsuhiro

Posted on • Originally published at qiita.com

UnityでOculus向けの環境で壊れたOVRHeadsetEmulatorを直す

はじめに

UnityのOculus Integrationはエディタ上でカメラの動きをエミュレートできるOVRHeadsetEmulatorがあります。

image.png

このコンポーネントはOVRCameraRig.prefabOVRPlayerController.prefabにアタッチされているのですぐに使えるのですがv1.38とv1.39でOpenVRがサポートされた時に動かなくなってしまいました。せっかくの便利なコンポーネントなので動くように直してみました。
※1.37以前と1.40はそのままで動作します。

修正方法

InitOVRManager()を実行するかどうかの判定をエディタの時は無視します。これはOpenVRサポート前と同じ挙動です。

diff --git a/Assets/Oculus/VR/Scripts/OVRManager.cs b/Assets/Oculus/VR/Scripts/OVRManager.cs
index bff7d992..4f6f3d45 100644
--- a/Assets/Oculus/VR/Scripts/OVRManager.cs
+++ b/Assets/Oculus/VR/Scripts/OVRManager.cs
@@ -1294,7 +1294,9 @@ public class OVRManager : MonoBehaviour
        private void Awake()
        {
                //If OVRPlugin is initialized on Awake(), or if the device is OpenVR, OVRManager should be initialized right away.
+#if !UNITY_EDITOR
                if (OVRPlugin.initialized || (Settings.enabled && Settings.loadedDeviceName == OPENVR_UNITY_NAME_STR))
+#endif
                {
                        InitOVRManager();
                }
Enter fullscreen mode Exit fullscreen mode

別の方法

Oculusのフォーラムにも修正方法がありOVRPlugin.initializedを反転してInitOVRManager()を実行しています。ですがこのプロパティはドキュメントがなく挙動が分からないので上記のようにエディタの時だけ変更した方が安全です。

diff --git a/Assets/Oculus/VR/Scripts/OVRManager.cs b/Assets/Oculus/VR/Scripts/OVRManager.cs
index bff7d992..30ee8cde 100644
--- a/Assets/Oculus/VR/Scripts/OVRManager.cs
+++ b/Assets/Oculus/VR/Scripts/OVRManager.cs
@@ -1294,7 +1294,7 @@ public class OVRManager : MonoBehaviour
        private void Awake()
        {
                //If OVRPlugin is initialized on Awake(), or if the device is OpenVR, OVRManager should be initialized right away.
-               if (OVRPlugin.initialized || (Settings.enabled && Settings.loadedDeviceName == OPENVR_UNITY_NAME_STR))
+               if (!OVRPlugin.initialized || (Settings.enabled && Settings.loadedDeviceName == OPENVR_UNITY_NAME_STR))
                {
                        InitOVRManager();
                }
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay