Search code examples
c#unity-game-engineoculus

Detecting Oculus HMD in Unity


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.


Solution

  • 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.