XRDevice.isPresent’ is obsolete

I updated a project to 2022.2 and got the following error message for the WebVRController and Manager scripts:

XRDevice.isPresent’ is obsolete: ‘This is obsolete and should no longer be used. Instead, find the active XRDisplaySubsytem and check that the running property is true (for details, see XRDevice.isPresent documentation);

I’m an inexperienced Unity user and couldn’t figure out what to do and didn’t find any help in the manual.

Any suggestions?

1 Like

Hi, please check this page. It as a full code example on how to solve this:
Unity - Scripting API: XR.XRDevice.isPresent (unity3d.com)

Let me know, if you have further questions.

1 Like

I really appreciate your response, Unfortunately, I don’t understand how to use the info you referenced to change my files. Here’s the content of WebVRController.cs:

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.XR;
public enum WebVRControllerHand { NONE, LEFT, RIGHT };
[System.Serializable]
public class WebVRControllerButton
{
public bool pressed;
public bool prevPressedState;
public bool touched;
public float value;
public WebVRControllerButton(bool isPressed, float buttonValue)
{
pressed = isPressed;
prevPressedState = false;
value = buttonValue;
}
}
public class WebVRController : MonoBehaviour
{
[Tooltip(“Controller hand to use.”)]
public WebVRControllerHand hand = WebVRControllerHand.NONE;
[Tooltip(“Controller input settings.”)]
public WebVRControllerInputMap inputMap;
[Tooltip(“Simulate 3dof controller”)]
public bool simulate3dof = false;
[Tooltip(“Vector from head to elbow”)]
public Vector3 eyesToElbow = new Vector3(0.1f, -0.4f, 0.15f);
[Tooltip(“Vector from elbow to hand”)]
public Vector3 elbowHand = new Vector3(0, 0, 0.25f);
private Matrix4x4 sitStand;
private float[ ] axes;
private XRNode handNode;
private Quaternion headRotation;
private Vector3 headPosition;
private Dictionary<string, WebVRControllerButton> buttonStates = new Dictionary<string, WebVRControllerButton>();
// Updates button states from Web gamepad API.
private void UpdateButtons(WebVRControllerButton[ ] buttons)
{
for (int i = 0; i < buttons.Length; i++)
{
WebVRControllerButton button = buttons*;*
foreach (WebVRControllerInput input in inputMap.inputs)
{
if (input.gamepadId == i)
SetButtonState(input.actionName, button.pressed, button.value);
}
}
}
public float GetAxis(string action)
{
for (var i = 0; i < inputMap.inputs.Count; i++)
{
WebVRControllerInput input = inputMap.inputs*;*
if (action == input.actionName)
{
if (UnityEngine.XR.XRDevice.isPresent && !input.unityInputIsButton)
{
return Input.GetAxis(input.unityInputName);
}
else
{
if (input.gamepadIsButton)
{
if (!buttonStates.ContainsKey(action))
return 0;
return buttonStates[action].value;
}
else
return axes*;*
}
}
}
return 0;
}
public bool GetButton(string action)
{
if (UnityEngine.XR.XRDevice.isPresent)
{
foreach(WebVRControllerInput input in inputMap.inputs)
{
if (action == input.actionName && input.unityInputIsButton)
return Input.GetButton(input.unityInputName);
}
}
if (!buttonStates.ContainsKey(action))
return false;
return buttonStates[action].pressed;
}
private bool GetPastButtonState(string action)
{
if (!buttonStates.ContainsKey(action))
return false;
return buttonStates[action].prevPressedState;
}
private void SetButtonState(string action, bool isPressed, float value)
{
if (buttonStates.ContainsKey(action))
{
buttonStates[action].pressed = isPressed;
buttonStates[action].value = value;
}
else
buttonStates.Add(action, new WebVRControllerButton(isPressed, value));
}
private void SetPastButtonState(string action, bool isPressed)
{
if (!buttonStates.ContainsKey(action))
return;
buttonStates[action].prevPressedState = isPressed;
}
public bool GetButtonDown(string action)
{
// Use Unity Input Manager when XR is enabled and WebVR is not being used (eg: standalone or from within editor).
if (UnityEngine.XR.XRDevice.isPresent)
{
foreach(WebVRControllerInput input in inputMap.inputs)
{
if (action == input.actionName && input.unityInputIsButton)
return Input.GetButtonDown(input.unityInputName);
}
}
if (GetButton(action) && !GetPastButtonState(action))
{
SetPastButtonState(action, true);
return true;
}
return false;
}
public bool GetButtonUp(string action)
{
// Use Unity Input Manager when XR is enabled and WebVR is not being used (eg: standalone or from within editor).
if (UnityEngine.XR.XRDevice.isPresent)
{
foreach(WebVRControllerInput input in inputMap.inputs)
{
if (action == input.actionName && input.unityInputIsButton)
return Input.GetButtonUp(input.unityInputName);
}
}
if (!GetButton(action) && GetPastButtonState(action))
{
SetPastButtonState(action, false);
return true;
}
return false;
}
private void onHeadsetUpdate(Matrix4x4 leftProjectionMatrix,
Matrix4x4 rightProjectionMatrix,
Matrix4x4 leftViewMatrix,
Matrix4x4 rightViewMatrix,
Matrix4x4 sitStandMatrix)
{
Matrix4x4 trs = WebVRMatrixUtil.TransformViewMatrixToTRS(leftViewMatrix);
this.headRotation = WebVRMatrixUtil.GetRotationFromMatrix(trs);
this.headPosition = WebVRMatrixUtil.GetTranslationFromMatrix(trs);
this.sitStand = sitStandMatrix;
}
private void onControllerUpdate(string id,
int index,
string handValue,
bool hasOrientation,
bool hasPosition,
Quaternion orientation,
Vector3 position,
Vector3 linearAcceleration,
Vector3 linearVelocity,
WebVRControllerButton[ ] buttonValues,
float[ ] axesValues)
{
if (handFromString(handValue) == hand)
{
SetVisible(true);
Quaternion sitStandRotation = WebVRMatrixUtil.GetRotationFromMatrix(this.sitStand);
Quaternion rotation = sitStandRotation * orientation;
if (!hasPosition || this.simulate3dof) {
position = applyArmModel(
this.sitStand.MultiplyPoint(this.headPosition),
rotation,
this.headRotation);
}
else
{
position = this.sitStand.MultiplyPoint(position);
}
transform.rotation = rotation;
transform.position = position;
UpdateButtons(buttonValues);
this.axes = axesValues;
}
}
private WebVRControllerHand handFromString(string handValue)
{
WebVRControllerHand handParsed = WebVRControllerHand.NONE;
if (!String.IsNullOrEmpty(handValue)) {
try
{
handParsed = (WebVRControllerHand) Enum.Parse(typeof(WebVRControllerHand), handValue.ToUpper(), true);
}
catch
{
Debug.LogError(“Unrecognized controller Hand '” + handValue + “'!”);
}
}
return handParsed;
}
private void SetVisible(bool visible) {
Renderer[ ] rendererComponents = GetComponentsInChildren();
{
foreach (Renderer component in rendererComponents) {
component.enabled = visible;
}
}
}
*// Arm model adapted from: https://github.com/aframevr/aframe/blob/master/src/components/tracked-controls.js*_
private Vector3 applyArmModel(Vector3 controllerPosition, Quaternion controllerRotation, Quaternion headRotation)
{
// Set offset for degenerate “arm model” to elbow.
Vector3 deltaControllerPosition = new Vector3(
this.eyesToElbow.x * (this.hand == WebVRControllerHand.LEFT ? -1 : this.hand == WebVRControllerHand.RIGHT ? 1 : 0),
this.eyesToElbow.y,
this.eyesToElbow.z);
// Apply camera Y rotation (not X or Z, so you can look down at your hand).
Quaternion headYRotation = Quaternion.Euler(0, headRotation.eulerAngles.y, 0);
deltaControllerPosition = (headYRotation * deltaControllerPosition);
controllerPosition += deltaControllerPosition;
// Set offset for forearm sticking out from elbow.
deltaControllerPosition.Set(this.elbowHand.x, this.elbowHand.y, this.elbowHand.z);
deltaControllerPosition = Quaternion.Euler(controllerRotation.eulerAngles.x, controllerRotation.eulerAngles.y, 0) * deltaControllerPosition;
controllerPosition += deltaControllerPosition;
return controllerPosition;
}
void Update()
{
// Use Unity XR Input when enabled. When using WebVR, updates are performed onControllerUpdate.
if (XRDevice.isPresent)
{
SetVisible(true);
if (this.hand == WebVRControllerHand.LEFT)
handNode = XRNode.LeftHand;
if (this.hand == WebVRControllerHand.RIGHT)
handNode = XRNode.RightHand;
if (this.simulate3dof)
{
transform.position = this.applyArmModel(
InputTracking.GetLocalPosition(XRNode.Head), // we use head position as origin
InputTracking.GetLocalRotation(handNode),
InputTracking.GetLocalRotation(XRNode.Head)
);
transform.rotation = InputTracking.GetLocalRotation(handNode);
}
else
{
transform.position = InputTracking.GetLocalPosition(handNode);
transform.rotation = InputTracking.GetLocalRotation(handNode);
}
foreach(WebVRControllerInput input in inputMap.inputs)
{
if (!input.unityInputIsButton)
{
if (Input.GetAxis(input.unityInputName) != 0)
SetButtonState(input.actionName, true, Input.GetAxis(input.unityInputName));
if (Input.GetAxis(input.unityInputName) < 1)
SetButtonState(input.actionName, false, Input.GetAxis(input.unityInputName));
}
}
}
}
void OnEnable()
{
if (inputMap == null)
{
Debug.LogError(“A Input Map must be assigned to WebVRController!”);
return;
}
WebVRManager.Instance.OnControllerUpdate += onControllerUpdate;
WebVRManager.Instance.OnHeadsetUpdate += onHeadsetUpdate;
SetVisible(false);
}
void OnDisabled()
{
WebVRManager.Instance.OnControllerUpdate -= onControllerUpdate;
WebVRManager.Instance.OnHeadsetUpdate -= onHeadsetUpdate;
SetVisible(false);
}
}_

Please perform the following:

  1. In your project panel right click and select Create → C# Script
  2. Name the new script file “ExampleUtil”
  3. Copy this content into the script file:
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
internal static class ExampleUtil
{
    public static bool isPresent()
    {
        var xrDisplaySubsystems = new List<XRDisplaySubsystem>();
        SubsystemManager.GetInstances<XRDisplaySubsystem>(xrDisplaySubsystems);
        foreach (var xrDisplay in xrDisplaySubsystems)
        {
            if (xrDisplay.running)
            {
                return true;
            }
        }
        return false;
    }
}
  1. In your own script you posed above, replace any occurence of “XRDevice.isPresent” or "UnityEngine.XR.XRDevice.isPresent" with this:
ExampleUtil.isPresent()

Once everything works you might want to rename “ExampleUtil” to something better.

14 Likes

Worked! Thank you, Din0m1te!

1 Like

Hello,i tried thé ExampleUtil méthod but there IS another error which pops up , (error CS0103:Thé name ‘ExampleUtil’ does not exist in the current context).
I’m also New to unity ,actually using unity 2020.

It worked, Thx!

@Damedefer ExampleUtil ist the script that you need to create in order for this to work. Please see my answer above. Create a script file and name it “ExampleUtil”, then copy the content inside that I posted above. Only then this class will be available for your other scripts to use.

thx. It works effectively on my code.

For anyone experiencing the issue that at @Damedefer described… you need to comment out all instances of XRDevice.isPresent in your scripts then allow Unity to recompile. The compiler errors cause by the obsolete method where preventing Unity from properly compiling ExampleUtil (at least for me) and thus it was not finding the class in the context when I went to do the replace XRDevice with ExampleUtil.

Hello
If anyone is still facing the “‘ExampleUtil’ does not exist in the current context” issue
When I created ExampleUtil, I had put it in the Assets Folder and it gave that error
When I moved it to “Assets/WebXR/Scripts/” this has solved the issue
Hope that helps

2 Likes

Gracias! Thanks!

IT WORKED when I moved the script to “Assets/WebXR/Scripts/”. It worked, Thank you.

Thank you guys for all the answers. It made sense to move it to “Assets/WebXR/Scripts/”