如何将您的 Unity 项目 Input 更新为 SteamVR 2.0? [英] How to update your Unity project Input to SteamVR 2.0?

查看:30
本文介绍了如何将您的 Unity 项目 Input 更新为 SteamVR 2.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 Unity 场景可以很好地与以前版本的 SteamVR 插件配合使用,因为有一个新版本的插件SteamVR Unity Plugin 2.0",我的代码不再有效.

在姿势动作"字段上,在输入源"上应该是:

控制器(左)

姿势动作:SkeletonLeftHand

输入源:左手

控制器(右)

姿势动作:SkeletonRightHand

输入源:右手

4) 将手写脚本添加到您的控制器(左)"和控制器(右)"对象:

5) 将您自己的自定义脚本添加到您的控制器(左)"和控制器(右)"对象,在我的例子中是HTC Vivie Input"脚本.

6) 确保您没有任何编译错误,在这种情况下,您应该能够在 Unity 的窗口菜单上看到SteamVR Input"和SteamVR Input Live View",

7) 例如默认情况下菜单按钮不包含任何关联的操作,或触摸板位置,因此打开SteamVR 输入"菜单,并添加操作:

  • 触摸板

  • touchPos

  • 菜单按钮

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

将菜单"与菜单按钮"操作相关联.

将触摸"与触摸板"操作相关联.

将位置"与touchPos"操作相关联.

然后从SteamVR 输入"菜单中按保存并生成按钮

9) 打开您的自定义脚本(在我的例子中是HTC Vivie Input")并添加您的代码,例如:

使用UnityEngine;使用 Valve.VR;使用 Valve.VR.InteractionSystem;公共类 HTCVivieInput : MonoBehaviour {私人手;//用于初始化无效开始(){hand = gameObject.GetComponent();}公共 Vector2 getTrackPadPos(){SteamVR_Action_Vector2 trackpadPos = SteamVR_Input._default.inActions.touchPos;返回 trackpadPos.GetAxis(hand.handType);}public bool getPinch(){返回 SteamVR_Input._default.inActions.GrabPinch.GetState(hand.handType);}public bool getPinchDown(){返回 SteamVR_Input._default.inActions.GrabPinch.GetStateDown(hand.handType);}public bool getPinchUp(){返回 SteamVR_Input._default.inActions.GrabPinch.GetStateUp(hand.handType);}公共布尔 getGrip(){返回 SteamVR_Input._default.inActions.GrabGrip.GetState(hand.handType);}public bool getGrip_Down(){返回 SteamVR_Input._default.inActions.GrabGrip.GetStateDown(hand.handType);}public bool getGrip_Up(){返回 SteamVR_Input._default.inActions.GrabGrip.GetStateUp(hand.handType);}public bool getMenu(){返回 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(){返回 SteamVR_Input._default.inActions.MenuButton.GetStateUp(hand.handType);}public bool getTouchPad(){返回 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(){返回 SteamVR_Input._default.inActions.Teleport.GetStateUp(hand.handType);}公共 Vector3 getControllerPosition(){SteamVR_Action_Pose[]poseActions = SteamVR_Input._default.poseActions;如果(poseActions.Length > 0){返回poseActions[0].GetLocalPosition(hand.handType);}返回新的 Vector3(0, 0, 0);}公共四元数 getControllerRotation(){SteamVR_Action_Pose[]poseActions = SteamVR_Input._default.poseActions;如果(poseActions.Length > 0){返回poseActions[0].GetLocalRotation(hand.handType);}返回 Quaternion.identity;}}

10) 发布版本时,从绑定 UI"菜单中替换默认绑定

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?

解决方案

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

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:

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

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,

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

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.

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

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

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

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

这篇关于如何将您的 Unity 项目 Input 更新为 SteamVR 2.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆