【Unity3D自学记录】开发PicoVR之获取手柄的方法

获取手柄有两种方法,一种是XR的方法,一种是PicoSDK中的方法。
第一种,XR方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;

public class PicoVRCS : MonoBehaviour
{
    List<InputDevice> foundControllers = new List<InputDevice>();
    // Start is called before the first frame update
    void Start()
    {
        InputDeviceCharacteristics leftTrackedControllerFilter = InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.Left;   
        InputDevices.GetDevicesWithCharacteristics(leftTrackedControllerFilter, foundControllers);
    }

    void Update()
    {
        foundControllers[0].TryGetFeatureValue(CommonUsages.primaryButton, out bool isPress);
        if (isPress)
        {
            
        }
    }
}

第二种是PicoSDK中的方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.XR;

public class PicoVRControl : MonoBehaviour
{
    private static PicoVRControl ins;
    
    public static PicoVRControl Instance { get { return ins; } }


    public XRController LeftControl;
    public XRController RightControl;

    private bool isWait = false;
    // Start is called before the first frame update
    void Awake()
    {
        if (!ins) ins= this;

    }

    void Update()
    {
        Vector2 result;
        bool success = LeftControl.inputDevice.TryGetFeatureValue(CommonUsages.primary2DAxis, out result);
        bool success1 = RightControl.inputDevice.TryGetFeatureValue(CommonUsages.primary2DAxis, out result);
        if (success || success1)
        {
            Vector3 pos = CenterObj.position;
            float value = 2.5f;
            CenterObj.position = new Vector3(pos.x, pos.y + value * result.y, pos.z);
        }

        LeftControl.inputDevice.TryGetFeatureValue(CommonUsages.primaryButton, out bool isPressed);
        RightControl.inputDevice.TryGetFeatureValue(CommonUsages.primaryButton, out bool isPressed1);
        if ((isPressed || isPressed1) && !isWait)
        {

        }
    }
}

猜你喜欢

转载自blog.csdn.net/hackdjh/article/details/127584660
今日推荐