Unity中加載Ini文件

    Ini文件通常運用在端遊PC端,在移動端也可以使用Ini文件,由於其配置靈活性,我們在一些配置的時候,也會選用其作爲配置文件,有個問題需要注意一下,就是在Unity中,擴展名ini文件是不識別的,我們需要將其改成擴展名weitxt文件,接下來我們就把源代碼給大家分享一下。

  首先給大家介紹一下Ini配置文件的格式:

[名字]    ----Section字段

key=value  ----前面的是鍵key值,value是其數值,舉例如下:

 

[scene1]

name=guanka

 

那我們如何實現Ini文件的讀取,代碼如何去寫?首先先要加載INi配置文件,將其放到表裏,根據字段將其存放,我們還需要能獲取到Section的字段以及Key值和Value值。

源代碼如下:

 

using System;
using System.Collections;
using UnityEngine;
public class IniFile
{
    private Hashtable ht;
    private string[] textData;
    public IniFile(string fileName)
    {
        string key = string.Empty;
        this.ht = new Hashtable();
        string text = LoadTextFile(fileName, ".txt");
        if (text != null)
        {
            this.textData = text.Split(new char[]
   {
    '\n'
   });
            for (int i = 0; i < this.textData.Length; i++)
            {
                string text2 = this.textData[i].Trim();
                if (!text2.Equals(string.Empty))
                {
                    if (!text2.Substring(0, 2).Equals("//"))
                    {
                        if (text2.StartsWith("[") && text2.EndsWith("]"))
                        {
                            key = text2.Substring(1, text2.Length - 2);
                        }
                        else
                        {
                            string key2 = text2.Substring(0, text2.IndexOf('='));
                            string value = text2.Substring(text2.IndexOf('=') + 1, text2.Length - text2.IndexOf('=') - 1);
                            if (this.ht.ContainsKey(key))
                            {
                                Hashtable hashtable = (Hashtable)this.ht[key];
                                hashtable.Add(key2, value);
                            }
                            else
                            {
                                Hashtable hashtable2 = new Hashtable();
                                hashtable2.Add(key2, value);
                                this.ht.Add(key, hashtable2);
                            }
                        }
                    }
                }
            }
        }
        else
        {
            if (Debug.isDebugBuild)
            {
                Debug.LogError("Inifile:>" + fileName + " object is null!!");
            }
        }
    }

    //讀整數
    public int ReadInt(string section, string ident, int defaultValue)
    {
        int result = defaultValue;
        if (this.ht.ContainsKey(section))
        {
            Hashtable hashtable = (Hashtable)this.ht[section];
            if (hashtable.ContainsKey(ident))
            {
                result = Convert.ToInt32(hashtable[ident]);
            }
        }
        return result;
    }

    //讀字符串
    public string ReadString(string section, string ident, string defaultVal)
    {
        string result = defaultVal;
        if (this.ht.ContainsKey(section))
        {
            Hashtable hashtable = (Hashtable)this.ht[section];
            if (hashtable.ContainsKey(ident))
            {
                result = hashtable[ident].ToString();
            }
        }
        return result;
    }
    public float ReadSingle(string section, string ident, float defaultValue)
    {
        float result = defaultValue;
        if (this.ht.ContainsKey(section))
        {
            Hashtable hashtable = (Hashtable)this.ht[section];
            if (hashtable.ContainsKey(ident))
            {
                result = Convert.ToSingle(hashtable[ident]);
            }
        }
        return result;
    }
    public double ReadDouble(string section, string ident, double defaultValue)
    {
        double result = defaultValue;
        if (this.ht.ContainsKey(section))
        {
            Hashtable hashtable = (Hashtable)this.ht[section];
            if (hashtable.ContainsKey(ident))
            {
                result = Convert.ToDouble(hashtable[ident]);
            }
        }
        return result;
    }
    public void WriteString(string section, string ident, string val)
    {
        if (this.SectionExists(section))
        {
            Hashtable hashtable = (Hashtable)this.ht[section];
            if (hashtable.ContainsKey(ident))
            {
                hashtable[ident] = val;
            }
            else
            {
                hashtable.Add(ident, val);
            }
        }
        else
        {
            Hashtable hashtable2 = new Hashtable();
            this.ht.Add(section, hashtable2);
            hashtable2.Add(ident, val);
        }
    }

   //字段是否存在
    public bool SectionExists(string section)
    {
        return this.ht.ContainsKey(section);
    }

    //獲取字段
    public Hashtable GetSection(string section)
    {
        if (this.SectionExists(section))
        {
            return this.ht[section] as Hashtable;
        }
        return null;
    }

 

   //value值是否存在
    public bool ValueExists(string section, string ident)
    {
        return this.SectionExists(section) && ((Hashtable)this.ht[section]).ContainsKey(ident);
    }

 

   //加載text文件

    public  string LoadTextFile(string path, string ext)
    {
            string text = string.Empty;
            string pathstr = path + ext;

            text = File.ReadAllText(pathstr);

            return text;
    }
}

 

我們在其他腳本里面通過調用IniFile file = new IniFile(filename),即可將其文件加載到內存,然後通過file調用其它接口。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章