zoukankan      html  css  js  c++  java
  • unity class to xml

    编写:solq
    blog:http://www.cnblogs.com/solq

    资源放在 Assets\StreamingAssets\ 目录


    SXMLTool.cs

    /*编写:solq
    blog:www.cnblogs.com/solq
    */
    using UnityEngine;
    using System.Xml.Serialization;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Collections.Generic;
    public class SXMLTool
    {
    
        public static string getApplicationDataPaths
        {
            get { 
                switch(Application.platform)
                {
                    case RuntimePlatform.Android:
                        return Application.persistentDataPath+"/assets/";
                    case RuntimePlatform.IPhonePlayer:
                        return Application.dataPath + "/Raw/";
                    default:
                        return Application.dataPath + "/StreamingAssets/";
                }
            }
        }
        public static void Path(ref string path)
        {
            path = getApplicationDataPaths + path;
        }
        protected static Dictionary<string, XmlDocument> cacheXML = new Dictionary<string, XmlDocument>(); 
        public static XmlDocument LoadXmlDocument(string path) //加载 xml
        {
            Path(ref path);
            if (cacheXML.ContainsKey(path)) return cacheXML[path];
    
            XmlDocument xmlDoc=new XmlDocument();
            cacheXML.Add(path, xmlDoc);
            xmlDoc.Load(path);
            return xmlDoc;
        }
        public static void ObjectToXml(object obj, string path) //类保存xml
        {
            Path(ref path);
            XmlSerializer ser = new XmlSerializer(obj.GetType());
            FileStream fs = File.Open(
                    path,
                    FileMode.Create, //每次都是新建
                    FileAccess.Write,
                    FileShare.ReadWrite);
    
            StreamWriter streamWriter = new StreamWriter(fs, System.Text.Encoding.UTF8); //格式化。。。。
            ser.Serialize(streamWriter, obj);
            streamWriter.Close();
            fs.Close();
        }
       
        public static T XmlToObject<T>(string path) //加载 xml 文件,,然后解析成相应类
        {
            Path(ref path);
            byte[] bytes = Encoding.UTF8.GetBytes(File.ReadAllText(path).Trim().ToString());
            MemoryStream mem = new MemoryStream(bytes);
            XmlSerializer ser = new XmlSerializer(typeof(T));
    
            return (T)ser.Deserialize(mem);
        }   
    
    }
    

    RoleConfig.cs

    using System.Collections.Generic;
    using System.Xml.Serialization;
    public class RoleConfig
    {
        public string name;//用户名
        public string sex;//性别
    
        public List<Skill> skill1 = new List<Skill>();//武器1的技能
        public List<Skill> skill2 = new List<Skill>();//武器2的技能
        public List<Skill> skill3 = new List<Skill>();//武器3的技能
    
        [XmlElement("ItemObj")]
        public List<ItemObj> item = new List<ItemObj>();//携带道具
    
        [XmlElement("dayTask")]
        public dayTask dayTask = new dayTask();
    
        public int loginCount = 0;//连续登陆的计数
    
        //******************仓库****************
        public List<Equip> equips = new List<Equip>();//装备
        public List<Amulet> amulets = new List<Amulet>();//护符
        public List<Weapon> weapons = new List<Weapon>(); //武器
        public List<Skill> skills = new List<Skill>(); //技能
        public List<ItemObj> items = new List<ItemObj>();//道具
        public List<Piece> pieces = new List<Piece>();//装备碎片
    
    
        public void save(string path)
        {
            SXMLTool.ObjectToXml(this, path);
        }
    }
    
    //扩展
    public class Equip
    {
        [XmlText]
        public string Name;
    
        [XmlAttribute("index")]
        public int index;
    }
    
    public class Amulet
    {
        [XmlText]
        public string Name;
    
        [XmlAttribute("index")]
        public int index;
    }
    
    public class Weapon
    {
        [XmlText]
        public string Name;
    
        [XmlAttribute("index")]
        public int index;
    }
    
    public class Skill
    {
        [XmlText]
        public string Name;
    
        [XmlAttribute("index")]
        public int index;
    }
    
    public class ItemObj
    {
        [XmlElement("Filter")]
        public string Name;
    
        [XmlAttribute("index")]
        public int index;
    
        [XmlAttribute("Num")]
        public int Num;
    }
    
    public class Piece
    {
        [XmlText]
        public string Name;
    
        [XmlAttribute("index")]
        public int index;
    }
    
    public class dayTask
    {
        [XmlElement("Filter")]
        //[XmlText]
        public string enemyID;
    
        [XmlAttribute("index")]
        public int index;
    
        [XmlAttribute("enemyNum")]
        public string enemyNum;
    
    }
    

    test.cs

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class TestConfig : MonoBehaviour {
    
    	void Start () {
     
            //保存
            RoleConfig cfg = new RoleConfig();
            cfg.name = "hello xxfrreee";
    
            Weapon w1 = new Weapon();
    
            w1.index = 1;
            w1.Name = "jn-10225";
            cfg.weapon.Add(w1);
    
            SXMLTool.ObjectToXml(cfg, "test2.xml");
    
    
            //读取
            RoleConfig test = SXMLTool.XmlToObject<RoleConfig>("test2.xml");
    
            print("name:" + test.name);
            print("  :" + test.weapon.Count);
    	}
    
    }
    

      

  • 相关阅读:
    pthread线程内存布局
    用户空间实现线程 内核实现线程 线程的调度
    堆 虚拟内存
    Operating System-Thread(3)用户空间和内核空间实现线程
    Linux进程地址空间与虚拟内存
    虚拟地址
    物理内存,虚拟内存,进程地址空间
    CPU中MMU的作用
    进程地址空间与虚拟存储空间的理解
    虚拟地址空间
  • 原文地址:https://www.cnblogs.com/solq/p/2547308.html
Copyright © 2011-2022 走看看