Restore values from json on create new managed reference

This commit is contained in:
Makihiro 2023-12-31 16:00:13 +09:00
parent 5d43ced8cc
commit cdbc2daf21

View File

@ -2,14 +2,27 @@
using System; using System;
using System.Reflection; using System.Reflection;
using UnityEditor; using UnityEditor;
using UnityEngine;
namespace MackySoft.SerializeReferenceExtensions.Editor { namespace MackySoft.SerializeReferenceExtensions.Editor {
public static class ManagedReferenceUtility { public static class ManagedReferenceUtility {
public static object SetManagedReference (this SerializedProperty property,Type type) { public static object SetManagedReference (this SerializedProperty property,Type type) {
object obj = (type != null) ? Activator.CreateInstance(type) : null; object result;
property.managedReferenceValue = obj; if (property.managedReferenceValue != null)
return obj; {
// 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) { public static Type GetType (string typeName) {