2024-09-14 23:30:11 +08:00
|
|
|
|
using System;
|
2024-06-30 01:12:21 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS.Unity.Internal
|
|
|
|
|
{
|
2024-09-11 10:37:25 +08:00
|
|
|
|
internal sealed class ReferenceButtonAttribute : PropertyAttribute
|
|
|
|
|
{
|
2024-09-14 23:30:11 +08:00
|
|
|
|
public readonly Type[] PredicateTypes;
|
|
|
|
|
public readonly bool IsHideButtonIfNotNull;
|
|
|
|
|
public ReferenceButtonAttribute(bool isHideButtonIfNotNull = false) : this(isHideButtonIfNotNull, Array.Empty<Type>()) { }
|
|
|
|
|
public ReferenceButtonAttribute(params Type[] predicateTypes) : this(false, predicateTypes) { }
|
|
|
|
|
public ReferenceButtonAttribute(bool isHideButtonIfNotNull, params Type[] predicateTypes)
|
2024-09-11 10:37:25 +08:00
|
|
|
|
{
|
2024-09-14 23:30:11 +08:00
|
|
|
|
IsHideButtonIfNotNull = isHideButtonIfNotNull;
|
|
|
|
|
PredicateTypes = predicateTypes;
|
2024-09-11 10:37:25 +08:00
|
|
|
|
Array.Sort(predicateTypes, (a, b) => string.Compare(a.AssemblyQualifiedName, b.AssemblyQualifiedName, StringComparison.Ordinal));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-30 01:12:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
namespace DCFApixels.DragonECS.Unity.Editors
|
|
|
|
|
{
|
|
|
|
|
using DCFApixels.DragonECS.Unity.Internal;
|
|
|
|
|
using System;
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
|
|
|
|
|
[CustomPropertyDrawer(typeof(ReferenceButtonAttribute))]
|
2024-09-14 23:30:11 +08:00
|
|
|
|
internal sealed class ReferenceButtonAttributeDrawer : ExtendedPropertyDrawer<ReferenceButtonAttribute>
|
2024-06-30 01:12:21 +08:00
|
|
|
|
{
|
2024-09-16 19:31:01 +08:00
|
|
|
|
protected override void OnInit()
|
|
|
|
|
{
|
|
|
|
|
Type fieldType = fieldInfo.FieldType;
|
|
|
|
|
if (fieldType.IsGenericType)
|
|
|
|
|
{
|
|
|
|
|
if (fieldType.IsGenericTypeDefinition == false)
|
|
|
|
|
{
|
|
|
|
|
fieldType = fieldType.GetGenericTypeDefinition();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-30 01:12:21 +08:00
|
|
|
|
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
|
|
|
|
{
|
2024-09-18 20:05:54 +08:00
|
|
|
|
Init();
|
2024-06-30 01:12:21 +08:00
|
|
|
|
if (property.managedReferenceValue != null)
|
|
|
|
|
{
|
|
|
|
|
return EditorGUI.GetPropertyHeight(property, label, true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-09-14 23:30:11 +08:00
|
|
|
|
return OneLineHeight;
|
2024-06-30 01:12:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-14 23:30:11 +08:00
|
|
|
|
|
|
|
|
|
protected override void DrawCustom(Rect position, SerializedProperty property, GUIContent label)
|
2024-06-30 01:12:21 +08:00
|
|
|
|
{
|
2024-09-16 19:31:01 +08:00
|
|
|
|
if (IsArrayElement)
|
|
|
|
|
{
|
|
|
|
|
label = UnityEditorUtility.GetLabelTemp();
|
|
|
|
|
}
|
2024-06-30 01:12:21 +08:00
|
|
|
|
Rect selButtnoRect = position;
|
2024-09-14 23:30:11 +08:00
|
|
|
|
selButtnoRect.height = OneLineHeight;
|
2024-09-16 19:31:01 +08:00
|
|
|
|
DrawSelectionPopupButton(selButtnoRect, property, label);
|
2024-06-30 01:12:21 +08:00
|
|
|
|
|
|
|
|
|
if (property.managedReferenceValue != null)
|
|
|
|
|
{
|
|
|
|
|
EditorGUI.PropertyField(position, property, label, true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
EditorGUI.BeginProperty(position, label, property);
|
|
|
|
|
EditorGUI.LabelField(position, label);
|
|
|
|
|
EditorGUI.EndProperty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 19:31:01 +08:00
|
|
|
|
private void DrawSelectionPopupButton(Rect position, SerializedProperty property, GUIContent label)
|
2024-06-30 01:12:21 +08:00
|
|
|
|
{
|
2024-09-16 19:31:01 +08:00
|
|
|
|
Rect buttonRect = IsArrayElement ? position : position.AddPadding(EditorGUIUtility.labelWidth, 0f, 0f, 0f); ;
|
2024-09-14 23:30:11 +08:00
|
|
|
|
EcsGUI.DrawSelectReferenceButton(buttonRect, property, Attribute.PredicateTypes.Length == 0 ? new Type[1] { fieldInfo.FieldType } : Attribute.PredicateTypes, Attribute.IsHideButtonIfNotNull);
|
2024-06-30 01:12:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-14 23:30:11 +08:00
|
|
|
|
#endif
|