Skip to content

Commit 6a845bc

Browse files
committed
Copy throws exception on UnityEngine.Object. Added copy method for unity object overwrite.
1 parent fdf48d6 commit 6a845bc

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

Runtime/SaveLoadManager.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public void Save(object obj, string filename, string folder = null)
8282
/// <returns>duplicated instance</returns>
8383
public object Copy(object obj)
8484
{
85+
if (obj is UnityEngine.Object)
86+
{
87+
throw new ArgumentException("UnityEngine.Object and child types not supported by copy method.");
88+
}
8589
var saveLoadMethod = GetSaveLoadMethod(saveMethod);
8690
return saveLoadMethod.Copy(obj);
8791
}
@@ -95,6 +99,10 @@ public object Copy(object obj)
9599
/// <returns>duplicated instance</returns>
96100
public T Copy<T>(T obj)
97101
{
102+
if (obj is UnityEngine.Object)
103+
{
104+
throw new ArgumentException("UnityEngine.Object and child types not supported by copy method.");
105+
}
98106
var saveLoadMethod = GetSaveLoadMethod(saveMethod);
99107
return (T)saveLoadMethod.Copy(obj);
100108
}
@@ -221,6 +229,17 @@ public bool LoadUnityObjectOverwrite(UnityEngine.Object objectToOverwrite, strin
221229
JsonUtility.FromJsonOverwrite(savedObj.jsonData,objectToOverwrite);
222230
return true;
223231
}
232+
233+
/// <summary>
234+
/// Copies the serializable fields from one UnityEngine.Object to another
235+
/// </summary>
236+
/// <param name="toCopy">object which should be copied</param>
237+
/// <param name="toOverwrite">object onto which copied fields should be written</param>
238+
public void CopyUnityObjectOverwrite(UnityEngine.Object toCopy, UnityEngine.Object toOverwrite)
239+
{
240+
var jsonData = JsonUtility.ToJson(toCopy);
241+
JsonUtility.FromJsonOverwrite(jsonData,toOverwrite);
242+
}
224243

225244
/// <summary>
226245
/// JsonSerializedUnityObject

Tests/Runtime/SaveLoadManagerTests.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class SaveLoadTestObject
1717
public int count;
1818
}
1919

20+
[Serializable]
2021
public class SaveLoadTestUnityObject : ScriptableObject
2122
{
2223
public string textValue = "";
@@ -207,16 +208,33 @@ public void CanCopyUnityObject([Values] SerializationMethodType method)
207208
{
208209
var manager = CreateManager(method);
209210
var testObj = ScriptableObject.CreateInstance<SaveLoadTestUnityObject>();
210-
testObj.textValue = "MyValue";
211+
var loadedTestObj = ScriptableObject.CreateInstance<SaveLoadTestUnityObject>();
211212

212-
var loadedTestObj = manager.Copy(testObj);
213+
testObj.textValue = "MyValue";
213214

215+
manager.CopyUnityObjectOverwrite(testObj,loadedTestObj);
216+
214217
Assert.IsTrue(loadedTestObj.textValue == testObj.textValue);
215218

216219
Object.Destroy(testObj);
217220
Object.Destroy(loadedTestObj);
218221
Object.Destroy(manager);
219222
}
223+
224+
[Test]
225+
public void CopyThrowsArgumentExceptionOnUnityObject([Values] SerializationMethodType method)
226+
{
227+
var manager = CreateManager(method);
228+
var testObj = ScriptableObject.CreateInstance<SaveLoadTestUnityObject>();
229+
230+
Assert.Throws<ArgumentException>(() =>
231+
{
232+
manager.Copy(testObj);
233+
});
234+
235+
Object.Destroy(testObj);
236+
Object.Destroy(manager);
237+
}
220238

221239
[Test]
222240
public void CanSaveAndLoadDictionary([Values(

0 commit comments

Comments
 (0)