自定义配置文件的使用及加载-txt

[Game]
Version=1

[Login]
Account = 阿斗阿斯顿撒
Password =我去饿我去恶趣味
Success = 成哥
Faild = 失败

[Job]
Job1 = 战士
Job2 = 法师
Description1="  打算大苏打实打实的武器恶趣味请问
Description2="  相册任务认为仍然显得十分的是
weapon1= 砍刀
weapon2= 发张

[Money]
Gold = 1234
Diamond = 11.2

[Scene]
LoadFaild = 加载失败

  txt信息

工具类

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;

public static class Configuration {

    //配置信息得数据结构--字典
    private static Dictionary<string,Dictionary<string,string>> dicConfig = new Dictionary<string, Dictionary<string, string>>();
    private static WWW www;
    //是否加载结束
    public static bool IsDone
    {
        //检测是否加载结束
        get
        {
            if (www != null && www.isDone && string.IsNullOrEmpty(www.error))
            {
                BuildDictionary(www.text);
                return true;
            }
            return false;
        }
    }
    /// <summary>
    /// 加载配置信息
    /// </summary>
    public static void LoadConfig()
    {
        string config = "file://" + Application.streamingAssetsPath + "/config.txt";
        if (www == null) { 
#if UNITY_ANDROID
        config="jar:"+config;
#endif
        www = new WWW(config);
    }
   }
    //对外提供提取配置项得接口
    public static string GetString(string mainKey, string subKey)
    {
        return Get(mainKey, subKey);
    }
    public static int GetInt(string mainKey, string subKey)
    {
        string value= Get(mainKey, subKey);
        return value!= null ? int.Parse(value) : 0;
    }
    public static float GetFloat(string mainKey, string subKey)
    {
        string value = Get(mainKey, subKey);
        return value != null ? float.Parse(value) : 0;
    }
    private static string Get(string mainKey, string subKey)
    {
        if(dicConfig.ContainsKey(mainKey) && dicConfig[mainKey].ContainsKey(subKey))
        {
            return dicConfig[mainKey][subKey];
        }
        return null;
    }

    //将配置信息在内存中形成数据结构
    private static void BuildDictionary(string configText)
    {
        //逐行读取配置信息
        StringReader reader = new StringReader(configText);
        string line = null;
        string mainKey = null;
        string subKey = null;
        string subValue = null;

        while((line=reader.ReadLine())!=null)
        {
           line=  line.Trim();
            if (!string.IsNullOrEmpty(line))
            {
                //检测是否是主键
                if(line.StartsWith("[")) //如果是主键
                {
                    //取得[]中的文本内容
                    mainKey=line.Substring(1, line.IndexOf("]") - 1);
                    dicConfig.Add(mainKey,new Dictionary<string, string>());
                }
                else //子键
                {
                    var configKeyValue=    line.Split(new char[] {'='},StringSplitOptions.RemoveEmptyEntries);
                    subKey= configKeyValue[0].Trim();
                    subValue = configKeyValue[1].Trim();
                    subValue=   subValue.StartsWith("\"")? subValue.Substring(1):subValue;
                    dicConfig[mainKey].Add(subKey, subValue);
                }
            }
        }
    }
}

测试类

using UnityEngine;
using System.Collections;

public class LoadConfigTest : MonoBehaviour {

    // Use this for initialization
    IEnumerator Start () {

        Configuration.LoadConfig();
        while (!Configuration.IsDone)
            yield return null;

        print(Configuration.GetString("Job", "Job1"));
        print(Configuration.GetString("Job","Job2"));

        print(Configuration.GetInt("Money", "Gold"));
        print(Configuration.GetFloat("Money", "Diamond"));
    }
    

}

 

posted @ 2017-01-16 13:08  RONGWEIJUN  阅读(655)  评论(0编辑  收藏  举报