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。
为 SerializedProperty 生成一个字段。
如果要在 Inspector 中自定义游戏对象的选项外观,可使用此方法。使用此方法为序列化属性创建字段。有关更改编辑器的更多信息,请参阅 Editor 部分。
另请参阅: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(); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.