- Home /
Unity Serilizer taking to long to Save/load?
I have had alot of problems with Unity Serializer recently and one of them was long saving and loading times. All i have in my game is a island and alot of tree's Unity Serializer is tracking 759 objects and it now takes to long to save and load.
Any ideas,
Oh also how do you save a object that was created in runtime??? ;)
Thanks
Answer by OperationDogBird · Sep 14, 2013 at 11:23 PM
You cannot save an 'object' while the game is playing. You can save any data that represents the current state of that object (bool, float, int, string).
Im not exactly sure what you mean by unity serializer saving, do you mean player prefs? I suggest using a different method of saving (player prefs writes to disk every time you Save(), thus long save times).
I use Preview Labs Player Prefs, very fast saving because you just add/modify keys in a hash table, then when you click quit in the game just call Flush() and it will write to disk.
//EDIT ( Additional information regarding saving simple data types)
This is where your logic will come into play. There is a million and 1 ways to do what you want, my way will more than likely not be the best solution or even a decent solution :P, but i will try to walk you through a semi-simple object example.
SwampTreeVersionA - (lets pretend this is one of your tree types)
You first need to create the global identifier key for this type of tree. Maybe adding a prefix is the best way? I will take that route since string operations are heavy, and the shortest identity of a string is the first unique char set.
"00_ST_VsA" - this will be our global identifier key prefix for the first instance of 'Swamp Tree Version A'
(float) 00_ST_VsA_Pos_(X,Y,Z) - these three values(suffix X,Y,Z) will represent the position (your trees probly dont move, but for future reference maybe?) //Same for rotation via local euler angles //( x, y, z, w ) for quaternion
(int) 00_ST_VsA_state - enum state of the tree (full, chopped1, chopped2, dead)
So thats all well and good, but how do we actually save that data?
Each piece of data is stored in a hashtable, which consists of Key-Value-Pairs.
{"String", "StringValue"} // string
{"Bool", false} // bool
{"Single", 0.03f} // float
{"Int", 22} // int
You can only save the 4 fore-mentioned types, these will be our values. The key to access each value will be a unique identifying string.
Saving these values will go hand in hand with loading the saved values, as would encrypting a file go hand in hand with decrypting that file.
So lets save our swamp tree!
We will save the current state of the SwampTreeVersionA ( enum represented by an int value )
PreviewLabs.PlayerPrefs.SetInt("00_ST_VsA_state", (int)yourTreesState);
Technically that is all you really need for saving a simple trees state. Since a tree doesnt move or rotate (well, most of the time). When you move on to saving classes the data can be a bit tougher to manage, but with a bit of thought and good planning, you will find a way to almost automate the entire saving process in a single method call.
Hope this helps you and many others with saving data. :)
I was thinking about using PlayerPrefs ins$$anonymous$$d of Unity serilizer. I'm new to PlayerPrefs and how they work so could you give a me a example, so my game AT$$anonymous$$ is all about gathering materials so when you gather wood from a tree the tree gets cut down how could i tell PlayerPrefs that it shouldn't load that tree when i next play
I guess you talk about:
http://whydoidoit.com/unityserializer/
when you use third-party stuff you should be more precise what you're using. Either include a link or a better description.