Skip to content

Commit 31456ff

Browse files
committed
fix: property drawer using a fake object
1 parent aa42c48 commit 31456ff

5 files changed

Lines changed: 171 additions & 131 deletions

Scripts/Editor/Core/CollectionItemIndirectReferenceDrawer.cs

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Linq;
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
namespace BrunoMikoski.ScriptableObjectCollections
7+
{
8+
[CustomPropertyDrawer(typeof(CollectionItemIndirectReference), true)]
9+
public sealed class CollectionItemIndirectReferencePropertyDrawer : PropertyDrawer
10+
{
11+
private const string COLLECTION_ITEM_GUID_PROPERTY_PATH = "collectionItemGUID";
12+
private const string COLLECTION_GUID_PROPERTY_PATh = "collectionGUID";
13+
14+
private SerializedProperty collectionItemGUIDSerializedProperty;
15+
private SerializedProperty collectionGUIDSerializedProperty;
16+
private CollectionItemItemPropertyDrawer collectionItemPropertyDrawer;
17+
18+
private ScriptableObjectCollectionItem collectionItem;
19+
private Type collectionItemType;
20+
21+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
22+
{
23+
if (collectionItemType == null)
24+
{
25+
Type arrayOrListType = fieldInfo.FieldType.GetArrayOrListType();
26+
Type properFieldType = arrayOrListType != null ? arrayOrListType : fieldInfo.FieldType;
27+
collectionItemType = GetGenericItemType(properFieldType).GetGenericArguments().First();
28+
}
29+
30+
if (collectionItemPropertyDrawer == null)
31+
{
32+
collectionItemPropertyDrawer = new CollectionItemItemPropertyDrawer();
33+
collectionItemPropertyDrawer.Initialize(collectionItemType, property.serializedObject.targetObject);
34+
}
35+
36+
37+
collectionItemGUIDSerializedProperty = property.FindPropertyRelative(COLLECTION_ITEM_GUID_PROPERTY_PATH);
38+
collectionGUIDSerializedProperty = property.FindPropertyRelative(COLLECTION_GUID_PROPERTY_PATh);
39+
40+
if (collectionItem != null)
41+
{
42+
if (string.IsNullOrEmpty(collectionItemGUIDSerializedProperty.stringValue)
43+
|| string.IsNullOrEmpty(collectionGUIDSerializedProperty.stringValue))
44+
{
45+
collectionItemGUIDSerializedProperty.stringValue = collectionItem.GUID;
46+
collectionGUIDSerializedProperty.stringValue = collectionItem.Collection.GUID;
47+
property.serializedObject.ApplyModifiedProperties();
48+
}
49+
}
50+
else
51+
{
52+
if (!string.IsNullOrEmpty(collectionItemGUIDSerializedProperty.stringValue)
53+
&& !string.IsNullOrEmpty(collectionGUIDSerializedProperty.stringValue))
54+
{
55+
if (CollectionsRegistry.Instance.TryGetCollectionByGUID(collectionGUIDSerializedProperty.stringValue,
56+
out ScriptableObjectCollection collection))
57+
{
58+
if (collection.TryGetItemByGUID(collectionItemGUIDSerializedProperty.stringValue,
59+
out ScriptableObjectCollectionItem resultCollection))
60+
{
61+
collectionItem = resultCollection;
62+
}
63+
}
64+
}
65+
}
66+
67+
collectionItemPropertyDrawer.DrawCollectionItemDrawer(
68+
position, collectionItem, label,
69+
item =>
70+
{
71+
string collectionItemGUID = string.Empty;
72+
string collectionGUID = string.Empty;
73+
if (item != null)
74+
{
75+
collectionItemGUID = item.GUID;
76+
collectionGUID = item.Collection.GUID;
77+
}
78+
79+
collectionItemGUIDSerializedProperty.stringValue = collectionItemGUID;
80+
collectionGUIDSerializedProperty.stringValue = collectionGUID;
81+
collectionItem = item;
82+
property.serializedObject.ApplyModifiedProperties();
83+
}
84+
);
85+
}
86+
87+
private Type GetGenericItemType(Type targetType)
88+
{
89+
Type baseType = targetType.BaseType;
90+
91+
while (baseType != null)
92+
{
93+
if (baseType.IsGenericType && baseType.GetGenericTypeDefinition() == typeof(CollectionItemIndirectReference<>))
94+
return baseType;
95+
baseType = baseType.BaseType;
96+
}
97+
return null;
98+
}
99+
}
100+
}
101+

Scripts/Editor/Core/CollectionItemIndirectReferenceDrawer.cs.meta renamed to Scripts/Editor/Core/CollectionItemIndirectReferencePropertyDrawer.cs.meta

File renamed without changes.

Scripts/Editor/Core/CollectionItemItemObjectPropertyDrawer.cs renamed to Scripts/Editor/Core/CollectionItemItemPropertyDrawer.cs

Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace BrunoMikoski.ScriptableObjectCollections
88
{
99
[CustomPropertyDrawer(typeof(ScriptableObjectCollectionItem), true)]
10-
public class CollectionItemItemObjectPropertyDrawer : PropertyDrawer
10+
public class CollectionItemItemPropertyDrawer : PropertyDrawer
1111
{
1212
private static readonly CollectionItemEditorOptionsAttribute DefaultAttribute
1313
= new CollectionItemEditorOptionsAttribute(DrawType.Dropdown);
@@ -41,12 +41,11 @@ object[] attributes
4141
return attributes[0] as CollectionItemEditorOptionsAttribute;
4242
return DefaultAttribute;
4343
}
44-
44+
4545
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
4646
{
4747
Initialize(property);
4848

49-
5049
if (OptionsAttribute.DrawType == DrawType.AsReference)
5150
{
5251
EditorGUI.PropertyField(position, property, label, true);
@@ -55,57 +54,68 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
5554

5655
item = property.objectReferenceValue as ScriptableObjectCollectionItem;
5756

58-
Rect popupRect = position;
59-
popupRect.height = 15;
57+
DrawCollectionItemDrawer(position, item, label,
58+
newItem =>
59+
{
60+
property.objectReferenceValue = newItem;
61+
property.serializedObject.ApplyModifiedProperties();
62+
});
63+
}
6064

61-
using (new EditorGUI.PropertyScope(position, label, property))
65+
internal void DrawCollectionItemDrawer(Rect position, ScriptableObjectCollectionItem collectionItem, GUIContent label,
66+
Action<ScriptableObjectCollectionItem> callback)
67+
{
68+
position.height = 15;
69+
position = EditorGUI.PrefixLabel(position, label);
70+
int indent = EditorGUI.indentLevel;
71+
EditorGUI.indentLevel = 0;
72+
if (collectionItem != null)
6273
{
63-
popupRect = EditorGUI.PrefixLabel(popupRect, label);
74+
DrawEditFoldoutButton(ref position);
75+
DrawGotoButton(ref position);
76+
}
6477

65-
int indent = EditorGUI.indentLevel;
66-
EditorGUI.indentLevel = 0;
78+
DrawCollectionItemDropDown(ref position, collectionItem, callback);
79+
DrawEditorPreview(collectionItem);
80+
EditorGUI.indentLevel = indent;
81+
}
6782

68-
if (item != null)
69-
{
70-
DrawEditFoldoutButton(ref popupRect);
71-
DrawGotoButton(ref popupRect);
72-
}
73-
74-
DrawCollectionItemDropDown(ref popupRect, property);
83+
private void DrawEditorPreview(ScriptableObjectCollectionItem scriptableObjectCollectionItem)
84+
{
85+
if (scriptableObjectCollectionItem == null)
86+
return;
87+
88+
if (!CollectionUtility.IsFoldoutOpen(scriptableObjectCollectionItem, currentObject))
89+
return;
7590

76-
if (item != null)
91+
EditorGUI.indentLevel++;
92+
using (new EditorGUILayout.VerticalScope("Box"))
93+
{
94+
Editor editor = EditorsCache.GetOrCreateEditorForItem(scriptableObjectCollectionItem);
95+
using (EditorGUI.ChangeCheckScope changedCheck = new EditorGUI.ChangeCheckScope())
7796
{
78-
if (CollectionUtility.IsFoldoutOpen(item, currentObject))
97+
GUILayout.Space(10);
98+
using (new EditorGUILayout.VerticalScope())
7999
{
80-
EditorGUI.indentLevel++;
81-
using (new EditorGUILayout.VerticalScope("Box"))
82-
{
83-
Editor editor = EditorsCache.GetOrCreateEditorForItem(item);
84-
using (EditorGUI.ChangeCheckScope changedCheck = new EditorGUI.ChangeCheckScope())
85-
{
86-
GUILayout.Space(10);
87-
using (new EditorGUILayout.VerticalScope())
88-
{
89-
editor.OnInspectorGUI();
90-
}
91-
92-
EditorGUILayout.Space();
93-
94-
if (changedCheck.changed)
95-
property.serializedObject.ApplyModifiedProperties();
96-
}
97-
}
98-
EditorGUI.indentLevel--;
100+
editor.OnInspectorGUI();
101+
}
102+
103+
EditorGUILayout.Space();
104+
if (changedCheck.changed)
105+
{
106+
EditorUtility.SetDirty(scriptableObjectCollectionItem);
99107
}
100108
}
101-
EditorGUI.indentLevel = indent;
102109
}
110+
111+
EditorGUI.indentLevel--;
103112
}
104113

105114
private void Initialize(SerializedProperty property)
106115
{
107116
if (initialized)
108117
return;
118+
109119
Type arrayOrListType = fieldInfo.FieldType.GetArrayOrListType();
110120
Type itemType = arrayOrListType != null ? arrayOrListType : fieldInfo.FieldType;
111121

@@ -118,20 +128,33 @@ private void Initialize(SerializedProperty property)
118128
initialized = true;
119129
}
120130

121-
private void DrawCollectionItemDropDown(ref Rect position, SerializedProperty property)
131+
internal void Initialize(Type collectionItemType, Object obj)
132+
{
133+
if (initialized)
134+
return;
135+
136+
collectionItemDropdown = new CollectionItemDropdown(
137+
new AdvancedDropdownState(),
138+
collectionItemType
139+
);
140+
141+
currentObject = obj;
142+
initialized = true;
143+
}
144+
145+
private void DrawCollectionItemDropDown(
146+
ref Rect position,
147+
ScriptableObjectCollectionItem collectionItem,
148+
Action<ScriptableObjectCollectionItem> callback)
122149
{
123150
GUIContent displayValue = new GUIContent("None");
124151

125-
if (item != null)
126-
displayValue = new GUIContent(item.name);
152+
if (collectionItem != null)
153+
displayValue = new GUIContent(collectionItem.name);
127154

128155
if (GUI.Button(position, displayValue, EditorStyles.popup))
129156
{
130-
collectionItemDropdown.Show(position, o =>
131-
{
132-
property.objectReferenceValue = o;
133-
property.serializedObject.ApplyModifiedProperties();
134-
});
157+
collectionItemDropdown.Show(position, callback.Invoke);
135158
}
136159
}
137160

@@ -167,5 +190,6 @@ private void DrawEditFoldoutButton(ref Rect popupRect)
167190
ObjectUtility.SetDirty(item);
168191
}
169192
}
193+
170194
}
171195
}

Scripts/Editor/Core/CollectionItemItemObjectPropertyDrawer.cs.meta renamed to Scripts/Editor/Core/CollectionItemItemPropertyDrawer.cs.meta

File renamed without changes.

0 commit comments

Comments
 (0)