1.配置文件放在StreamingAssets文件夹下,二进制文件都放在StreamingAssets文件夹下
2.资源加载器
3.每个表对应一个类:类中由属性+方法(加载表方法/根据ID获取表信息方法)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
public class TableLoader : MonoBehaviour {
private static TableLoader instance;
public static TableLoader Instance
{
get
{
if (instance == null)
{
instance = new GameObject("TableLoader").AddComponent<TableLoader>();
}
return instance;
}
}
public void LoadTable(string tableName, Action<string> action)
{
string path = Path.Combine(Application.streamingAssetsPath, tableName);
StartCoroutine(LoadText(path, action));
}
IEnumerator LoadText(string path, Action<string> action)
{
string text = string.Empty;
if (path.Contains("://"))
{
WWW www = new WWW(path);
yield return www;
text = www.text;
}
else
{
if (File.Exists(path))
text = File.ReadAllText(path);
else
{
Debug.LogError("can not find table,path=" + path);
yield break;
}
}
if (action != null)
action(text);
}
}
public class TestInfo
{
public int ID;
public string name;
public static List<TestInfo> TestInfos = new List<TestInfo>();
public static void LoadTestInfo()
{
TableLoader.Instance.LoadTable("TestInfo.txt", (text) =>
{
Debug.LogError(text);
});
}
void HandleLines(string text)
{
text = text.Trim();
StringReader sr = new StringReader(text);
string line = null;
while ((line = sr.ReadLine()) != null)
{
HandleLine(line);
}
}
void HandleLine(string line)
{
TestInfo ti = new TestInfo();
string[] items = line.Split(',');
ti.ID = int.Parse(items[0]);
ti.name = items[1];
TestInfos.Add(ti);
}
public static TestInfo GetTestInfo(int ID)
{
TestInfo ti = null;
foreach (var item in TestInfos)
{
if (item.ID == ID)
{
ti = item;
break;
}
}
if (ti == null)
Debug.LogError("can not find TestInfo,ID=" + ID);
return ti;
}
}