Unity IniFile 保存游戏数据到本地文件

来源:互联网 发布:剑灵辅助软件 编辑:程序博客网 时间:2024/06/05 11:12


保存文件的地址FilePath.cs

using System.Collections;using UnityEngine;//保存所有文件路径的类public class FilePath {    //用户登录信息的本地文件夹    public static string Local_User_Login_Info = "/userLoginInfo.ini";    //游戏版本号文本    public static string Local_Game_Version_No = "/gameVersionNo.ini";    //role database文件地址    public static string Local_Role_DataBase = "/role_database.ini";}


方法类IniFile.cs

using UnityEngine;using System.Collections.Generic;using System.IO;public class IniFile{    string fileFullName;    class Pair    {        public string name;        public string value;        public string comment;        public Pair(string n, string v, string c)        {            this.name = n;            this.value = v;            this.comment = c;        }    }    class Section    {        public string name;        public string comment;        public Dictionary<string, Pair> pairs;        public Section(string n, string c)        {            this.name = n;            this.comment = c;            pairs=new Dictionary<string, Pair>();        }    }    Section current_section;    Dictionary<string, Section> data;    public IniFile()    {        Reset();    }    public void Reset()    {        current_section = null;        data = new Dictionary<string, Section>();    }        public bool Load_From_Mobile(string relatefileName)    {        Debug.Log("[IniFile] Load: " + Application.persistentDataPath + relatefileName);        return Load_File(Application.persistentDataPath + relatefileName);    }    public bool Load_File(string fileName)    {        fileFullName = fileName;        if (!System.IO.File.Exists(fileName))        {            return false;        }        StreamReader sr = new StreamReader(fileFullName);        while (!sr.EndOfStream)        {            string line = sr.ReadLine();            line = line.Trim();            if (line.Length == 0)                continue;            if (Is_Load_Comment(line))                continue;            if (Is_Load_A_Section(line))            {                Load_A_Section(line);            }            else            {                Load_A_Pair(line);            }        }        sr.Close();        current_section = null;        return true;    }    private bool Is_Load_Comment(string rawLine)    {        if (rawLine[0] == ';')        {            return true;        }        return false;    }    private bool Is_Load_A_Section(string rawLine)    {        if (rawLine[0] == '[')        {            return true;        }        return false;    }    private void Load_A_Section(string rawLine)    {        int starSection = rawLine.IndexOf('[');        int endSection = rawLine.LastIndexOf(']');        string name = rawLine.Substring((starSection + 1), (endSection - starSection - 1));        string comment = "";        if (rawLine.Split(';').Length >= 2)        {            comment = rawLine.Split(';')[1];        }        name = name.Trim();        comment = comment.Trim();        current_section = new Section(name, comment);        data.Add(name, current_section);    }    private void Load_A_Pair(string rawLine)    {        string name = rawLine.Split('=')[0];        string value = rawLine.Split('=')[1].Split(';')[0];        string comment = "";        if (rawLine.Split('=')[1].Split(';').Length >= 2)        {            comment = rawLine.Split('=')[1].Split(';')[1];        }        name = name.Trim();        value = value.Trim();        comment = comment.Trim();        Pair p = new Pair(name, value, comment);        current_section.pairs.Add(name, p);    }    public bool Is_Section(string section)    {        return data.ContainsKey(section);    }    public bool Goto_Section(string section)    {        if (Is_Section(section))        {            current_section = data[section];            return true;        }        return false;    }    public bool Create_Section(string section, string comment = "")    {        if (Is_Section(section))        {            current_section = data[section];            current_section.comment = comment;            return false;        }        current_section = new Section(section, comment);        data.Add(section, current_section);        return true;    }    public bool Is_Name(string name)    {        if (current_section != null)        {            return current_section.pairs.ContainsKey(name);        }        return false;    }    public string Get_Section_Comment()    {        if (current_section != null)        {            return current_section.comment;        }        return "";    }    public string Get_Comment(string name, string defaultValue = "")    {        if (current_section != null)        {            if (Is_Name(name))            {                return current_section.pairs[name].comment;            }        }        return defaultValue;    }    public string Get_String(string name, string defaultValue = "")    {        if (current_section != null)        {            if (Is_Name(name))            {                return current_section.pairs[name].value;            }        }        return defaultValue;    }    public string[] Get_Strings(string name, string defaultValue = "")    {        string strConn = Get_String(name, defaultValue);        string[] subs = strConn.Split(',');        for (int i = 0; i < subs.Length; i++)        {            subs[i] = subs[i].Trim();        }        return subs;    }    public bool Get_Bool(string name, bool defaultValue = false)    {        return bool.Parse(Get_String(name, defaultValue.ToString()));    }    public int Get_Intl(string name, int defaultValue = 0)    {        return int.Parse(Get_String(name, defaultValue + ""));    }    public float Get_Float(string name, float defaultValue = 0f)    {        return float.Parse(Get_String(name, defaultValue + ""));    }    //TODO GetColor      //public Color Get_Color(string name, Color defaultValue)      //{      //    Color c = defaultValue;      //    Color.TryParseHexString(Get_String(name, c.ToHexStringRGBA()), out c);      //    return c;      //}      public Vector2 Get_Vector3(string name, Vector2 defaultValue)    {        Vector2 v = defaultValue;        string s = Get_String(name, "");        if (s == "")        {            return v;        }        string[] ss = s.Split(',');        v.x = float.Parse(ss[0]);        v.y = float.Parse(ss[1]);        return v;    }    public Vector3 Get_Vector3(string name, Vector3 defaultValue)    {        Vector3 v = defaultValue;        string s = Get_String(name, "");        if (s == "")        {            return v;        }        string[] ss = s.Split(',');        v.x = float.Parse(ss[0]);        v.y = float.Parse(ss[1]);        v.z = float.Parse(ss[2]);        return v;    }    public Quaternion Get_Quaternion(string name, Quaternion defaultValue)    {        Quaternion q = defaultValue;        string s = Get_String(name, "");        if (s == "")        {            return q;        }        string[] ss = s.Split(',');        q.x = float.Parse(ss[0]);        q.y = float.Parse(ss[1]);        q.z = float.Parse(ss[2]);        q.w = float.Parse(ss[3]);        return q;    }    public float[] Get_FloatArray(string name, float[] defaultValue)    {        string s = Get_String(name, "");        if (s == "")        {            return defaultValue;        }        string[] ss = s.Split(',');        float[] f = new float[ss.Length];        for (int i = 0; i < f.Length; i++)        {            f[i] = float.Parse(ss[i]);        }        return f;    }    public void Set_FloatArray(string name, float[] f, string comment = "")    {        string s = "";        for (int i = 0; i < f.Length - 1; i++)        {            s += (f[i] + ",");        }        if (f.Length > 0)        {            s += (f[f.Length - 1] + "");        }        Set_String(name, s, comment);    }    public void Set_String(string name, string value, string comment = "")    {        if (current_section != null)        {            if (Is_Name(name))            {                current_section.pairs[name].value = value;                if (comment != "")                {                    current_section.pairs[name].comment = comment;                }            }            else            {                current_section.pairs.Add(name, new Pair(name, value, comment));            }        }    }    public void Set_Bool(string name, bool value, string comment = "")    {        Set_String(name, value.ToString(), comment);    }    public void Set_Int(string name, int value, string comment = "")    {        Set_String(name, value + "", comment);    }    public void Set_Float(string name, float value, string comment = "")    {        Set_String(name, value + "", comment);    }    public void Set_Vector2(string name, Vector2 v, string comment = "")    {        string s = "" + v.x + "," + v.y;        Set_String(name, s, comment);    }    public void Set_Vector3(string name, Vector3 v, string comment = "")    {        string s = "" + v.x + "," + v.y + "," + v.z;        Set_String(name, s, comment);    }    public void Set_Quaternion(string name, Quaternion q, string comment = "")    {        string s = "" + q.x + "," + q.y + "," + q.z + "," + q.w;        Set_String(name, s, comment);    }    //SetColor      //public void Set_Color(string name, Color value, string comment = "")      //{      //    Set_String(name,value.ToHexStringRGBA(),comment);      //}      public void SaveToMoblie(string relatefileName)    {        Debug.Log("[IniFile] Save: " + Application.persistentDataPath + relatefileName);        SaveTo(Application.persistentDataPath + relatefileName);        if (Application.isEditor)        {            SaveTo(Application.dataPath + relatefileName);        }    }    public void Save()    {        SaveTo(fileFullName);    }    public void SaveTo(string fileName)    {        fileFullName = fileName;        StreamWriter sw = new StreamWriter(fileName, false);        foreach (string key in data.Keys)        {            Section s = data[key];            sw.WriteLine("[" + s.name + "]" + ((s.comment == "") ? "" : (" ; " + s.comment)));            foreach (string name in s.pairs.Keys)            {                Pair p = s.pairs[name];                sw.WriteLine(" " + p.name + " = " + p.value + ((p.comment == "") ? "" : (" ; " + p.comment)));            }//TODO        }        sw.Close();    }    public string[] Get_All_Section()    {        List<string> ret = new List<string>();        foreach (string key in data.Keys)        {            ret.Add(key);        }        return ret.ToArray();    }    public string[] Get_All_Pair_Names()    {        List<string> ret = new List<string>();        foreach (string key in current_section.pairs.Keys)        {            ret.Add(key);        }        return ret.ToArray();    }    public string[] Get_All_Pair_Values()    {        List<string> ret = new List<string>();        foreach (string key in current_section.pairs.Keys)        {            ret.Add(current_section.pairs[key].value);        }        return ret.ToArray();    }}





原创粉丝点击