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

文件目录为:Assets/Scripts/
GameConfig.cs
JXML主要就是在这里使用
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如图:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值