Sunday, March 22, 2020

Serialization of Scriptable Objects in Unity (Nesting) pt2. Binary!

In my last blog I said I wasn't going to do a binary saver, well - actually I was asked on the Mister Taft creates Discord server to post up a binary version, so here it is!

Disclaimer: I am in NO way a professional Unity coder, I am hacky and this might not (probably isn't) the best way to do this! But it works for me and I and hopefully, this is useful to someone, if so or if you have any questions please come to the Discord channel and let me know!

Not one to shy away I have modified the classes from <Part 1> to allow a flip between JSON and binary based on a selection in the editor!

Thanks again to my friend Ashfid from the discord server, we had a discussion on utilising an approach which was also featured in a GameGrind video.

I have again left out a lot of error checking for sake of clarity, but make sure to put some in your own code :) 

Thanks, Ashfid, MisterTaft, Bomara and all on the Mister Taft Discord.

Much love to you all, this is an absolutely crazy time, stay safe!

PappaP x

ps. I'm moving to Pastebin for the code, whilst not as pretty - it supports cutting the code out directly!

Downloads:



First up, a change to our SerializeListString class!

I have moved the Struct out into its own class SerialItem. There is no other change to this class.

Exceprt from SerializableListString.cs




The BinarySaver class.

Almost completely a shameless rip off from the GameGrind video mentioned above, I'd suggest having a watch if you want to understand the nuances. The methods in this class are static, so we can call them from anywhere.

Essentially I am utilising a generic method to ensure I can save out my SerializeListString class object. I'll let the code speak for itself as it's quite simple - there are some comments inline to explain where I think it's required.

For the binary save, I'm using the ./saves/ directory in our application persistent data path.

BinarySaver.cs



Meat and potatoes, again! Or How to actually use the BinarySaver!

As promised here we actually get to control whether we want to save out in JSON or Binary.

I am creating an enum to represent the two different save states and creating a public variable in the inspector to allow us to switch between states.

Exceprt from InventorySaver.cs




Switch for Saves! (and loads...)

OK, that was an awful pun, did anyone else get it? I didn't think so.

So, what we need to do now is this. We know from part 1 that our SL object is all we care about as we recreate/or save our inventory from that object; therefore BuildSaveData() and ImportSaveData() don't need to change at all! Nice!

In fact, the only things we actually need to change are the two functions LoadScitpables() and SaveScriptables(). That's a little misleading, as what I'm also going to do is break out the code for saving and loading via JSON into their own methods. The code inside the method is the same as we created in part 1.

JSONSave()


JSONLoad()

And next, I'm going to create a couple of complementary methods for BinarySave() and BinaryLoad(). I'm sure there are loads among you who can see where I'm going with this, hold on to your hats we are almost there! :D

BinarySave()



BinaryLoad()



Having the static methods saved earlier in the BinarySaver class really help to make this more readable! I should really go back and pull out the JSON methods into their own static method class too. Feel free to do that if you want!

Igor, Flip the switch!

These are the last couple of changes, first up, SaveScriptables. We take advantage of the enum saveType we have set in the inspector, depending on which one is set we either call JSONSave or BinarySave()!

SaveScriptables()

LoadScriptables()


And that's really it! You can now choose between using JSON or Binary to save your inventory object!

Hope you enjoyed!

PappaP x

2 comments:

  1. I actually saw the git hub version before i saw your blog. This is cool! Trying to put this in project. You explained very well. Thanks.

    ReplyDelete

ASP.NET Core(porate) , yup I'm hilarious :|

 So, lately, I have been in full corporate mode. That means looking after my team of engineers and when I have some gaps in my calendar work...

Most Viewed