Common(十三)—— GameConfig和JXML使用方法

来源:互联网 发布:北南安知我意 编辑:程序博客网 时间:2024/05/15 15:28
文件目录为:Assets/Scripts/GameConfig.csJXML主要就是在这里使用
using System;using System.IO;using System.Xml;using UnityEngine;using System.Collections;using System.Collections.Generic;using Object = UnityEngine.Object;//游戏配置类public class GameConfig : Singleton<GameConfig>{    //配置xml信息    private JXml xml = null;    //游戏配置文件路径    private string mGameConfigPath = Application.persistentDataPath + "/Config/xml";    public void Init()    {        xml = new JXml ("<Config></Config>");        FileInfo t = new FileInfo (mGameConfigPath);        //如果此文件不存在则创建        if (!t.Exists)        {            //设置特效等级            WriteProfileString("Effect", "LodLevel", "High");            xml.SaveToFile (mGameConfigPath);            Debug.Log ("GameConfig not exited");        }        else        {            //如果此文件存在则打开            xml.LoadFromFile (mGameConfigPath);            Debug.Log ("Load GameConfig not exsited");        }        //初始配置信息        string effectLevel = LoadProfileString ("Effect", "LodLevel");        Debug.Log ("effectLevel:" + effectLevel);        //特效高效果        if (effectLevel == "High")        {            //BlGame.Effect.EffectManager.Instance.SetEffectLodLevel(Effect.EffectLodLevel.High);        }        else        {            // BlGame.Effect.EffectManager.Instance.SetEffectLodLevel(Effect.EffectLodLevel.Low);        }    }    //保存配置信息    public void Save()    {        xml.SaveToFile (mGameConfigPath);    }    //读取配置信息    public string LoadProfileString(string section, string item)    {        return xml [section].Attributes [item];    }    //写入配置合集信息    public void WriteProfileString(string section, string item, string value)    {        xml [section].Attributes [item] = value;    }    //调试配置信息    public void Dump()    {        Debug.Log ("<Effect>");        Debug.Log ("     LodLevel:" + xml["Effect"].Attributes["LodLevel"]);        Debug.Log ("</Effect>");    }}

然后来看看怎么用:
这是一个很简单的用法

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;using System.IO;public class TestJXML : MonoBehaviour{    [MenuItem("JXML/CreateJXML")]    public static void Create()    {        string mGameConfigPath = Application.persistentDataPath + "/Config.xml";        Debug.Log (mGameConfigPath);        JXml xml = new JXml ("<Config></Config>");        xml["Effect"].Attributes["LodLevel"] = "High";        xml.SaveToFile (mGameConfigPath);    }    [MenuItem("JXML/ReadJXML")]    public static void Read()    {        string mGameConfigPath = Application.persistentDataPath + "/Config.xml";        Debug.Log (mGameConfigPath);        JXml xml = new JXml();        xml.LoadFromFile (mGameConfigPath);        string str = xml ["Effect"].Attributes ["LodLevel"];        Debug.Log (str);    }}

创建,读取以后,debug.log如图:
这里写图片描述