public static Vector3 Slider (Vector3 position, Vector3 direction);
public static Vector3 Slider (Vector3 position, Vector3 direction, float size, Handles.CapFunction capFunction, float snap);

Parameters

position Handles.matrix 空间中当前点的位置。
direction Handles.matrix 空间中滑动条的方向轴。
size Handles.matrix 空间中手柄的大小。如果您想要一个恒定的屏幕空间大小,请使用 HandleUtility.GetHandleSize
snap贴靠增量。请参阅 Handles.SnapValue
capFunction要在执行实际绘制时调用的函数。默认情况下,该函数是 Handles.ArrowHandleCap,但您可以使用具有相同签名的任何函数。

Returns

Vector3 通过用户与手柄的交互修改的新值。如果用户没有移动手柄,则将返回传递给相应函数的位置值。

Description

创建一个沿着一个轴移动的 3D 滑动条。

This method will draw a 3D-draggable handle on the screen. The handle is constrained to sliding along a direction vector in 3D space.


Slider handle in the Scene View.

Add the following script to your Assets folder as SliderExample.cs and add the SliderExample component to an object in a Scene.

using UnityEngine;

[ExecuteInEditMode] public class SliderExample : MonoBehaviour { public Vector3 targetPosition { get { return m_TargetPosition; } set { m_TargetPosition = value; } } [SerializeField] private Vector3 m_TargetPosition = new Vector3(1f, 0f, 2f);

public virtual void Update() { transform.LookAt(m_TargetPosition); } }

将以下脚本作为 SliderExampleEditor.cs 添加到 Assets/Editor,然后选择包含 SliderExample 组件的对象。

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(SliderExample)), CanEditMultipleObjects] public class SliderExampleEditor : Editor { protected virtual void OnSceneGUI() { SliderExample example = (SliderExample)target;

float size = HandleUtility.GetHandleSize(example.targetPosition) * 0.5f; float snap = 0.1f;

EditorGUI.BeginChangeCheck(); Vector3 newTargetPosition = Handles.Slider(example.targetPosition, Vector3.right, size, Handles.ConeHandleCap, snap); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(example, "Change Look At Target Position"); example.targetPosition = newTargetPosition; example.Update(); } } }