Version: 2017.3
public static Vector3 Slider2D (int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.CapFunction capFunction, Vector2 snap, bool drawHelper= false);
public static Vector3 Slider2D (Vector3 handlePos, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.CapFunction capFunction, float snap, bool drawHelper= false);
public static Vector3 Slider2D (int id, Vector3 handlePos, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.CapFunction capFunction, Vector2 snap, bool drawHelper= false);
public static Vector3 Slider2D (Vector3 handlePos, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.CapFunction capFunction, Vector2 snap, bool drawHelper= false);

参数

id (可选)覆盖此 Slider2D 实例的默认 ControlID。
handlePos Handles.matrix 空间中当前点的位置。
offset (可选)在 handlePos 渲染 Slider2D,但将 Slider2D 的原点视为 handlePos + 偏移。这对于相对于另一个对象或手柄放置/渲染的 Slider2D 实例非常有用。
handleDir Handles.matrix 空间中手柄的方向,仅用于手柄渲染。
slideDir1 Handles.matrix 空间中滑动条的移动平面的第一个轴。
slideDir2 Handles.matrix 空间中滑动条的移动平面的第二个轴。
handleSize Handles.matrix 空间中手柄的大小。如果您想要一个恒定的屏幕空间大小,请使用 HandleUtility.GetHandleSize
snap (浮点或 Vector2)沿着两个轴的贴靠增量,可以是一致的,也可以根据每个轴设置。请参阅 Handles.SnapValue
drawHelper (默认值:false)拖动时,围绕手柄渲染一个矩形。
capFunction 要在执行实际绘制时调用的函数。

返回

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

描述

创建一个沿两个轴定义的平面移动的 3D 滑动条。

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


2D slider handle in the Scene View.

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

using UnityEngine;

[ExecuteInEditMode] public class Slider2DExample : 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); } }

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

using UnityEditor;
using UnityEngine;

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

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

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