Unity3D 使用XML进行简单的配置文件修改
来源:互联网 发布:批量编辑图片的软件 编辑:程序博客网 时间:2024/06/06 14:27
1、首先是看看效果图:
开始运行项目如下图所示
2、点击绿色按钮修改配置文件再点击修改按钮成功修改方块颜色
3、接了下是实现的过程:
首先在Hierarchy创建一个cube,其次在Assets下分别创建Scenes和Scripts文件夹,分别是用来保存的的当前的场景和我们所写的脚本。在Scripts文件夹下面右键创建一个C#脚本,接我们在脚本里面开始创建一个XML文件和对应的文件夹。在外部声明一个private string m_sXmlPath;用来显示xml路径,接着在Start()方法中配置和创建XML,
void Start () {
m_sXmlPath = Application.dataPath + "/Configs/config.xml";
Debug.Log("Application Path:" + Application.dataPath);
Debug.Log("config.xml Path:" + m_sXmlPath);
CreateXml();//创建XML
}
创建XML方法如下:
private void CreateXml(){
if(!File.Exists(m_sXmlPath))//如果不存在这个目录我们就创建一个这样的目录
{
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("创建XML完毕");
}
}
创建形成之后我们点击XML会有如下的信息在里面:
对应的我们需要更新我们修改的XML文件:
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("修改成功:" + color);
}
}
接着就是实现方块的颜色的修改:
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("读取的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;
}
}
}
我们要实现如效果图中的点击按钮就会修改对应的方块,我们需要创建我们的按钮:
void OnGUI(){
//create modify color buttons
GUI.Label(new Rect(10,10,200,30),"修改配置文件");
if(GUI.Button(new Rect(10,40,100,30),"红色")){
UpdateXML(0xff0000);
}
if(GUI.Button(new Rect(10,70,100,30),"绿色")){
UpdateXML(0x00ff00);
}
//create button use to modify Cube'color
GUI.Label(new Rect(10,110,200,30),"根据配置文件修改方块");
if(GUI.Button(new Rect(10,140,200,30),"修改方块")){
ModifyCubeColor();
}
}
- Unity3D 使用XML进行简单的配置文件修改
- 使用Jdom进行简单的XML解析
- 使用Tinyxml2进行简单的xml操作
- unity3d读取XML配置文件
- 使用PHP对配置文件进行修改
- 使用PHP对配置文件进行修改
- 使用PHP对配置文件进行修改
- Spring_使用XML配置文件进行读取
- XML使用Pull进行解析的简单Dome
- XML使用Pull进行解析的简单Dome
- 修改Struts2的struts.xml配置文件位置
- 修改Struts2的struts.xml配置文件位置
- 修改Struts2的struts.xml配置文件位置
- 修改Struts2的struts.xml配置文件位置
- XML配置文件的读取与修改
- 修改Struts2的struts.xml配置文件位置
- XML配置文件的读取与修改
- 在Unity3D中使用C#如何进行XML文件的读写
- [视频讲解]Java(JDK的下载安装及第一个程序运行)
- 缓冲区的比较
- Apache下htaccess文件不起作用/rewrite 没有效果
- 不用if比较两个数大小
- 关于在cocos2d中展示文字
- Unity3D 使用XML进行简单的配置文件修改
- Rotate Image、Pascal's Triangle vector用法
- Unity3D传递ProtoBuf-net消息测试
- Dom获取元素的几种方式
- 循环队列 链式队列 的jJAVA实现
- 学习swift资源汇总
- 利用原生JavaScript获取样式的方式小结
- 数据结构与算法分析的C++描述结构学习(2)
- POJ 3580——SuperMemo(Splay树,经典题)