I’m trying to do a Save/Load object mechanism for an app I’m developing and the documentation and tutorials online are very confusing.
The user interacts with objects in the scene, and can save the alterations made to the object. I want to save the object and have the user be able to spawn them again from a list that is visible to him. For example, I want the user to do different kinds of paintings on virtual canvases and want him to be able to save them, and later choose from all of his saved paintings from a list and spawn them once he clicks on them.
From what I’ve read, you can’t save a prefab, or can you? If you can save the prefab data, how do i do this? and more importantly, how do I populate a new “canvas” object with the texture from the painting he drew then. Is it possible to make a list from the saved paintings on files that can be visible to the user?
Can anyone point me towards a clear documentation site or youtube tutorial where I can see how to do this?
thank you
An excellent discussion of loading/saving in Unity3D by Xarbrough:
Loading/Saving ScriptableObjects by a proxy identifier such as name:
When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls new to make one, it cannot make the native engine portion of the object.
Instead you must first create the MonoBehaviour using AddComponent() on a GameObject instance, or use ScriptableObject.CreateInstance() to make your SO, then use the appropriate JSON “populate object” call to fill in its public fields.
If you want to use PlayerPrefs to save your game, it’s always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:
Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.
Thanks Kurt! Somehow your set of links came in super handy and I was able to figure out something! They were really super clear and to the point. I really took a lot of time trying to see if I understood it before testing, but I ended up giving up and giving it a try without understanding much. It ended up working! I was able to shape it the way I wanted it and I learned so much more.
I’m not completely done with the code, and frankly I still don’t understand it fully. But now I feel like I got down the basics for a save/load system and feel more comfortable googling more specific questions instead of being super lost.
Don’t worry bout that… code is never “done.” Code is merely good enough to ship out!
Don’t worry about that either… understand it as best as you need to get it working, and always leverage documentation and explaining it to your dog, which you can see me doing in my avatar image above.
And in a month when you’ve forgotten how it works (that’s how my brain forgets anyway), at least now you have the tools to put it back together in your brain, plus it is quality time for you and your dog.