以前说实话没用过配置文件,程序打包后都是写死的。但是现在工作了做项目也要考虑很多问题了,unity通过配置文件修改数据有很多方式,txt文本,json,XML,INI等等。问了龙哥让我先用XML。我整了整,如下:
要实现的功能是通过修改配置文件中的值来改变cube的位置,颜色,以及旋转速度还有3Dtext的。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml;
using UnityEngine.UI;
public class BilXmlTest : MonoBehaviour
{
private string xmlPath;
public Button CreateBtn;
// public Button SaveBtn;
public GameObject [] cube;
public GameObject [] text;
private float SpeedCube;
private float SpeedText;
private Renderer cuberender;
private TextMesh textrender;
void Start()
{
CreateBtn.onClick.AddListener(ReadXml);
xmlPath = Application.streamingAssetsPath + "/XMLFile.xml";
cuberender = cube[0].GetComponent<Renderer>();
textrender = text[0].GetComponentInChildren<TextMesh>();
}
private void Update()
{
cube[0].transform.Rotate(0, 0, 1 * SpeedCube);
text[0].transform.Rotate(0, 1 * SpeedText, 0 );
}
public void ReadXml()
{
if (File.Exists(xmlPath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
//Cube
Vector3 Tempcube = new Vector3();
Tempcube.x = float.Parse( xmlDoc.GetElementsByTagName("Item")[0].SelectSingleNode("pos").SelectSingleNode("X").InnerText);
Tempcube.y = float.Parse(xmlDoc.GetElementsByTagName("Item")[0].SelectSingleNode("pos").SelectSingleNode("Y").InnerText);
Tempcube.z = float.Parse(xmlDoc.GetElementsByTagName("Item")[0].SelectSingleNode("pos").SelectSingleNode("Z").InnerText);
float SpeedXML1 = float.Parse(xmlDoc.GetElementsByTagName("Item")[0].SelectSingleNode("Speed").InnerText);
SpeedCube = SpeedXML1;
cube[0].transform.position = Tempcube;
string color1 = xmlDoc.GetElementsByTagName("Item")[0].SelectSingleNode("Color").InnerText;
if (color1 == "red")
{
cuberender.material.color = Color.red;
}
else if (color1 == "green")
{
cuberender.material.color = Color.green;
}
//Text
float SpeedXML2 = float.Parse(xmlDoc.GetElementsByTagName("Item")[1].SelectSingleNode("Speed").InnerText);
SpeedText = SpeedXML2;
string color2 = xmlDoc.GetElementsByTagName("Item")[1].SelectSingleNode("Color").InnerText;
if (color2 == "red")
{
textrender.color = Color.red;
}
else if (color2 == "green")
{
textrender.color = Color.green;
}
}
}
}
这是我的XML配置文件,生成XML文件有两种方法。
第一种:直接在StreamingAssets文件夹下创建text文本,然后改xml后缀名,然后把你想要的内容写进去。
第二种:就是在程序里完成对XML的创建。(根据自己的项目需求来吧)
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Item>
<name>Cube</name>
<Color>red</Color>
<Speed>10</Speed>
<pos>
<X>-4</X>
<Y>3</Y>
<Z>0</Z>
</pos>
</Item>
<Item>
<name>Circle</name>
<Color>red</Color>
<Speed>0.1</Speed>
</Item>
</Data>
程序创建XML的方法
public void CreateXML()
{
if (!File.Exists(path))
{
XmlDocument xml = new XmlDocument();
XmlElement root = xml.CreateElement("UserData");
XmlElement element = xml.CreateElement("Item1");
XmlElement elementChild1 = xml.CreateElement("name");
elementChild1.InnerText = "cube";
XmlElement elementChild2 = xml.CreateElement("pos");
XmlElement elementposX = xml.CreateElement("X");
elementposX.InnerText = "100";
XmlElement elementposY = xml.CreateElement("Y");
elementposY.InnerText = "50";
XmlElement elementposZ = xml.CreateElement("Z");
elementposZ.InnerText = "100";
XmlElement elementS = xml.CreateElement("Item2");
XmlElement elementChild1S = xml.CreateElement("name");
elementChild1S.InnerText = "circle";
XmlElement elementChild2S = xml.CreateElement("pos");
XmlElement elementposXS = xml.CreateElement("X");
elementposXS.InnerText = "20";
XmlElement elementposYS = xml.CreateElement("Y");
elementposYS.InnerText = "100";
XmlElement elementposZS = xml.CreateElement("Z");
elementposZS.InnerText = "10";
elementChild2.AppendChild(elementposX);
elementChild2.AppendChild(elementposY);
elementChild2.AppendChild(elementposZ);
element.AppendChild(elementChild1);
element.AppendChild(elementChild2);
root.AppendChild(element);
elementChild2S.AppendChild(elementposXS);
elementChild2S.AppendChild(elementposYS);
elementChild2S.AppendChild(elementposZS);
elementS.AppendChild(elementChild1S);
elementS.AppendChild(elementChild2S);
root.AppendChild(elementS);
xml.AppendChild(root);
xml.Save(path);
}
}