Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

73 行
2.8 KiB

using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System.Collections.Generic;
namespace NaughtyAttributes.Editor
{
[PropertyDrawer(typeof(ReorderableListAttribute))]
public class ReorderableListPropertyDrawer : PropertyDrawer
{
private Dictionary<string, ReorderableList> reorderableListsByPropertyName = new Dictionary<string, ReorderableList>();
string GetPropertyKeyName(SerializedProperty property)
{
return property.serializedObject.targetObject.GetInstanceID() + "/" + property.name;
}
public override void DrawProperty(SerializedProperty property)
{
EditorDrawUtility.DrawHeader(property);
if (property.isArray)
{
var key = GetPropertyKeyName(property);
if (!this.reorderableListsByPropertyName.ContainsKey(key))
{
ReorderableList reorderableList = new ReorderableList(property.serializedObject, property, true, true, true, true)
{
drawHeaderCallback = (Rect rect) =>
{
EditorGUI.LabelField(rect, string.Format("{0}: {1}", property.displayName, property.arraySize), EditorStyles.label);
},
drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
var element = property.GetArrayElementAtIndex(index);
if(element.hasChildren)
{
rect.y += 1.0f;
rect.x += 10.0f;
rect.width -= 10.0f;
}
EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element, true);
},
elementHeightCallback = (int index) =>
{
return EditorGUI.GetPropertyHeight(property.GetArrayElementAtIndex(index)) + 4.0f;
}
};
this.reorderableListsByPropertyName[key] = reorderableList;
}
this.reorderableListsByPropertyName[key].DoLayoutList();
}
else
{
string warning = typeof(ReorderableListAttribute).Name + " can be used only on arrays or lists";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawPropertyField(property);
}
}
public override void ClearCache()
{
this.reorderableListsByPropertyName.Clear();
}
}
}