Version: 2020.2
public static Vector3 Vector3Field (string label, Vector3 value, params GUILayoutOption[] options);
public static Vector3 Vector3Field (GUIContent label, Vector3 value, params GUILayoutOption[] options);

参数

label 显示在字段上方的标签。
value 要编辑的值。
options 一个可选的布局选项列表,用于指定额外的 布局属性。此处传递的任何值都将覆盖 style 定义的设置。
另请参阅:GUILayout.WidthGUILayout.HeightGUILayout.MinWidthGUILayout.MaxWidthGUILayout.MinHeightGUILayout.MaxHeightGUILayout.ExpandWidthGUILayout.ExpandHeight

返回

Vector3 用户输入的值。

描述

创建用于输入 Vector3 的 X、Y 和 Z 字段。


测量 2 个游戏对象或 3D 空间中 2 个位置之间的距离。

using UnityEditor;
using UnityEngine;

public class EditorGUILayoutVector3Field : UnityEditor.EditorWindow { float distance = 0f; Vector3 obj1; Vector3 obj2;

[MenuItem("Examples/Measure Distance between 2 objects")] static void Init() { EditorGUILayoutVector3Field window = (EditorGUILayoutVector3Field)EditorWindow.GetWindow(typeof(EditorGUILayoutVector3Field), true, "My Empty Window"); window.Show(); }

void OnGUI() { GUILayout.Label("Select an object in the Hierarchy view and click 'Capture Position'"); EditorGUILayout.BeginHorizontal(); obj1 = EditorGUILayout.Vector3Field("GameObject 1:", obj1); if (GUILayout.Button("Capture Position")) obj1 = Selection.activeTransform.position; EditorGUILayout.EndHorizontal();

EditorGUILayout.BeginHorizontal(); obj2 = EditorGUILayout.Vector3Field("GameObject 2:", obj2); if (GUILayout.Button("Capture Position")) obj2 = Selection.activeTransform.position; EditorGUILayout.EndHorizontal(); EditorGUILayout.LabelField("Distance:", distance.ToString()); if (GUILayout.Button("Close")) this.Close(); }

void OnInspectorUpdate() { distance = Vector3.Distance(obj1, obj2); this.Repaint(); } }