Quest2 在Unity中获取手柄的输入,以及检测各个按键是否被按下,以及控制手柄振动功能

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

public class Xr_input_Record : MonoBehaviour
{
    //private UnityEngine.XR.InputDevice device;
    // Start is called before the first frame update
    public UnityEngine.XR.InputDevice leftHand_device;
    public float 振幅=0.5f;
    public uint 频道=0;
    public float 持续时长=1f;
    void Start()
    {
          
           // Allinput_device_record();
          //  get_left_hand();
        
    }
    private void OnEnable() {
        // Allinput_device_record();
         //   get_left_hand();
          //  display_device_featureName();
        get_left_hand();
          控制器振动功能();
        
    }

    public void Allinput_device_record()
    {
        var inputDevices = new List<UnityEngine.XR.InputDevice>();
        UnityEngine.XR.InputDevices.GetDevices(inputDevices);

        foreach (var device_a in inputDevices)
        {
            Debug.Log(string.Format("设备名字 '{0}' 设备角色 '{1}'", device_a.name, device_a.role.ToString()));
        }
    }

    public void get_left_hand()
    {
        var leftHandedControllers = new List<UnityEngine.XR.InputDevice>();
        var desiredCharacteristics = UnityEngine.XR.InputDeviceCharacteristics.HeldInHand | UnityEngine.XR.InputDeviceCharacteristics.Left | UnityEngine.XR.InputDeviceCharacteristics.Controller;
        UnityEngine.XR.InputDevices.GetDevicesWithCharacteristics(desiredCharacteristics, leftHandedControllers);

        foreach (var device in leftHandedControllers)
        {
            Debug.Log(string.Format("左手设备名 '{0}' 该设备具有的特征 '{1}'", device.name, device.characteristics.ToString()));
            leftHand_device=device;
        }
    }

    public void display_device_featureName()
    {
        var inputFeatures = new List<UnityEngine.XR.InputFeatureUsage>();
        if (leftHand_device.TryGetFeatureUsages(inputFeatures)) //用于获取设备的使用表格
        {
            foreach (var feature in inputFeatures)
            {
                if (feature.type == typeof(bool))
                {
                    bool featureValue;
                    if (leftHand_device.TryGetFeatureValue(feature.As<bool>(), out featureValue))
                    {
                        Debug.Log(string.Format("功能名字 {0}'s 功能的值 {1}", feature.name, featureValue.ToString()));
                    }
                }
            }
        }
    }


    public void update_inputDynamic_record()
    {
           
            bool triggerValue;
            //知道按键名字,直接根据名字获得相对应的值
            if (leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.gripButton, out triggerValue) && triggerValue)
            {
                Debug.Log("按下grip button");
            }
            else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.menuButton, out triggerValue) && triggerValue)
            {
                     Debug.Log("按下menu button");
            }
            else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primaryButton, out triggerValue) && triggerValue)
            {
                     Debug.Log("按下Primary button");
            }
            else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primaryTouch, out triggerValue) && triggerValue)
            {
                     Debug.Log("按下Primary touch");
            }
            else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.secondaryButton, out triggerValue) && triggerValue)
            {
                     Debug.Log("按下second button");
            }
            else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.secondaryTouch, out triggerValue) && triggerValue)
            {
                     Debug.Log("按下second touch");
            }
            else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.triggerButton, out triggerValue) && triggerValue)
            {
                     Debug.Log("按下trigger button");
            }
            else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxisClick, out triggerValue) && triggerValue)
            {
                     Debug.Log("按下primary 2D click button");
            }
            else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxisTouch, out triggerValue) && triggerValue)
            {
                     Debug.Log("按下primary 2D click touch");
            }
             
            /*else if(leftHand_device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.isTracked, out triggerValue) && triggerValue)
            {
                     Debug.Log("按下is_tracked touch");
            }*/
    }

    public void 控制器振动功能()
    {
       // List<UnityEngine.XR.InputDevice> devices = new List<UnityEngine.XR.InputDevice>(); 

           print("A1");
            UnityEngine.XR.HapticCapabilities capabilities;
            if (leftHand_device.TryGetHapticCapabilities(out capabilities))
            {
                     print("A2");
           
                    if (capabilities.supportsImpulse)
                    {
                         print("A3");
                        uint channel = 0;
                       // float amplitude = 5f;
                        float duration = 5.0f;
                        leftHand_device.SendHapticImpulse(频道, 振幅, 持续时长);
                    }
            }
        
    }

    // Update is called once per frame
    void Update()
    {
        update_inputDynamic_record();
    }
}

  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Unity播放本地视频,你可以使用Unity自带的VideoPlayer组件。以下是一个简单的示例代码: ```csharp using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; public class VideoPlayerController : MonoBehaviour { public RawImage videoScreen; public VideoClip videoClip; private VideoPlayer videoPlayer; void Start() { videoPlayer = gameObject.AddComponent<VideoPlayer>(); videoPlayer.playOnAwake = false; videoPlayer.clip = videoClip; videoPlayer.renderMode = VideoRenderMode.RenderTexture; videoPlayer.targetTexture = new RenderTexture(1920, 1080, 0); videoScreen.texture = videoPlayer.targetTexture; } public void PlayVideo() { videoPlayer.Play(); } public void StopVideo() { videoPlayer.Stop(); } } ``` 在这个示例代码,我们首先在Start方法创建了一个VideoPlayer组件,并将其配置为不自动播放。然后,我们将要播放的视频剪辑赋给VideoPlayer的clip属性,并将其渲染模式设置为RenderTexture,这样就可以将视频渲染到一个纹理。我们还创建了一个新的RenderTexture并将其分配给VideoPlayer的targetTexture属性,这样就可以将视频渲染到RawImage组件上。最后,我们在PlayVideo方法调用VideoPlayer的Play方法来播放视频,在StopVideo方法调用VideoPlayer的Stop方法来停止视频播放。 你可以通过在VideoPlayerController脚本上添加一个UI按钮来控制视频的播放和停止。当用户点击播放按钮时,调用PlayVideo方法来播放视频;当用户点击停止按钮时,调用StopVideo方法来停止视频播放。另外,你需要将要播放的视频剪辑赋给videoClip属性。在Unity编辑器,你可以将视频文件拖放到videoClip属性上来进行赋值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值