Skip to content

Commit 0386bd3

Browse files
committed
Merge branch 'feature/scriptable-objects-creation-tool' into bugfix/fix-serialized-scene-serialization
2 parents 456c12f + 57258c3 commit 0386bd3

9 files changed

Lines changed: 48 additions & 24 deletions

File tree

Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelf/ReferencePickerAttributeDrawer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ private void UpdateContexts(ReferencePickerAttribute attribute)
2424

2525
private Type GetParentType(SerializedProperty property, ReferencePickerAttribute attribute)
2626
{
27-
property.GetFieldInfo(out Type propertyType);
27+
var fieldInfo = property.GetFieldInfo(out _);
28+
var fieldType = property.GetProperType(fieldInfo);
2829
var candidateType = attribute.ParentType;
2930
if (candidateType != null)
3031
{
31-
if (propertyType.IsAssignableFrom(candidateType))
32+
if (fieldType.IsAssignableFrom(candidateType))
3233
{
3334
return candidateType;
3435
}
3536

3637
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
37-
$"Provided {nameof(attribute.ParentType)} ({candidateType}) cannot be used because it's not assignable from: '{propertyType}'");
38+
$"Provided {nameof(attribute.ParentType)} ({candidateType}) cannot be used because it's not assignable from: '{fieldType}'");
3839
}
3940

40-
return propertyType;
41+
return fieldType;
4142
}
4243

4344
private void CreateTypeProperty(Rect position, SerializedProperty property, Type parentType)

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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ namespace Toolbox.Editor
1010
{
1111
public static partial class PropertyUtility
1212
{
13+
//NOTE: last non-reflection implementation was ok but support for [SerializeReference] makes it a bit slow
14+
// unfortunately UnityEditor.ScriptAttributeUtility.GetFieldInfoFromProperty is internal so we have to retrive it using reflection
15+
private static readonly MethodInfo getGetFieldInfoFromPropertyMethod =
16+
ReflectionUtility.GetEditorMethod("UnityEditor.ScriptAttributeUtility", "GetFieldInfoFromProperty",
17+
BindingFlags.NonPublic | BindingFlags.Static);
18+
1319
/// <summary>
1420
/// Indicates if the property has all changes applied and can be safely used for reflection-based features.
1521
/// </summary>
@@ -249,22 +255,20 @@ public static Type GetScriptTypeFromProperty(SerializedProperty property)
249255
return scriptInstance.GetClass();
250256
}
251257

252-
253258
public static FieldInfo GetFieldInfo(this SerializedProperty property)
254259
{
255260
return GetFieldInfo(property, out _);
256261
}
257262

258263
public static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType)
259264
{
260-
return GetFieldInfoFromProperty(property, out propertyType);
261-
}
262-
263-
public static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType, Object target)
264-
{
265-
return GetFieldInfoFromProperty(property, out propertyType, target.GetType());
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;
266269
}
267270

271+
[Obsolete("This method is no longer safe, use GetFieldInfo() instead.")]
268272
public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type)
269273
{
270274
var classType = GetScriptTypeFromProperty(property);
@@ -277,8 +281,11 @@ public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, ou
277281
return GetFieldInfoFromProperty(property, out type, classType);
278282
}
279283

284+
[Obsolete("This method is no longer safe, use GetFieldInfo() instead.")]
280285
public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type, Type host)
281286
{
287+
const BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
288+
282289
FieldInfo field = null;
283290
type = host;
284291

@@ -297,7 +304,6 @@ public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, ou
297304
continue;
298305
}
299306

300-
const BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
301307
FieldInfo foundField = null;
302308
for (var currentType = type; foundField == null && currentType != null; currentType = currentType.BaseType)
303309
{
@@ -383,7 +389,7 @@ public static SerializedProperty GetSize(this SerializedProperty array)
383389

384390
public static T GetAttribute<T>(SerializedProperty property) where T : Attribute
385391
{
386-
return GetAttribute<T>(property, GetFieldInfoFromProperty(property, out _));
392+
return GetAttribute<T>(property, GetFieldInfo(property, out _));
387393
}
388394

389395
public static T GetAttribute<T>(SerializedProperty property, FieldInfo fieldInfo) where T : Attribute
@@ -393,7 +399,7 @@ public static T GetAttribute<T>(SerializedProperty property, FieldInfo fieldInfo
393399

394400
public static T[] GetAttributes<T>(SerializedProperty property) where T : Attribute
395401
{
396-
return GetAttributes<T>(property, GetFieldInfoFromProperty(property, out _));
402+
return GetAttributes<T>(property, GetFieldInfo(property, out _));
397403
}
398404

399405
public static T[] GetAttributes<T>(SerializedProperty property, FieldInfo fieldInfo) where T : Attribute

Assets/Editor Toolbox/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,8 @@ public int var1;
555555

556556
You can draw properties marked with the **[SerializeReference]** attribute with an additional type picker that allows you to manipulate what managed type will be serialized.
557557

558+
To prevent issues after renaming types use `UnityEngine.Scripting.APIUpdating.MovedFromAttribute`.
559+
558560
```csharp
559561
[SerializeReference, ReferencePicker(TypeGrouping = TypeGrouping.ByFlatName)]
560562
public Interface1 var1;

Assets/Editor Toolbox/Tests/Editor/PropertyUtilitesTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void TestGetParentPass(string propertyPath, string parentName)
3636
public void TestGetValuePass(string propertyPath, object value)
3737
{
3838
var property = scriptableObject.FindProperty(propertyPath);
39-
var fieldInfo = property.GetFieldInfo(out _, scriptableObject.targetObject);
39+
var fieldInfo = property.GetFieldInfo(out _);
4040
property.SetProperValue(fieldInfo, value);
4141
var newValue = property.GetProperValue(fieldInfo);
4242
Assert.AreEqual(value, newValue);

Assets/Examples/Scenes/SampleScene.unity

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,15 +583,16 @@ MonoBehaviour:
583583
id: 1
584584
var3:
585585
id: 2
586-
vars: []
586+
vars:
587+
- id: 3
587588
references:
588589
version: 1
589590
00000000:
590591
type: {class: SampleBehaviour6/ClassWithInterface1, ns: , asm: Assembly-CSharp}
591592
data:
592593
go: {fileID: 977748987}
593594
var1:
594-
id: 3
595+
id: 4
595596
00000001:
596597
type: {class: SampleBehaviour6/ClassWithInterface3, ns: , asm: Assembly-CSharp}
597598
data:
@@ -602,6 +603,11 @@ MonoBehaviour:
602603
var1: 0
603604
mat: {fileID: 2100000, guid: 7404c70251f9d0045a4aabaa49d83963, type: 2}
604605
00000003:
606+
type: {class: SampleBehaviour6/Struct, ns: , asm: Assembly-CSharp}
607+
data:
608+
var1: 0
609+
var2: 0
610+
00000004:
605611
type: {class: SampleBehaviour6/Struct, ns: , asm: Assembly-CSharp}
606612
data:
607613
var1: 1

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,8 @@ public int var1;
555555

556556
You can draw properties marked with the **[SerializeReference]** attribute with an additional type picker that allows you to manipulate what managed type will be serialized.
557557

558+
To prevent issues after renaming types use `UnityEngine.Scripting.APIUpdating.MovedFromAttribute`.
559+
558560
```csharp
559561
[SerializeReference, ReferencePicker(TypeGrouping = TypeGrouping.ByFlatName)]
560562
public Interface1 var1;

0 commit comments

Comments
 (0)