Data persistence
When it comes to data persistence, in Unity's game development is very important, whether it is on the local and server side, data persistence is our learning difficulties, data persistence technology There are many kinds of, here only select several, and now I have learned, in the next time will be organized here.
Part1:playerprefs class
This is given in the unity Holy Scriptures,
Playerprefs Game Archive Description describes storing and accessing game archives in a game session. This is persistent data storage, such as saving game records. Editor/standalone Editor/desktop platform Mac OS on Mac OS X playerprefs is stored in the ~/library/preferences folder, called Unity. [Company name]. [Product Name].plist, where the company name and product name are set in Project setting, the. plist file can be used for the editor and desktop platform to run. (Open find, hold down the option key, and click "Go →" library to find the Preferences folder.) Windows under Windows platform, Playerprefs is stored under the Hkey_current_user\software\[company name]\[product name] key in the registry (open "Run" Enter regedit to open the registry, where company name and product name name are set in Project setting. Linux in Linux,playerprefs is stored in ~/.config/unity3d/[companyname]/[productname]. Where the CompanyName and ProductName names are set in Project setting Windows store in Windows store,playerprefs is stored in%userprofile%\appdata\ Local\packages\[productpackageid]>\localstate\playerprefs.dat. Windows Phone in Windows Phone 8,playerprefs is stored in the app's own folder, see: Windows.Directory.localFolderWebPlayer Web Page in Web platform, Playerprefs is stored in a binary file, see the corresponding platform location below: Mac OS X: ~/library/preferences/unity/webplayerprefswindows:%appdata%\unity\ Webplayerprefs A game archive file corresponds to a Web player URL and the file size is limited to 1MB.If this limit is exceeded, setint, setfloat, and setstring will not store the value and throw a Playerprefsexception exception. static function deleteallremoves all keys and values from the preferences. Functions Use with caution. Remove all keys from the game archive. Please use it carefully. Deletekeyremoves key and its corresponding value from the preferences. Removes the key and its corresponding value from the game archive. Getfloatreturns the value corresponding to key in the preference file if it exists. If present, returns the floating-point value corresponding to key in the game archive file. Getintreturns the value corresponding to key in the preference file if it exists. If present, returns the integer value corresponding to key in the game archive file. Getstringreturns the value corresponding to key in the preference file if it exists. If present, returns the string value corresponding to key in the game archive file. Haskeyreturns true if key exists in the preferences. Returns true if key exists in the game archive. Savewrites all modified preferences to disk. Write all modification parameters to the hard disk. Setfloatsets the value of the preference identified by key. Sets the floating-point value determined by key. Setintsets the value of the preference identified by key. Sets the integer value determined by the key key. Setstringsets the value of the preference identified by key. Sets the string value determined by key.
Below I have written a small piece of code to show how to use:
Public classperson{/// <summary> ///used to store names/// </summary> Private stringname; /// <summary> ///using attribute characteristics to store a variable into playerprefs, data persistence/// </summary> /// <value>The name.</value> Public stringName {Get{ returnPlayerprefs.getstring ("Name"); } Set{playerprefs.setstring ("Name", value); Playerprefs.save (); } } /// <summary> ///return Age/// </summary> Private intAge ; Public intage{Get{ returnPlayerprefs.getint (" Age"); } Set{playerprefs.setint (" Age", value); Playerprefs.save (); } } /// <summary> ///constructor with no parameters/// </summary> PublicPerson () {}/// <summary> ///Constructors with parameters/// </summary> /// <param name= "_name" >Name.</param> /// <param name= "_age" >Age .</param> PublicPerson (string_name,int_age) { This. Name =_name; This. Age =_age; } Public Override stringToString () {return string. Format ("[person:name={0}, age={1}]", Name, age); }}
Storing the data in a property is also a good way to do it. It is important to note that after setting the value, call the Save () method.
Part2:json Data Persistence
First need to use a JSON dynamic link library, Jsonfx, online can be found, here is a Baidu disk connection link: https://pan.baidu.com/s/1kVmc4Qz Password: kqg2
Then we will import this dynamic link library into the Plugins directory of our project and refer to it. Here is the Code section
This is the target class to be converted with JSON,
usingSystem.Collections;usingSystem.Collections.Generic; Public classBody { Public stringname; Public intAge ; PublicBody () {} PublicBody (string_name,int_age) { This. Name =_name; This. Age =_age; } Public Override stringToString () {return string. Format ("[Body:name:{0},age:{1}]", Name,age); }}
The following class is a test class for conversions:
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine;usingJsonfx.json;usingSystem.IO; Public classTestjson:monobehaviour {/// <summary> ///static field save path/// </summary> Static stringPathdata="/data.txt"; voidStart () {Debug.Log (Application.datapath); Write (); Read (); } /// <summary> ///writes JSON data, serializes the class/// </summary> Public Static voidWrite () {Body Body=NewBody ("Zhangsan", -); stringText =jsonwriter.serialize (body); File.writealltext (Datapathtest.getdatapath ()+pathdata, text); } /// <summary> ///Deserializes text into a class/// </summary> Public Static voidRead () {stringstr = File.readalltext (Datapathtest.getdatapath () +pathdata); Body BD= jsonreader.deserialize<body>(str); DEBUG.LOG (Bd. ToString ()); }}
The following is just a class to get the path, (a bit superfluous)
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine; Public classDatapathtest { Public Static stringGetdatapath () {if(Application.platform = =runtimeplatform.iphoneplayer) {stringPath = Application.dataPath.Substring (0, Application.dataPath.Length-5); returnPath +"/dcoument"; } Else if(Application.platform = =runtimeplatform.android) {returnApplication.persistentdatapath +"/"; } Else { returnApplication.datapath; } }}
Finally add an easy wrong point, I was at the beginning of the transformation with Jsonfx some problems, the body object into a JSON string, let the later in the process of conversion to body error, then Baidu found the reason, the specific can refer to this article:/http Www.cnblogs.com/zhuhongjongy/p/4974473.html
It's written here today,
Cond....
Unity Game Development Learning path-data persistence