property | 要为其创建字段的 SerializedProperty。 |
label | 可选标签,用于使用。如果未指定,则使用属性本身的标签。使用 GUIContent.none 完全不显示标签。 |
includeChildren | 如果 true ,则绘制包括子项的属性;否则仅绘制控件本身(例如,仅绘制折叠项,但不绘制其下方的任何内容)。 |
options | 指定额外布局属性的可选布局选项列表。此处传递的任何值都将覆盖由 style 定义的设置。其他资源:GUILayout.Width、GUILayout.Height、GUILayout.MinWidth、GUILayout.MaxWidth、GUILayout.MinHeight、GUILayout.MaxHeight、GUILayout.ExpandWidth、GUILayout.ExpandHeight。 |
bool 如果属性具有子项,已展开,并且 includeChildren
设置为 false,则返回 true;否则返回 false。您可以使用它来确定属性的 isExpanded
状态,并在必要时自定义子项的渲染。
为 SerializedProperty 创建一个字段。
当您想要自定义 Inspector 中 GameObject 选项的外观时使用它。使用它为序列化属性创建字段。有关更改编辑器的更多信息,请参阅 编辑器 部分。
其他资源:SerializedProperty、SerializedObject。
//The scripts below show how to use a propertyField to change your editor. //Attach this first script to the GameObject that you would like to control. Add code in this script for any of the actions you require.
using UnityEngine;
public class MyGameObjectScript : MonoBehaviour { public int m_MyInt = 75; public Vector3 m_MyVector = new Vector3(20, 1, 0); public GameObject m_MyGameObject; }
//This next script shows how to call upon variables from the "MyGameObject" Script (the first script) to make custom fields in the Inspector for these variables.
using UnityEngine; using UnityEditor;
// Custom Editor using SerializedProperties. // Automatic handling of multi-object editing, undo, and Prefab overrides. [CustomEditor(typeof(MyGameObjectScript))] [CanEditMultipleObjects] public class EditorGUILayoutPropertyField : Editor { SerializedProperty m_IntProp; SerializedProperty m_VectorProp; SerializedProperty m_GameObjectProp;
void OnEnable() { // Fetch the objects from the GameObject script to display in the inspector m_IntProp = serializedObject.FindProperty("m_MyInt"); m_VectorProp = serializedObject.FindProperty("m_MyVector"); m_GameObjectProp = serializedObject.FindProperty("m_MyGameObject"); }
public override void OnInspectorGUI() { //The variables and GameObject from the MyGameObject script are displayed in the Inspector with appropriate labels EditorGUILayout.PropertyField(m_IntProp, new GUIContent("Int Field"), GUILayout.Height(20)); EditorGUILayout.PropertyField(m_VectorProp, new GUIContent("Vector Object")); EditorGUILayout.PropertyField(m_GameObjectProp, new GUIContent("Game Object"));
// Apply changes to the serializedProperty - always do this at the end of OnInspectorGUI. serializedObject.ApplyModifiedProperties(); } }