From 76ba63fc49bc5c9da3ee35b73c489d96df84ac12 Mon Sep 17 00:00:00 2001 From: Alexander Heinsius Date: Sat, 15 Aug 2020 17:39:34 +0200 Subject: [PATCH 1/2] Support writing Custom Properties to serialized private or protected fields and properties. --- .../Editor/Extensions/GameObjectExtensions.cs | 9 ++-- .../Extensions/GameObjectExtensions.cs.orig | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Extensions/GameObjectExtensions.cs.orig diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/GameObjectExtensions.cs b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/GameObjectExtensions.cs index 23eae93e..c2ee2372 100644 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/GameObjectExtensions.cs +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Editor/Extensions/GameObjectExtensions.cs @@ -173,7 +173,7 @@ public static void BroadcastProperty(this GameObject go, CustomProperty property continue; } - // Finally, look for public fields + // Finally, look for fields var csfield = FindFieldBySignature(comp, property.m_Name, objValue.GetType()); if (csfield != null) { @@ -185,7 +185,7 @@ public static void BroadcastProperty(this GameObject go, CustomProperty property private static MethodInfo FindMethodBySignature(MonoBehaviour component, string name, Type paramType) { - return component.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public).Where(info => + return component.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(info => { // Name must match if (info.Name != name) @@ -218,8 +218,7 @@ private static MethodInfo FindMethodBySignature(MonoBehaviour component, string private static PropertyInfo FindPropertyBySignature(MonoBehaviour component, string name, Type valueType) { - // Property must be public and instanced and writable - return component.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where( + return component.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where( info => info.CanWrite && info.Name == name && @@ -229,7 +228,7 @@ private static PropertyInfo FindPropertyBySignature(MonoBehaviour component, str private static FieldInfo FindFieldBySignature(MonoBehaviour component, string name, Type valueType) { - return component.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public).Where( + return component.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where( info => !info.IsInitOnly && info.Name == name && diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Extensions/GameObjectExtensions.cs.orig b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Extensions/GameObjectExtensions.cs.orig new file mode 100644 index 00000000..79eaed04 --- /dev/null +++ b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Extensions/GameObjectExtensions.cs.orig @@ -0,0 +1,48 @@ +using UnityEngine; + +namespace SuperTiled2Unity +{ + public static class GameObjectExtensions + { + // Like GetComponentInParent but doesn't check current object + public static T GetComponentInAncestor(this MonoBehaviour mono) where T : MonoBehaviour + { + return GetComponentInAncestor(mono.gameObject); + } + + // Like GetComponentInParent but doesn't check current object + public static T GetComponentInAncestor(this GameObject go) where T : MonoBehaviour + { + if (go == null) + { + return null; + } + + var parent = go.transform.parent; + if (parent == null) + { + return null; + } + + return parent.GetComponentInParent(); + } + + public static bool TryGetCustomPropertySafe(this GameObject go, string name, out CustomProperty property) + { + property = null; + + if (go == null) + { + return false; + } + + var customProperties = go.GetComponent(); + if (customProperties == null) + { + return false; + } + + return customProperties.TryGetCustomProperty(name, out property); + } + } +} From 9eb651b74fa7c8e3cfa2a3c0565a82e497273ec3 Mon Sep 17 00:00:00 2001 From: Alexander Heinsius Date: Sat, 15 Aug 2020 17:59:10 +0200 Subject: [PATCH 2/2] Delete irrelevant file --- .../Extensions/GameObjectExtensions.cs.orig | 48 ------------------- 1 file changed, 48 deletions(-) delete mode 100644 SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Extensions/GameObjectExtensions.cs.orig diff --git a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Extensions/GameObjectExtensions.cs.orig b/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Extensions/GameObjectExtensions.cs.orig deleted file mode 100644 index 79eaed04..00000000 --- a/SuperTiled2Unity/Assets/SuperTiled2Unity/Scripts/Extensions/GameObjectExtensions.cs.orig +++ /dev/null @@ -1,48 +0,0 @@ -using UnityEngine; - -namespace SuperTiled2Unity -{ - public static class GameObjectExtensions - { - // Like GetComponentInParent but doesn't check current object - public static T GetComponentInAncestor(this MonoBehaviour mono) where T : MonoBehaviour - { - return GetComponentInAncestor(mono.gameObject); - } - - // Like GetComponentInParent but doesn't check current object - public static T GetComponentInAncestor(this GameObject go) where T : MonoBehaviour - { - if (go == null) - { - return null; - } - - var parent = go.transform.parent; - if (parent == null) - { - return null; - } - - return parent.GetComponentInParent(); - } - - public static bool TryGetCustomPropertySafe(this GameObject go, string name, out CustomProperty property) - { - property = null; - - if (go == null) - { - return false; - } - - var customProperties = go.GetComponent(); - if (customProperties == null) - { - return false; - } - - return customProperties.TryGetCustomProperty(name, out property); - } - } -}