Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

XRDevice.isPresent’ is obsolete

Discussion in 'Editor & General Support' started by dsantamonica, Feb 14, 2021.

  1. dsantamonica

    dsantamonica

    Joined:
    Jan 24, 2019
    Posts:
    22
    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?
     
    YvanDev5 likes this.
  2. Din0m1te

    Din0m1te

    Joined:
    Sep 9, 2015
    Posts:
    34
    kamiyama-yuya likes this.
  3. dsantamonica

    dsantamonica

    Joined:
    Jan 24, 2019
    Posts:
    22
    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<Renderer>();
    {
    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);
    }
    }
     
  4. Din0m1te

    Din0m1te

    Joined:
    Sep 9, 2015
    Posts:
    34
    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:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5. internal static class ExampleUtil
    6. {
    7.     public static bool isPresent()
    8.     {
    9.         var xrDisplaySubsystems = new List<XRDisplaySubsystem>();
    10.         SubsystemManager.GetInstances<XRDisplaySubsystem>(xrDisplaySubsystems);
    11.         foreach (var xrDisplay in xrDisplaySubsystems)
    12.         {
    13.             if (xrDisplay.running)
    14.             {
    15.                 return true;
    16.             }
    17.         }
    18.         return false;
    19.     }
    20. }
    4. In your own script you posed above, replace any occurence of "XRDevice.isPresent" or "UnityEngine.XR.XRDevice.isPresent" with this:

    Code (CSharp):
    1. ExampleUtil.isPresent()
    Once everything works you might want to rename "ExampleUtil" to something better.
     
  5. dsantamonica

    dsantamonica

    Joined:
    Jan 24, 2019
    Posts:
    22
    Worked! Thank you, Din0m1te!
     
    Din0m1te likes this.
  6. Damedefer

    Damedefer

    Joined:
    Jul 28, 2021
    Posts:
    1
    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.
     
  7. reginaLunar

    reginaLunar

    Joined:
    Mar 21, 2018
    Posts:
    2
    It worked, Thx!

     
  8. Din0m1te

    Din0m1te

    Joined:
    Sep 9, 2015
    Posts:
    34
    @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.
     
  9. Dangoring

    Dangoring

    Joined:
    Oct 26, 2021
    Posts:
    1
    thx. It works effectively on my code.

     
  10. Mlorenz821

    Mlorenz821

    Joined:
    Jan 8, 2016
    Posts:
    1
    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.
     
  11. YvanDev5

    YvanDev5

    Joined:
    Jun 13, 2022
    Posts:
    1
    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
     
    DARKANANS likes this.
  12. Bloodmindd

    Bloodmindd

    Joined:
    Apr 9, 2022
    Posts:
    1
    Gracias! Thanks!
     
  13. maady634

    maady634

    Joined:
    Nov 10, 2022
    Posts:
    1
    IT WORKED when I moved the script to "Assets/WebXR/Scripts/". It worked, Thank you.
     
  14. XMaymo

    XMaymo

    Joined:
    Mar 2, 2017
    Posts:
    5
    Thank you guys for all the answers. It made sense to move it to "Assets/WebXR/Scripts/"