使用手柄操作滑动条(Unity / Slider / Htc Vive)

创建Slider

第一步:创建一个Canvas,右键选择UI,在选择Canvas
在这里插入图片描述

第二步:在Canvas下创建Slider,点击Canvas,右键选择UI,然后选择Slider
在这里插入图片描述

创建交互组件Sphere

点击Slider,右键选择3D Object,然后选择Sphere
在这里插入图片描述
然后移动Sphere的位置,将其移动到Slider子物体的第一行,成为Slider的第一个子物体
在这里插入图片描述

挂载交互脚本

在Slider上需要挂载 SliderControl 脚本,Sphere的移动位置根据自己的需求更改,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace UnityVolumeRendering
{
   
    public class SliderControl : MonoBehaviour
    {
   
        private Slider slider;
        private Transform sphere;
        private Removable removable;

        //public void SetValue(float value)
        //{
   
        //    slider = GetComponent<Slider>();
        //    slider.value = value;
        //    sphere = transform.GetChild(0);
        //    sphere.localPosition = new Vector3(value * 140 - 70, 0, 0);
        //}
        // Use this for initialization
        void Start()
        {
   
            slider = GetComponent<Slider>();
            Debug.Log(slider.value);
            sphere = transform.GetChild(0);
            Debug.Log(sphere.name);
            removable = sphere.GetComponent<Removable>();
            //sphere.localPosition = Vector3.zero;

            //slider.value = (float)0.5;
        }

        // Update is called once per frame
        void Update()
        {
   
            if (sphere.localPosition.x >= 70)
            {
   
                slider.value = (float)1;
            }
            else if (sphere.localPosition.x <= -70)
            {
   
                slider.value = 0;
            }
            else
            {
   
                slider.value = (sphere.localPosition.x + 70) / 140;
            }

            if (!removable.moving)
            {
   

                if (sphere.localPosition.x >= 70)
                {
   
                    sphere.localPosition = new Vector3(70, 0, 0);
                }
                else if (sphere.localPosition.x <= -150)
                {
   
                    sphere.localPosition = new Vector3(-70, 0, 0);
                }
                else
                {
   
                    sphere.localPosition = new Vector3(sphere.localPosition.x, 0, 0);
                }
            }
        }
    }
}

在Sphere上挂载 Interactable 脚本和 Removable脚本,代码如下:
Interactable

//======= Copyright (c) Valve Corporation, All rights reserved. ===============
//
// Purpose: This object will get hover events and can be attached to the hands
//
//=============================================================================

using UnityEngine;
using UnityEngine.Events;
using System.Collections;
using System.Collections.Generic;

namespace Valve.VR.InteractionSystem
{
   
    //-------------------------------------------------------------------------
    public class Interactable : MonoBehaviour
    {
   
        [Tooltip("Activates an action set on attach and deactivates on detach")]
        public SteamVR_ActionSet activateActionSetOnAttach;

        [Tooltip("Hide the whole hand on attachment and show on detach")]
        public bool hideHandOnAttach = true;

        [Tooltip("Hide the skeleton part of the hand on attachment and show on detach")]
        public bool hideSkeletonOnAttach = false;

        [Tooltip("Hide the controller part of the hand on attachment and show on detach")]
        public bool hideControllerOnAttach = false;

        [Tooltip("The integer in the animator to trigger on pickup. 0 for none")]
        public int handAnimationOnPickup = 0;

        [Tooltip("The range of motion to set on the skeleton. None for no change.")
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值