public Transform OculusPlayerPrefab;
public Transform DefaultPlayerPrefab;
void Start() {
Transform player = OVRDevice.IsHMDPresent() ?
(Transform)Instantiate(OculusPlayerPrefab) :
(Transform)Instantiate(DefaultPlayerPrefab);
player.position = transform.position;
}
This should detect if the oculus rift HMD is connected and instantiate the oculus player prefab, otherwise the default. However, IsHMDPresent()
returns false whether the Oculus Rift is connected or not. In the unity/oculus integration package however, OVRMainMenu uses the IsHMDPresent() method with the expected results.
Go to the Unity Asset Store tab(Click Window > Asset Store) in Unity and search Oculus Integration in the search field at the top. Download and import the Oculus Integration into your project. At the end of the import you may see a prompt that says API update required. Click on “I made a backup, go ahead!”
Enabling Unity VR support To enable VR for your game builds and the editor, open the Player Settings (menu: Edit > Project Settings > Player). Select Other Settings and check the Virtual Reality Supported checkbox. Set this for each build target.
As of (at least) Unity 2018.2, using the Oculus Utilities, the following works:
if (OVRManager.isHMDPresent) {
// headset connected
}
I'll add that you can subscribe to HMDMounted
and HMDUnmounted
events as well which is somewhat related:
OVRManager.HMDMounted += MyOnHMDMountedFunction();
OVRManager.HMDUnmounted += MyOnHMDUnmountedFunction();
Those will fire when you put on (HMDMounted) and/or take off (HMDUnmounted) your headset.
Unity now has a built-in way to detect this.
http://forum.unity3d.com/threads/simply-detecting-the-oculus-rifts-presence-solved.294089/#post-2368233
Docs: http://docs.unity3d.com/ScriptReference/VR.VRDevice-isPresent.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With