版本: Unity 6 (6000.0)
语言英语
  • C#

EditorGUI.PropertyField

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们确实阅读了用户提供的每个建议的更改,并在适用情况下进行更新。

关闭

提交失败

由于某种原因,您的建议更改无法提交。请 <a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static bool PropertyField(Rect position, SerializedProperty property, bool includeChildren = false);

声明

public static bool PropertyField(Rect position, SerializedProperty property, GUIContent label, bool includeChildren = false);

参数

position 屏幕上用于属性字段的矩形。
property 要创建字段的 SerializedProperty。
label 可选的标签,用于使用。如果没有指定,则使用属性本身的标签。使用 GUIContent.none 来完全不显示标签。
includeChildren 如果 true,则绘制包含子元素的属性;否则仅绘制控件本身(例如,仅绘制折叠面板,但没有下面内容)。

返回值

bool 如果属性有子元素并且已展开,并且 includeChildren 设置为 false,则返回 true;否则返回 false。

描述

使用此方法在编辑器中为 SerializedProperty 创建一个字段。

其他资源:SerializedPropertySerializedObject

//Attach a script like this to the GameObject you would like to have a custom Editor window.

using UnityEngine;

public class MyScript : MonoBehaviour { public int myInt = 90; }
//Create a folder and name it “Editor” and place this second script within it. To do this right click within the Assets directory and go to Create>Folder
//Ensure you insert your first script’s name as a parameter in the CustomEditor e.g. [CustomEditor(typeof(MyScript))]

using UnityEngine; using UnityEditor;

// Custom Editor using SerializedProperties. // Make sure to put the name of the script on your GameObject in here [CustomEditor(typeof(MyScript))] // Automatic handling of multi-object editing, undo, and Prefab overrides. [CanEditMultipleObjects]

public class EditorGUIPropertyField : Editor { SerializedProperty m_IntProperty;

void OnEnable() { // Fetch the objects from the MyScript script to display in the inspector m_IntProperty = serializedObject.FindProperty("myInt"); }

public override void OnInspectorGUI() { //The variables and GameObject from the GameObject script are displayed in the Inspector and have the appropriate label EditorGUI.PropertyField(new Rect(0, 300, 500, 30), m_IntProperty, new GUIContent("Int : "));

// Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI. serializedObject.ApplyModifiedProperties(); } }