如何将Unity项目输入更新至SteamVR 2.0?

8

我有一些Unity场景,之前使用的SteamVR插件版本很好,但是由于出现了新版本的插件“SteamVR Unity Plugin 2.0”,我的代码不再起作用。

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

我按照文档的说法,在导入新文件夹前删除了“SteamVR”文件夹。但是我收到了以下错误信息:
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?

所以我可以看到这些类已经被弃用:

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

使用SteamVR 2.0插件,通过代码获取输入的新方法是什么?

1个回答

26

要切换到SteamVR 2.0,我按照以下步骤进行:

1) 删除"SteamVR"文件夹,然后从Unity Asset Store导入"SteamVR"插件。

2) 从场景中删除以前的"[CameraRig]"对象,并将位于"SteamVR/Prefabs"上的新对象拖动到场景中。

3) 在"Controller (left)"和"Controller (right)"对象上检查脚本"Steam VR_Behaviour_Pose" enter image description here enter image description here

在"Pose Action"字段和"Input Source"字段上应该是:

Controller (left)

Pose Action: SkeletonLeftHand

Input Source: Left Hand

Controller (right)

Pose Action: SkeletonRightHand

Input Source: right Hand

4) 将手部脚本添加到"Controller (left)"和"Controller (right)"对象中:

enter image description here

5) 在您的“控制器(左)”和“控制器(右)”对象中添加自己的自定义脚本,在我的情况下是“HTC Vivie Input”脚本。

enter image description here

6)确保没有编译错误,如果有错误,你应该能在Unity的窗口菜单中看到“SteamVR输入”和“SteamVR输入实时视图”, enter image description here 7)默认情况下,例如菜单按钮不包含任何关联操作,或者触摸板位置,因此打开“SteamVR输入”菜单,并添加以下操作: - touchPad - touchPos - MenuButton

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

8) 当您的SteamVR服务正在运行时,单击“打开绑定UI”按钮,并编辑当前绑定

将“菜单”与“MenuButton”操作相关联。

enter image description here

将“Touch”与“touchPad”操作相关联。
将“Position”与“touchPos”操作相关联。

enter image description here

然后从“SteamVR输入”菜单中按下保存和生成按钮

enter image description here

9) 打开您的自定义脚本(在我的情况下是“HTC Vivie Input”),并添加您的代码,例如:

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) 在制作发布版本时,从“绑定UI”菜单中替换默认绑定。

enter image description here


3
嘿!我只是想说你的指南是我迄今为止找到的最好的。谢谢你花时间编写所有这些,它将是巨大的帮助。 - BlackDragonBE

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接