Today to review the basic knowledge of data storage, unity3d commonly used in the storage of data with unity Playerprefs, XML documents, JSON documents, and commonly used lightweight database SQL, involving a lot of trivial knowledge, here to summarize their own, It is convenient to know the new in the future.
Playerprefs Storing data
Playerprefs storage is very simple, but the availability is not strong, generally only for the debugging process to store a small amount of data, very few large-scale use
Applicable devices: Mac OSX, Linux, Windows,web Players
Storage mechanism: Key-value (similar to dictionary key-value pairs, indexed by key values to corresponding data)
Variable types can be stored: int, float, string
Common methods: SetFloat store data of type float G etfloat getting data of type float
setint storing data of type int GetInt getting data of type int
SetString Storing string-type data GetString getting data of type string
DeleteAll Delete all playerprefs data haskey determine if there is data for the key value
The simple code example below, is not very simple?
void exampleplayerprefs () { playerprefs.setfloat ("playerscore", 10f); Debug.Log (Playerprefs.getfloat ("playerscore"));}
Storage and parsing of XML
(1) XML common classes and their common properties and methods
XmlNode
InnerText Gets or sets the value of the node and all its child nodes (only element nodes have)
Value Gets or sets the values of the node (property node owned only)
AppendChild adds the specified node to the end of the node's child nodes list
ChildNodes get all child nodes of a node
Firestchild
XmlDocument
Createxmldeclaration Creating a XmlDeclaration node with the specified value
CreateElement creating an element with the specified name
CreateNode creating a XmlNode with the specified node type
AppendChild adding the specified node to the end (inherited from XmlNode)
Save saves the XML document to the specified file
Load loads an XML document from the specified URL
LOADXML loading an XML document from a specified string
XmlElement
SetAttribute setting the value of an attribute with the specified name
HasAttributes determines whether the element node has attributes
GetAttribute returns the property value with the specified name
(2) Steps for generating XML data
How do I generate local XML data in the Unity engine?
First step: Introduce the C # namespace System.Xml
Step two: Generate an XML document (XmlDocument Class)
Step three: Generate a root element (XmlElement Class) to add to the Document object
Fourth step: Looping through the generation of child elements added to the parent element
Fifth step: Save the generated XML document
(3) XML parsing steps
When parsing, it is necessary to create the corresponding data model according to the structure of XML file to accept the parsed data!!!
Storage and parsing of JSON
JSON generally uses two class libraries, one System.json (for JSON generation), and one is Litjson (JSON parsing is easy)
JSON generation
voidLitctreatjson () {Jsondata hero=NewJsondata (); hero["Heroname"] ="Knorr-Bremse"; hero["Hp"] = -; hero["Attack"] = +; hero["Skills"] =NewJsondata (); Jsondata skill1=NewJsondata (); skill1["KeyCode"] ="Q"; skill1["Name"] ="Big Kill quartet"; skill1["Mp"] = -; skill1["CD"] =8; hero["Skills"]. ADD (SKILL1); Jsondata skill2=NewJsondata (); skill2["KeyCode"] ="W"; skill2["Name"] ="Crippled Blow"; skill2["Mp"] = +; skill2["CD"] =5; hero["Skills"]. ADD (SKILL2); Jsondata Skill3=NewJsondata (); skill3["KeyCode"] ="E"; skill3["Name"] ="Ruthless Iron hand"; skill3["Mp"] =Ten; skill3["CD"] =9; hero["Skills"]. ADD (SKILL3); Jsondata SKILL4=NewJsondata (); skill4["KeyCode"] ="R"; skill4["Name"] ="Norwalk Guillotine"; skill4["Mp"] = -; skill4["CD"] = -; hero["Skills"]. ADD (SKILL4); }
JSON parsing
voidLitparsejson () {FileInfo file=NewFileInfo (Application.datapath +"/lol.json"); StreamReader SR=NewStreamReader (file. OpenRead (), Encoding.UTF8); stringContent =Sr. ReadToEnd (); Jsondata Data=jsonmapper.toobject (content); foreach(Jsondata Iteminchdata["Skills"]) {print (item["name"]); } Hero Hero; Hero= jsonutility.fromjson(content); Sr. Close (); Sr. Dispose (); }
Storage and analysis of UNITY3D data storage Playerprefs,xml,json data