From cdbc2daf213bcef493a690de8a86cbcb6729995b Mon Sep 17 00:00:00 2001 From: Makihiro Date: Sun, 31 Dec 2023 16:00:13 +0900 Subject: [PATCH] Restore values from json on create new managed reference --- .../Editor/ManagedReferenceUtility.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/ManagedReferenceUtility.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/ManagedReferenceUtility.cs index bec2c47..5cc7223 100644 --- a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/ManagedReferenceUtility.cs +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/ManagedReferenceUtility.cs @@ -2,14 +2,27 @@ using System; using System.Reflection; using UnityEditor; +using UnityEngine; namespace MackySoft.SerializeReferenceExtensions.Editor { public static class ManagedReferenceUtility { public static object SetManagedReference (this SerializedProperty property,Type type) { - object obj = (type != null) ? Activator.CreateInstance(type) : null; - property.managedReferenceValue = obj; - return obj; + object result; + if (property.managedReferenceValue != null) + { + // Restore an previous values from json. + string json = JsonUtility.ToJson(property.managedReferenceValue); + result = JsonUtility.FromJson(json, type); + } + else + { + result = (type != null) ? Activator.CreateInstance(type) : null; + } + + property.managedReferenceValue = result; + return result; + } public static Type GetType (string typeName) {