创建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.")