unity 使用xml创建外部配置文件


 

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using System.IO;
 5 using System.Xml;
 6 
 7 public class XmlManager 
 8 {
 9     private string Path = Application.streamingAssetsPath + "/Config.xml";
10     public  static   Dictionary<string, string> XmlData=new Dictionary<string, string>();
11     //默认配置
12     private  Dictionary<string, string> XmlDefault = new Dictionary<string, string> {
13         {"是否开启垂直同步,0关闭,1默认65Fps,2默认30FPS","0" },
14         {"显示FPS","0" },
15         {"限制FPS","65" },
16         { "FPS字体大小","30"},
17         { "屏幕宽", "1920 "},
18         { "屏幕高", "1080 "},
19         { "屏幕全屏", "0"},
20         { "窗口化无边框","0"},
21         { "窗口化无边框窗口位置x","0"},
22         { "窗口化无边框窗口位置y","0"},
23         { "鼠标隐藏", "1" },
24 
25 
26 
27     };
28 
29    
30     public XmlManager() {
31         Start();
32     }
33      void Start()
34     {
35         if (!File.Exists(Path)) {
36             EstablishXml(XmlDefault);
37         }
38         ReadXml();
39         
40     }
41     //创建xml
42     void EstablishXml(  Dictionary<string ,string > XmlConfig) {
43         XmlDocument doc = new XmlDocument();
44         //创建XML声明
45         XmlDeclaration dec = doc.CreateXmlDeclaration("1.0","UTF-8","");
46         doc.AppendChild(dec);
47         //创建根节点
48         XmlElement root = doc.CreateElement("root");
49         doc.AppendChild(root);
50         foreach (KeyValuePair<string, string> pair in XmlConfig) {
51 
52             XmlElement Label = doc.CreateElement("label");
53             //设置属性
54             Label.SetAttribute("说明", pair.Key);
55             //设置多条属性
56             //Label.SetAttribute("说明1", pair.Key);
57             //添加内容
58             Label.InnerText = pair.Value.ToString();
59             root.AppendChild(Label);
60         }
61         doc.Save(Path);
62     }
63     //  读取
64     void ReadXml() {
65 
66         XmlDocument xml = new XmlDocument();
67         xml.Load(Path);
68 
69 
70         //XmlNodeList list = xml.SelectNodes("/root/label");
71 
72         //foreach (XmlElement element in list)
73         //{       
74         //   // XmlData.Add(element.GetAttribute("说明"), element.InnerText.Replace(" ", ""));
75         //    // Debug.Log(element.GetAttribute("说明"));
76         //}
77 
78 
79         //得到root节点下的所有子节点
80         XmlNodeList xmlNodeList = xml.SelectSingleNode("root").ChildNodes;
81         //遍历所有子节点
82         foreach (XmlElement element in xmlNodeList)
83         {
84             //  Debug.Log(element.Name);
85             //读取第一个属性名称
86             // Debug.Log(element.Attributes[0].Name);
87             //读取第一属性内容
88             // Debug.Log(element.Attributes[0].Value);
89             //根据名称读取属性内容
90             //Debug.Log(element.GetAttribute("说明"));
91             //读取内容
92             //Debug.Log(element.InnerText);
93             XmlData.Add(element.GetAttribute("说明"), element.InnerText.Replace(" ", ""));
94         }
95         //Debug.Log(xml.OuterXml);
96     }   
97     
98 }

通过创建字典来创建默认的基本配置项,    在固定的文件夹下没有xml时,会创建一个默认的xml配置,  当配置文件出现错误,或者配置内容出错时, 可以删除xml来还原配置,    添加配置项只需要在

XmlDefault 字典里面添加内容即可  ,      
如何应用 在新脚本中  1 void Awake() 2 { 3 XmlManager xml = new XmlManager(); 4 } 

取用字典中的配置项
 void Start()
    {
       int  _posX = int.Parse(XmlManager.XmlData["窗口化无边框窗口位置x"]);
      int  _posY = int.Parse(XmlManager.XmlData["窗口化无边框窗口位置y"]);
       int _Txtwith = int.Parse(XmlManager.XmlData["屏幕宽"]);
      int   _Txtheight = int.Parse(XmlManager.XmlData["屏幕高"]);


        bool mouse = XmlManager.XmlData["鼠标隐藏"] == "0" ? false : true;
}

 

 当出现字典查询错误时, 可能室脚本执行顺序的问题 , 需要把带有

XmlManager xml = new XmlManager(); 

方法的脚本最先执行          unity调整脚本执行顺序如图   

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM