Search code examples
c#unity-game-enginepluginssteamvr

How to update your Unity project Input to SteamVR 2.0?


I have some Unity Scenes that worked well with the previous version of the SteamVR plugin, since there is a new version of the plugin "SteamVR Unity Plugin 2.0" my code no longer works.

https://steamcommunity.com/games/250820/announcements/detail/1696059027982397407

I deleted the "SteamVR" folder before importing the new one as the documentation say.

But I get this errors:

error CS0246: The type or namespace name `SteamVR_Controller' could not be found. Are you missing an assembly reference?
error CS0246: The type or namespace name `SteamVR_TrackedController' could not be found. Are you missing an assembly reference?

So I can see that this classes are deprecated:

private SteamVR_Controller.Device device;
private SteamVR_TrackedController controller;
controller = GetComponent<SteamVR_TrackedController>();

What is the new way to get the Input by code using the SteamVR 2.0 plugin?


Solution

  • To move to SteamVR 2.0 I followed this steps:

    1) Delete the "SteamVR" folder, and then import the "SteamVR" plugin from the Unity Asset Store.

    2) Delete your previous "[CameraRig]" object from your scenes and drag the new one located on: "SteamVR/Prefabs"

    3) Check for the script "Steam VR_Behaviour_Pose" on the "Controller (left)", and "Controller (right)" objects enter image description here enter image description here

    there on the "Pose Action" field, and on "Input Source" should be:

    Controller (left)

    Pose Action: SkeletonLeftHand

    Input Source: Left Hand

    Controller (right)

    Pose Action: SkeletonRightHand

    Input Source: right Hand

    4) Add hand script to your "Controller (left)" and "Controller (right)" objects:

    enter image description here

    5) Add your own custom script to your "Controller (left)" and "Controller (right)" objects, in my case "HTC Vivie Input" script.

    enter image description here

    6) Make sure you do not have any compilation errors, in that case you should be able to see the "SteamVR Input" and "SteamVR Input Live View" on window menu from Unity, enter image description here

    7) By default for example the Menu button does not contain any Action asociated, or the Touch pad position, so open the "SteamVR Input" menu, and add the actions:

    • touchPad

    • touchPos

    • MenuButton

    enter image description here\ enter image description here enter image description here enter image description here

    8) Click on the "Open binding UI" button while your SteamVR service is running, and edit the current binding

    Asociate the "Menu" with the "MenuButton" action.

    enter image description here

    Asociate the "Touch" with the "touchPad" action.

    Asociate the "Position" with the "touchPos" action.

    enter image description here

    Then press the Save and generate button from the "SteamVR Input" menu

    enter image description here

    9) Open your custom script ("HTC Vivie Input" in my case) and add your code, for example:

    using UnityEngine;
    using Valve.VR;
    using Valve.VR.InteractionSystem;
    
    public class HTCVivieInput : MonoBehaviour {
    
        private Hand hand;
    
        // Use this for initialization
        void Start () {
            hand = gameObject.GetComponent<Hand>();
        }
    
        public Vector2 getTrackPadPos()
        {
            SteamVR_Action_Vector2 trackpadPos = SteamVR_Input._default.inActions.touchPos;
            return trackpadPos.GetAxis(hand.handType);
        }
    
        public bool getPinch()
        {
            return SteamVR_Input._default.inActions.GrabPinch.GetState(hand.handType);
        }
    
        public bool getPinchDown()
        {
            return SteamVR_Input._default.inActions.GrabPinch.GetStateDown(hand.handType);
        }
    
        public bool getPinchUp()
        {
            return SteamVR_Input._default.inActions.GrabPinch.GetStateUp(hand.handType);
        }
    
        public bool getGrip()
        {
            return SteamVR_Input._default.inActions.GrabGrip.GetState(hand.handType);
        }
    
        public bool getGrip_Down()
        {
            return SteamVR_Input._default.inActions.GrabGrip.GetStateDown(hand.handType);
        }
    
        public bool getGrip_Up()
        {
            return SteamVR_Input._default.inActions.GrabGrip.GetStateUp(hand.handType);
        }
    
        public bool getMenu()
        {
            return SteamVR_Input._default.inActions.MenuButton.GetState(hand.handType);
        }
    
        public bool getMenu_Down()
        {
            return SteamVR_Input._default.inActions.MenuButton.GetStateDown(hand.handType);
        }
    
        public bool getMenu_Up()
        {
            return SteamVR_Input._default.inActions.MenuButton.GetStateUp(hand.handType);
        }
    
        public bool getTouchPad()
        {
            return SteamVR_Input._default.inActions.Teleport.GetState(hand.handType);
        }
    
        public bool getTouchPad_Down()
        {
            return SteamVR_Input._default.inActions.Teleport.GetStateDown(hand.handType);
        }
    
        public bool getTouchPad_Up()
        {
            return SteamVR_Input._default.inActions.Teleport.GetStateUp(hand.handType);
        }
    
        public Vector3 getControllerPosition()
        {
            SteamVR_Action_Pose[] poseActions = SteamVR_Input._default.poseActions;
            if (poseActions.Length > 0)
            {
                return poseActions[0].GetLocalPosition(hand.handType);
            }
            return new Vector3(0, 0, 0);
        }
    
        public Quaternion getControllerRotation()
        {
            SteamVR_Action_Pose[] poseActions = SteamVR_Input._default.poseActions;
            if (poseActions.Length > 0)
            {
                return poseActions[0].GetLocalRotation(hand.handType);
            }
            return Quaternion.identity;
        }
    }
    

    10) When making a release build replace the default bindings from the "binding UI" menu

    enter image description here