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