Last active
July 16, 2016 12:31
-
-
Save yangruihan/5755c32dd3fcb8ac08ce4a1b81fb2129 to your computer and use it in GitHub Desktop.
Unity3D 读取文本文件中的配置信息
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections.Generic; | |
| using System.IO; | |
| using System.Text; | |
| namespace ruihanyang.ConfigHelper | |
| { | |
| /// <summary> | |
| /// <para>配置助手</para> | |
| /// <para>读取文本文件中的配置信息</para> | |
| /// <para>文本内容形如:</para> | |
| /// <para>a:100</para> | |
| /// <para>b:200</para> | |
| /// <para></para> | |
| /// <para>读取信息</para> | |
| /// <code>ConfigHelper.Instance.GetConfig ("a"); // 100</code> | |
| /// <para></para> | |
| /// <para>保存信息</para> | |
| /// <code>ConfigHelper.Instance.AddConfig ("abc", "1000");</code> | |
| /// <para></para> | |
| /// <code>ConfigHepler.Instance.SaveConfig ();</code> | |
| /// <para></para> | |
| /// <para>添加信息后保存</para> | |
| /// <code>ConfigHepler.Instance.SaveAfterAddConfig ("abc", "1000");</code> | |
| /// </summary> | |
| public class ConfigHelper : MonoBehaviour | |
| { | |
| // 文件名 | |
| public string fileName = "config.txt"; | |
| // 游戏对象 | |
| private static GameObject configHelperObj = null; | |
| // 用来存储配置数据的字典 | |
| private Dictionary<string, string> _config; | |
| private Dictionary<string, string> Config | |
| { | |
| get | |
| { | |
| if (_config == null) | |
| { | |
| _config = new Dictionary<string, string>(); | |
| _config = Instance.GetConfig(); | |
| } | |
| return _config; | |
| } | |
| } | |
| // 单例模式 | |
| private static ConfigHelper _instance; | |
| public static ConfigHelper Instance | |
| { | |
| get | |
| { | |
| if (_instance == null) | |
| { | |
| // 创建游戏对象 | |
| if (configHelperObj == null) | |
| { | |
| configHelperObj = new GameObject("ConfigHelper"); | |
| configHelperObj.AddComponent<ConfigHelper>(); | |
| DontDestroyOnLoad(configHelperObj); | |
| } | |
| _instance = GameObject.FindObjectOfType(typeof(ConfigHelper)) as ConfigHelper; | |
| } | |
| return _instance; | |
| } | |
| } | |
| /// <summary> | |
| /// 获取配置信息 | |
| /// </summary> | |
| /// <returns>The config.</returns> | |
| private Dictionary<string, string> GetConfig() | |
| { | |
| Dictionary<string, string> result = new Dictionary<string, string>(); | |
| List<string> lines = ReadLines(fileName); | |
| foreach (var line in lines) | |
| { | |
| string[] dic = line.Split(':'); | |
| if (dic.Length == 1) | |
| { | |
| result.Add(dic[0], ""); | |
| } | |
| else | |
| { | |
| result.Add(dic[0], dic[1]); | |
| } | |
| } | |
| return result; | |
| } | |
| /// <summary> | |
| /// 返回一个文件中的所有行 | |
| /// </summary> | |
| /// <returns>The lines.</returns> | |
| private List<string> ReadLines(string fileName) | |
| { | |
| List<string> lines = new List<string>(); | |
| // 如果文件不存在直接返回 | |
| if (!File.Exists(fileName)) | |
| { | |
| Debug.LogError("Config file not exist!"); | |
| return lines; | |
| } | |
| using (StreamReader sr = new StreamReader(fileName, Encoding.Default)) | |
| { | |
| for (string line = sr.ReadLine(); !string.IsNullOrEmpty(line); line = sr.ReadLine()) | |
| { | |
| lines.Add(line); | |
| } | |
| } | |
| return lines; | |
| } | |
| /// <summary> | |
| /// 将List中的内容写入一个文件 | |
| /// </summary> | |
| /// <param name="lines">Lines.</param> | |
| private void WriteLines(List<string> lines, string fileName) | |
| { | |
| FileStream fs = new FileStream(fileName, FileMode.Create); | |
| string result = ""; | |
| foreach (var line in lines) | |
| { | |
| result += line + "\n"; | |
| } | |
| byte[] data = System.Text.Encoding.Default.GetBytes(result); | |
| fs.Write(data, 0, data.Length); | |
| fs.Flush(); | |
| fs.Close(); | |
| } | |
| /// <summary> | |
| /// 添加配置后保存配置 | |
| /// </summary> | |
| /// <param name="key"></param> | |
| /// <param name="value"></param> | |
| public void SaveAfterAddConfig(string key, string value) | |
| { | |
| AddConfig(key, value); | |
| SaveConfig(); | |
| } | |
| /// <summary> | |
| /// 添加配置后保存配置 | |
| /// </summary> | |
| /// <param name="key"></param> | |
| /// <param name="value"></param> | |
| /// <param name="fileName"></param> | |
| public void SaveAfterAddConfig(string key, string value, string fileName) | |
| { | |
| AddConfig(key, value); | |
| SaveConfig(fileName); | |
| } | |
| /// <summary> | |
| /// 添加配置 | |
| /// </summary> | |
| /// <param name="key">Key.</param> | |
| /// <param name="value">Value.</param> | |
| public void AddConfig(string key, string value) | |
| { | |
| if (Config.ContainsKey(key)) | |
| { | |
| Config[key] = value; | |
| } | |
| else | |
| { | |
| Config.Add(key, value); | |
| } | |
| } | |
| /// <summary> | |
| /// 保存配置 | |
| /// </summary> | |
| public void SaveConfig() | |
| { | |
| SaveConfig(fileName); | |
| } | |
| /// <summary> | |
| /// 保存配置 | |
| /// </summary> | |
| /// <param name="fileName">File name.</param> | |
| public void SaveConfig(string fileName) | |
| { | |
| List<string> lines = new List<string>(); | |
| foreach (var key in Config.Keys) | |
| { | |
| string line = key + ":" + Config[key]; | |
| lines.Add(line); | |
| } | |
| WriteLines(lines, fileName); | |
| } | |
| /// <summary> | |
| /// 获取配置 | |
| /// 如果有该键则返回相应的值,若没有则返回"" | |
| /// </summary> | |
| /// <param name="key"></param> | |
| /// <returns></returns> | |
| public string GetConfig(string key) | |
| { | |
| if (Config.ContainsKey(key)) | |
| { | |
| return Config[key]; | |
| } | |
| else | |
| { | |
| return ""; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment