DragonECS-Unity/src/Internal/ReferenceButtonAttributeDrawer.cs

117 lines
4.0 KiB
C#
Raw Normal View History

2024-10-06 09:09:36 +08:00
using System;
2024-06-30 01:12:21 +08:00
2024-10-02 14:01:18 +08:00
namespace DCFApixels.DragonECS.Unity.Editors
{
2024-10-06 09:09:36 +08:00
internal interface IReferenceButtonAttribute
2024-10-02 14:01:18 +08:00
{
Type[] PredicateTypes { get; }
bool IsHideButtonIfNotNull { get; }
}
}
2024-06-30 01:12:21 +08:00
#if UNITY_EDITOR
2026-03-02 14:55:10 +08:00
namespace DCFApixels.DragonECS.Unity.Internal
{
using DCFApixels.DragonECS.Unity.Editors;
using UnityEditor;
internal partial class UnityReflectionCache
{
public bool IsReferenceButtonCacheInit_Editor;
public bool InitReferenceButtonCache_Editor(SerializedProperty sp)
{
if (IsReferenceButtonCacheInit_Editor) { return false; }
HasSerializableData_Editor = sp.HasSerializableData();
IsReferenceButtonCacheInit_Editor = true;
return true;
}
public bool HasSerializableData_Editor;
}
}
2024-06-30 01:12:21 +08:00
namespace DCFApixels.DragonECS.Unity.Editors
{
using DCFApixels.DragonECS.Unity.Internal;
using System;
using UnityEditor;
2024-10-06 09:09:36 +08:00
using UnityEngine;
2024-06-30 01:12:21 +08:00
2024-10-02 14:01:18 +08:00
[CustomPropertyDrawer(typeof(ReferenceButtonAttribute), true)]
internal sealed class ReferenceButtonAttributeDrawer : ExtendedPropertyDrawer<IReferenceButtonAttribute>
2024-06-30 01:12:21 +08:00
{
2024-11-01 13:50:48 +08:00
private Type[] _withOutTypes;
2024-09-16 19:31:01 +08:00
protected override void OnInit()
{
Type fieldType = fieldInfo.FieldType;
2024-11-01 13:50:48 +08:00
_withOutTypes = fieldType.TryGetAttribute(out ReferenceButtonWithOutAttribute a) ? a.PredicateTypes : Array.Empty<Type>();
2026-03-02 14:55:10 +08:00
//if (fieldType.IsGenericType)
//{
// if (fieldType.IsGenericTypeDefinition == false)
// {
// fieldType = fieldType.GetGenericTypeDefinition();
// }
//}
}
private UnityReflectionCache _reflectionCache;
private UnityReflectionCache Cahce(SerializedProperty sp)
{
if (UnityReflectionCache.InitLocal(sp.managedReferenceValue.GetType(), ref _reflectionCache))
2024-09-16 19:31:01 +08:00
{
2026-03-02 14:55:10 +08:00
_reflectionCache.InitReferenceButtonCache_Editor(sp);
2024-09-16 19:31:01 +08:00
}
2026-03-02 14:55:10 +08:00
return _reflectionCache;
2024-09-16 19:31:01 +08:00
}
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();
if (property.propertyType == SerializedPropertyType.ManagedReference &&
property.managedReferenceValue != null &&
2026-03-02 14:55:10 +08:00
Cahce(property).HasSerializableData_Editor)
2024-06-30 01:12:21 +08:00
{
return EditorGUI.GetPropertyHeight(property, label, true);
}
else
{
return OneLineHeight;
2024-06-30 01:12:21 +08:00
}
}
protected override void DrawCustom(Rect position, SerializedProperty property, GUIContent label)
2024-06-30 01:12:21 +08:00
{
if(property.propertyType != SerializedPropertyType.ManagedReference)
{
GUI.Label(position, label);
return;
}
2024-09-16 19:31:01 +08:00
if (IsArrayElement)
{
label = UnityEditorUtility.GetLabelTemp();
}
2024-06-30 01:12:21 +08:00
Rect selButtnoRect = position;
selButtnoRect.height = OneLineHeight;
2024-11-01 17:17:52 +08:00
DrawSelectionPopupButton(selButtnoRect, property);
2024-06-30 01:12:21 +08:00
2026-03-02 14:55:10 +08:00
if (property.managedReferenceValue != null &&
Cahce(property).HasSerializableData_Editor)
2024-06-30 01:12:21 +08:00
{
EditorGUI.PropertyField(position, property, label, true);
}
else
{
EditorGUI.BeginProperty(position, label, property);
EditorGUI.LabelField(position, label);
EditorGUI.EndProperty();
}
}
2024-11-01 17:17:52 +08:00
private void DrawSelectionPopupButton(Rect position, SerializedProperty property)
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-11-01 13:50:48 +08:00
EcsGUI.DrawSelectReferenceButton(buttonRect, property, Attribute.PredicateTypes.Length == 0 ? new Type[1] { fieldInfo.FieldType } : Attribute.PredicateTypes, _withOutTypes, Attribute.IsHideButtonIfNotNull);
2024-06-30 01:12:21 +08:00
}
}
}
#endif