position | 3D 空间中手柄的中心位置。 |
rotation | 3D 空间中手柄的方向。 |
Vector3 通过用户与手柄的交互修改的新值。如果用户没有移动手柄,则将返回您传递给相应函数的值。
创建一个位置手柄。
此手柄的行为类似于 Unity 中的内置移动工具。
Position handle in the Scene View.''
Add the following script to your Assets folder as PositionHandleExample.cs and add the PositionHandleExample component to an object in a Scene.
using UnityEngine;
[ExecuteInEditMode] public class PositionHandleExample : 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); } }
将以下脚本作为 PositionHandleExampleEditor.cs 添加到 Assets/Editor,然后选择包含 PositionHandleExample 组件的对象。
using UnityEditor; using UnityEngine;
[CustomEditor(typeof(PositionHandleExample)), CanEditMultipleObjects] public class PositionHandleExampleEditor : Editor { protected virtual void OnSceneGUI() { PositionHandleExample example = (PositionHandleExample)target;
EditorGUI.BeginChangeCheck(); Vector3 newTargetPosition = Handles.PositionHandle(example.targetPosition, Quaternion.identity); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(example, "Change Look At Target Position"); example.targetPosition = newTargetPosition; example.Update(); } } }