1、自定义特性
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 自定义特性
/// </summary>
public class CustomLabelAttribute : PropertyAttribute
{
public string content;
public string tooltip;
public CustomLabelAttribute(string content,string tooltip = "")
{
this.content = content;
this.tooltip = tooltip;
}
}
2、定制显示方式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;
[CustomPropertyDrawer(typeof(CustomLabelAttribute))]
public class CustomLabelDrawer : PropertyDrawer
{
private GUIContent _label = null;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
float height = 0;
if (property.isExpanded)
{
SerializedProperty iterator = property.Copy();
int depth = iterator.depth;
bool enterChildren = true;
do
{
height += EditorGUI.GetPropertyHeight(iterator, null, true) + EditorGUIUtility.standardVerticalSpacing;
enterChildren = false;
}
while (iterator.NextVisible(enterChildren) && (depth < iterator.depth));
}
else
{
height = EditorGUI.GetPropertyHeight(property, null, true);
}
return height;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (_label == null)
{
string content = (attribute as CustomLabelAttribute).content;
string tooltip = (attribute as CustomLabelAttribute).tooltip;
_label = new GUIContent(content, tooltip);
}
if (!property.hasVisibleChildren)
{
EditorGUI.PropertyField(position, property, _label);
}
else
{
property.isExpanded = EditorGUI.Foldout(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), property.isExpanded, _label);
if (property.isExpanded)
{
float offsetY = EditorGUIUtility.singleLineHeight;
SerializedProperty iterator = property.Copy();
int depth = iterator.depth;
bool enterChildren = true;
while (iterator.NextVisible(enterChildren) && (depth < iterator.depth))
{
Rect childRect = new Rect(position.x, position.y + offsetY, position.width, EditorGUI.GetPropertyHeight(iterator, null, true));
EditorGUI.PropertyField(childRect, iterator, true);
offsetY += EditorGUI.GetPropertyHeight(iterator, null, true) + EditorGUIUtility.standardVerticalSpacing;
enterChildren = false;
}
}
else
{
EditorGUI.PropertyField(position, property, _label);
}
}
}
}