Unity3d using XML for simple configuration file changes

Source: Internet
Author: User

1, the first is to look at:

Start the execution of the project, for example, as seen

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2nsdw9qawpp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

2. Click on the green button to change the configuration file and click Change button to change the color of the box successfully


3, the next is the realization of the process:

First create a cube in hierarchy. Next, create the scenes and scripts directories under assets, each of which is used to save the current scene and the script we wrote. Right-click below the scripts directory to create a C # script that we'll start with in the script to create an XML file and the corresponding directory. Declare a private string m_sxmlpath externally; Use to display the XML path. The XML is then configured and created 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 an XML method such as the following:

private void Createxml () {
if (! File.exists (M_sxmlpath))//Assuming this folder does not exist, we create a folder 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 Completion");
}
}

After creating the form we click on the XML will have information such as the following in the inside:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2nsdw9qawpp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Accordingly 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 ("changed successfully:" + color);

}
}

Next is the implementation of the change in 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 the Click button in the box will change the corresponding block, we need to create our button:

void Ongui () {
Create Modify Color buttons
Gui. Label (New Rect (10,10,200,30), "change config 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), "alter block according to Profile");
if (GUI. button (new Rect (10,140,200,30), "alter Block")) {
Modifycubecolor ();
}

}

Unity3d using XML for simple configuration file changes

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.