Unity C # data Persistence and XML

Source: Internet
Author: User

Recent work requires data persistence, so share it here, and by looking at the data, persistence is largely through XML or JSON. Unity has customized the data persistence method for us, but it is more limited, and we need to do the data persistence method ourselves.

(i) Unity method

Unity provides three methods for data persistence, but data persistence is only possible with the int float string type, as in the following command

        //set the value of Key=float to 12fPlayerprefs.setfloat ("float", 12f); //a value of 12 is obtained by the key value "float", which defaults to 1f if "float" is not setPlayerprefs.getfloat ("float", 1f); //the definition is the same as SetFloatPlayerprefs.setint ("int", A); Playerprefs.getint ("int",1); Playerprefs.setstring ("Str","ABC"); Playerprefs.getstring ("Str","default");

Unity offers a limited approach, and complex data requires data sustainability on its own

(ii) C # and XML

Write read-Modify XML through C #

1) Writing XML

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml;namespacexmlrw{classProgram {Static voidMain (string[] args) {XmlDocument xmldoc=NewXmlDocument (); //join the declaration paragraph of XML, <?xml version= "1.0" encoding= "gb2312"?>XmlDeclaration xmldecl; Xmldecl= xmldoc. Createxmldeclaration ("1.0","gb2312",NULL); XmlDoc.            AppendChild (XMLDECL); //Add a root elementXmlElement Xmlelem = xmldoc. CreateElement ("","Toolandframe",""); XmlDoc.                   AppendChild (Xmlelem);  for(inti =1; I <3; i++)            {                stringIdentifystr; stringID; intcount; if(i==1) {Identifystr="Tool"; Count= -; }                Else{identifystr="Frame"; Count= +; }                //Adding secondary nodesXmlNode root = xmldoc. selectSingleNode ("Toolandframe");//Find <ToolAndFrame>XmlElement Elem = xmldoc. CreateElement (IDENTIFYSTR);//Create a <identifyStr> nodeElem. SetAttribute ("ID", IDENTIFYSTR);//Set the Node ID property//Xe1. SetAttribute ("ISBN", "2-3631-4");//Set the Node ISBN property                 for(intj=0; j<count;j++) {XmlElement Toolelem= xmldoc. CreateElement ("Elem"+i.tostring ()); Toolelem.setattribute ("ID", j.tostring ()); Toolelem.innertext="unknown [?]"; Elem.                AppendChild (Toolelem); } root. AppendChild (elem);//Add to <Employees> node} xmldoc. Save ("Toolandframe.xml"); }    }}

2) Read

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml;namespacereadxml{classProgram {Static voidMain (string[] args) {XmlDocument xmldoc=NewXmlDocument (); Xmldoc.load ("Toolandframe.xml"); List<string> strlist =Newlist<string>(); XmlNodeList NodeList= Xmldoc.selectsinglenode ("Toolandframe"). ChildNodes;//get all child nodes of the employees node            foreach(XmlNode TempinchNodeList)//Traverse all child nodes{XmlElement Elem= (XmlElement) temp;//To Convert a child node type to a XmlElement type//if (elem. GetAttribute ("genre") = = "Zhang San")//if the genre property value is "Zhang San"//{                //Elem. SetAttribute ("Genre", "Update Zhang San");//then modify the property to "update Zhang San"//}XmlNodeList Childelem = Elem. ChildNodes;//continue to acquire all child nodes of the XE child node                foreach(XmlNode nodeinchChildelem)//Traverse{XmlElement XML= (XmlElement) node;//Conversion Type//if (XML. Name = = "Author")//If you find//{                    //XML. InnerText = "Ya Sheng";//then modify//}Strlist.add (XML.                InnerText); }            }            foreach(stringStrinchstrlist)            {Console.WriteLine (str);            } Console.WriteLine (Strlist.count);        Console.readkey (); }    }}

3) Find and change

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml;namespacechangetext{classProgram {Static voidMain (string[] args) {            stringID ="Tool"; intnum =7; stringText ="New Name";                    Changeval (ID, num, text); }         Public Static voidChangeval (stringIdintNumstringtext) {XmlDocument xmldoc=NewXmlDocument (); Xmldoc.load ("Toolandframe.xml"); //list<string> strlist = new list<string> ();XmlNodeList nodeList = Xmldoc.selectsinglenode ("Toolandframe"). ChildNodes;//get all child nodes of the employees node            foreach(XmlNode TempinchNodeList)//Traverse all child nodes{XmlElement Elem= (XmlElement) temp;//To Convert a child node type to a XmlElement type                if(Elem. Name = = ID)//if the genre property value is "Zhang San"{Console.WriteLine (elem). GetAttribute ("ID")); Console.WriteLine (Elem.                    Name); XmlNodeList Childelem= Elem. ChildNodes;//continue to acquire all child nodes of the XE child node                    foreach(XmlNode nodeinchChildelem)//Traverse{XmlElement XML= (XmlElement) node;//Conversion Type                        if(XML. GetAttribute ("ID") ==Num. ToString ()) {XML. InnerText=text;  Break; }                    }                     Break; }} xmldoc.save ("Toolandframe.xml");        Console.readkey (); }    }}

This article looks and changes only changed the properties, if you need to add nodes or delete nodes, only need to find the relevant node, with the command remvoechild, and finally must be saved

Unity C # data Persistence and XML

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.