- Home /
Deep Copy of ScriptableObject
Hey, I know a similar topic is floating around in Answers, but it's some years old and doesn't work, at least for me...
So I want to copy a scriptableObject WITH its references... CopySerialized doesn't work as well as Instantiate... does not...
What am I doing wrong?
internal static PersonalObject Personalise(this PersonalObject pObject, Object owner)
{
string newPath = CreateAssetDirectory(pObject.GetType(), owner);
newPath = AssetDatabase.GenerateUniqueAssetPath(newPath + "/" + pObject.GetType().Name + ".asset");
PersonalObject newObject = ScriptableObject.Instantiate(pObject) as PersonalObject;
AssetDatabase.CreateAsset(newObject, newPath);
EditorUtility.CopySerialized(pObject, newObject);
newObject.isPersonalised = true;
newObject.ownerID = owner.GetInstanceID();
return newObject;
}
I shuffled order in any way I could imagine, also used AssetDatabase.CopyAsset, nothing works. Is CopySerialized supposed to use the SerializedObject?
Thanks...
Answer by Bunny83 · Aug 13, 2017 at 02:09 PM
Serialized references will always stay references. The only exception are references to child objects when cloning gameobjects.
ScriptableObjects are assets. Assets can only reference other assets. So if you want a "true" deep copy it would have a quite heavy effect. For example when your scriptable object references other scriptable objects which might reference prefabs. A deep copy would require to also duplicate those "referenced" scriptableobjects as well. However since they are assets they need a new file name. However the referenced prefabs would also need to be duplicated. If the prefabs reference Meshes and materials those need to be duplicated as well. The Material will reference a shader and some textures which would be duplicated as well. I can't really imagine any situation where you need that.
In most cases you will have only some specific references you want to duplicate. In that case you have to duplicate those manually. Keep in mind that all assets need to be stored in some sort of asset file.
If that doesn't answer your question you should be more specific about what kind of references you talk about and what you mean by "deep copy".
well quite a while ago and i dug into other stuff, but still I can't even copy a string from one object to another...
what am i doing wrong? Apply$$anonymous$$odifiedProperties as well as serializedObject.Update() don't work.
Your answer
Follow this Question
Related Questions
How do I remove a ScriptableObject reference after the script has been deleted? 0 Answers
Create List of custom class types, for use in custom editor 0 Answers
[SOLVED] Custom editor can't acces to variables of a scriptable object 1 Answer
Reorderable list of UnityEvents 2 Answers
Implementing a 2D array as a field on a ScriptableObject? 1 Answer