Version: 2021.3
public static bool PropertyField (SerializedProperty property, params GUILayoutOption[] options);
public static bool PropertyField (SerializedProperty property, GUIContent label, params GUILayoutOption[] options);
public static bool PropertyField (SerializedProperty property, bool includeChildren, params GUILayoutOption[] options);
public static bool PropertyField (SerializedProperty property, GUIContent label, bool includeChildren, params GUILayoutOption[] options);

参数

property 要为其创建字段的 SerializedProperty。
label 要使用的可选标签。如果未指定,则使用属性本身的标签。使用 GUIContent.none 可完全不显示标签。
includeChildren 如果为 /true/,将绘制包含子项的属性;否则仅绘制控件本身(例如,只有一个折叠箭头,其下不含任何内容)。
options 一个可选的布局选项列表,用于指定额外的布局属性。此处传递的任何值都将覆盖 style 定义的设置。
另请参阅:GUILayout.WidthGUILayout.HeightGUILayout.MinWidthGUILayout.MaxWidthGUILayout.MinHeightGUILayout.MaxHeightGUILayout.ExpandWidthGUILayout.ExpandHeight

返回

bool 如果属性拥有子项且已展开,并且 includeChildren 设置为 false,则为 True;否则为 false。

描述

SerializedProperty 生成一个字段。

如果要在 Inspector 中自定义游戏对象的选项外观,可使用此方法。使用此方法为序列化属性创建字段。有关更改编辑器的更多信息,请参阅 Editor 部分。

另请参阅:SerializedPropertySerializedObject

//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(); } }