Version: 2017.2

Handles.FreeMoveHandle

切换到手册
public static Vector3 FreeMoveHandle (Vector3 position, Quaternion rotation, float size, Vector3 snap, Handles.CapFunction capFunction);

参数

position Handles.matrix 空间中手柄的位置。
rotation Handles.matrix 空间中手柄的旋转。
size Handles.matrix 空间中手柄的大小。如果您想要一个恒定的屏幕空间大小,请使用 HandleUtility.GetHandleSize
snap 所有轴上的贴靠增量。请参阅 Handles.SnapValue
capFunction 要在执行实际绘制时调用的函数。

返回

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

描述

创建一个不受约束的移动手柄。

This handle can move freely in all directions. Hold down Ctrl (Cmd on macOS) to snap to the grid (see [wiki:PositioningGameObjects]). Hold Ctrl-Shift (Cmd-Shift on macOS) to snap the object to any Collider surface under the mouse pointer.


Free Move handle in the Scene View.

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

using UnityEngine;

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

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

using UnityEditor;
using UnityEngine;

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

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

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