Skip to content

Commit d215642

Browse files
committed
Fix caching FieldInfo for nested [SerializeReference] collections; fix label rect calculations in the PropertyScope
1 parent f920bba commit d215642

4 files changed

Lines changed: 30 additions & 9 deletions

File tree

Assets/Editor Toolbox/Editor/Internal/PropertyScope.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@ private void TryDrawLabel(Rect rect, GUIContent label)
3636
InputRect = rect;
3737
var size = EditorStyles.label.CalcSize(label);
3838
rect.xMax = rect.xMin + size.x;
39+
rect.xMax += EditorGuiUtility.IndentSize;
40+
rect.xMax += EditorGuiUtility.SpacingSize;
3941
LabelRect = rect;
4042
if (property.hasVisibleChildren)
4143
{
4244
var labelRect = LabelRect;
43-
labelRect.xMax += EditorGuiUtility.FoldoutOffset;
45+
labelRect.xMax += EditorGuiUtility.FoldoutSize;
4446
LabelRect = labelRect;
4547
property.isExpanded = EditorGUI.Foldout(labelRect, property.isExpanded, label, true);
4648
}
4749
else
4850
{
49-
EditorGUI.LabelField(rect, label);
51+
EditorGUI.LabelField(LabelRect, label);
5052
}
5153
}
5254

Assets/Editor Toolbox/Editor/ToolboxPropertyHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,11 @@ private void DrawProperty(SerializedProperty property, GUIContent label)
286286
//draw target property using the associated type drawer
287287
ToolboxDrawerModule.GetTargetTypeDrawer(type)?.OnGui(property, label);
288288
}
289+
290+
return;
289291
}
290-
else
291-
{
292-
OnGuiDefault(property, label);
293-
}
292+
293+
OnGuiDefault(property, label);
294294
}
295295

296296
private void BeginDecoratorDrawers(PropertyCondition conditionState = PropertyCondition.Valid)

Assets/Editor Toolbox/Editor/Utilities/EditorGuiUtility.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Toolbox.Editor
1212
/// </summary>
1313
internal static class EditorGuiUtility
1414
{
15+
private const float indentPerLevel = 15.0f;
16+
1517
private static readonly Dictionary<string, Texture2D> loadedTextures = new Dictionary<string, Texture2D>();
1618

1719

@@ -99,6 +101,9 @@ public static Texture GetHelpIcon(MessageType messageType)
99101
}
100102

101103

102-
public static float FoldoutOffset { get; internal set; } = 28.0f;
104+
public static float FoldoutSize { get; internal set; } = 15.0f;
105+
public static float SpacingSize => EditorGUIUtility.standardVerticalSpacing;
106+
public static float HeightSize => EditorGUIUtility.singleLineHeight;
107+
public static float IndentSize => EditorGUI.indentLevel * indentPerLevel;
103108
}
104109
}

Assets/Editor Toolbox/Editor/Utilities/PropertyUtility.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ namespace Toolbox.Editor
1010
{
1111
public static partial class PropertyUtility
1212
{
13+
//TODO: temp
14+
private static readonly MethodInfo getGetFieldInfoFromPropertyMethod =
15+
ReflectionUtility.GetEditorMethod("UnityEditor.ScriptAttributeUtility", "GetFieldInfoFromProperty",
16+
BindingFlags.NonPublic | BindingFlags.Static);
17+
1318
/// <summary>
1419
/// Indicates if the property has all changes applied and can be safely used for reflection-based features.
1520
/// </summary>
@@ -257,7 +262,13 @@ public static FieldInfo GetFieldInfo(this SerializedProperty property)
257262

258263
public static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType)
259264
{
260-
return GetFieldInfoFromProperty(property, out propertyType);
265+
var parameters = new object[] { property, null };
266+
var result = getGetFieldInfoFromPropertyMethod.Invoke(null, parameters) as FieldInfo;
267+
propertyType = parameters[1] as Type;
268+
return result;
269+
270+
//NOTE: ...
271+
//return GetFieldInfoFromProperty(property, out propertyType);
261272
}
262273

263274
public static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType, Object target)
@@ -277,8 +288,12 @@ public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, ou
277288
return GetFieldInfoFromProperty(property, out type, classType);
278289
}
279290

291+
//NOTE: ...
292+
//TODO: make it internal
280293
public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type, Type host)
281294
{
295+
const BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
296+
282297
FieldInfo field = null;
283298
type = host;
284299

@@ -297,7 +312,6 @@ public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, ou
297312
continue;
298313
}
299314

300-
const BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
301315
FieldInfo foundField = null;
302316
for (var currentType = type; foundField == null && currentType != null; currentType = currentType.BaseType)
303317
{

0 commit comments

Comments
 (0)