Skip to content

Commit cc17228

Browse files
Bruno MikoskiBruno Mikoski
authored andcommitted
add: new try method to try to get with the backing field as well
1 parent 1cf590e commit cc17228

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using BrunoMikoski.ScriptableObjectCollections.Utils;
2+
using UnityEditor;
3+
4+
namespace BrunoMikoski.ScriptableObjectCollections
5+
{
6+
public static class SerializedObjectExtensions
7+
{
8+
public static bool TryFindProperty(this SerializedObject serializedObject, string propertyName, out SerializedProperty property, bool tryBackingFieldIfFails = true)
9+
{
10+
property = serializedObject.FindProperty(propertyName);
11+
if (property == null && tryBackingFieldIfFails)
12+
{
13+
property = serializedObject.FindProperty(propertyName.AsBackingField());
14+
}
15+
16+
return property != null;
17+
}
18+
}
19+
}

Scripts/Editor/Extensions/SerializedObjectExtensions.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Editor/Extensions/SerializedPropertyExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace BrunoMikoski.ScriptableObjectCollections
1111
{
12-
1312
public static class SerializedPropertyExtensions
1413
{
1514
public static FieldInfo GetFieldInfoFromPathByType(this SerializedProperty property, Type parentType)
@@ -303,4 +302,4 @@ static T DescendHierarchy<T>(object targetObject, List<string> splitName, List<i
303302
return DescendHierarchy<T>(newObj, splitName, splitCounts, depth + 1);
304303
}
305304
}
306-
}
305+
}

Scripts/Editor/Generators/CollectionGenerators.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,7 @@ private static void CopyFieldToSerializedProperty(
268268
return;
269269

270270
// Get the property to copy the value to.
271-
SerializedProperty serializedProperty = serializedObject.FindProperty(field.Name);
272-
if (serializedProperty == null)
273-
{
274-
serializedProperty = serializedObject.FindProperty(field.Name.AsBackingField());
275-
if(serializedProperty == null)
276-
return;
277-
}
271+
serializedObject.TryFindProperty(field.Name, out SerializedProperty serializedProperty);
278272

279273
object value = field.GetValue(owner);
280274

0 commit comments

Comments
 (0)