Unity3d using XML for simple configuration file modification

Source: Internet
Author: User

1, the first is to look at:

Start running the project as shown in

2, click the green button to modify the configuration file and then click the Modify button to successfully modify the block color


3, the next is the realization of the process:

First create a cube in hierarchy, and then create the scenes and scripts folders under assets, respectively, to save the current scene and the script we wrote. Under the Scripts folder, right-click to create a C # script, then we start to create an XML file and the corresponding folder in the script. Declare a private string M_sxmlpath externally, to display the XML path, and then to configure and create the XML in the start () method,

void Start () {
M_sxmlpath = Application.datapath + "/configs/config.xml";
Debug.Log ("Application Path:" + Application.datapath);
Debug.Log ("config + Path:" + M_sxmlpath);
Createxml ();//Create XML
}

Create the XML method as follows:

private void Createxml () {
if (! File.exists (M_sxmlpath))//If this directory does not exist, we will create a directory like this

{
XmlDocument xmldoc = new XmlDocument ();
XmlElement data = xmldoc.createelement ("Data");
Xmldoc.appendchild (data);
XmlElement color = xmldoc.createelement ("color");
Data. AppendChild (color);
Xmldoc.save (M_sxmlpath);
Debug.Log ("Create XML Complete");
}
}

After creating the form we click on the XML to have the following information in it:


Corresponding we need to update our modified XML file:

private void Updatexml (uint color) {
if (file.exists (M_sxmlpath))

{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (M_sxmlpath);
XmlNode Colornode = Xmldoc.selectsinglenode ("Data/color");
Colornode.innertext = color. ToString ();
Xmldoc.save (M_sxmlpath);
Debug.Log ("Modified successfully:" + color);

}
}

Next is to implement the change of the color of the BLOCK:

private void Modifycubecolor () {
if (file.exists (M_sxmlpath)) {
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (M_sxmlpath);
XmlNode Colornode = Xmldoc.selectsinglenode ("Data/color");
UINT color = Convert.touint32 (Colornode.innertext);
Debug.Log ("Read color:" + color);


Gameobject cube = gameobject.find ("cube");
if (color = = 0xff0000) {
Cube.renderer.material.color = color.red;
}else if (color = = 0x00ff00) {
Cube.renderer.material.color = Color.green;
}
}
}

We want to implement a click in the button will modify the corresponding block, we need to create our button:

void Ongui () {
Create Modify Color buttons
Gui. Label (New Rect (10,10,200,30), "Modify configuration File");
if (GUI. button (new Rect (10,40,100,30), "red")) {
Updatexml (0xff0000);
}
if (GUI. button (new Rect (10,70,100,30), "green")) {
Updatexml (0X00FF00);
}


Create button use to modify Cube ' color
Gui. Label (New Rect (10,110,200,30), "Modify block according to profile");
if (GUI. button (new Rect (10,140,200,30), "Modify Block")) {
Modifycubecolor ();
}

}

Unity3d using XML for simple configuration file modification

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.