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调用其它接口。