父类:
//方法可扩展
public abstract class TxtConfig<T> where T: TxtConfig<T>,new() {
private static Dictionary<string, T> dataDic = new Dictionary<string, T>();
static TxtConfig()
{
ParseTable();
}
/// <summary>
/// 解析数据表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tableName"></param>
public static void ParseTable(System.Action onFinsh = null)
{
TextAsset ta = Resources.Load<TextAsset>("Config/" + typeof(T).Name);
if (ta == null) return;
byte[] utf8 = System.Text.UnicodeEncoding.UTF8.GetBytes(ta.text);
string configText = System.Text.UnicodeEncoding.UTF8.GetString(utf8);
dataDic.Clear();
configText = configText.Replace("\r", "");
string[] configAry = configText.Split('\n');
for (int i = 2; i < configAry.Length; i++)
{
if (configAry[i].Equals("")) continue;
T data = new T();
data.Parse(configAry[i].Split('\t'));
dataDic.Add(configAry[i].Split('\t')[0], data);
}
if (onFinsh != null)
onFinsh();
}
/// <summary>
/// 解析
/// </summary>
/// <param name="ary"></param>
protected abstract void Parse(string[] ary);
/// <summary>
/// 初始化
/// </summary>
public static void Init() { }
/// <summary>
/// 是否包含
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool IsHave(string key)
{
return dataDic.ContainsKey(key);
}
/// <summary>
/// 是否包含
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsHave(string key, out T value)
{
if (dataDic.ContainsKey(key))
{
value = dataDic[key];
return true;
}
value = null;
return false;
}
/// <summary>
/// 获取
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void TryGet(string key, out T value)
{
if (!IsHave(key,out value))
{
value = null;
}
}
/// <summary>
/// 获取
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static T TryGet(string key)
{
T value = default(T);
TryGet(key,out value);
return value;
}
/// <summary>
/// 数量
/// </summary>
/// <returns></returns>
public static int Count()
{
return dataDic.Count;
}
/// <summary>
/// 获取
/// </summary>
/// <returns></returns>
public static List<T> TryGet()
{
return new List<T>(dataDic.Values);
}
/// <summary>
/// 获取
/// </summary>
/// <param name="dataList"></param>
public static void TryGet(out List<T> dataList)
{
dataList = TryGet();
}
}
子类:
//需要继承TxtConfig,并且实现Parse解析方法
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class GeneralTable : TxtConfig<GeneralTable>
{
///
// 键
///
public string GeneralKey{get;private set;}
///
// 值
///
public string GeneralValue{get;private set;}
///
// 描述
///
public string GeneralDes{get;private set;}
protected override void Parse(string[] ary)
{
GeneralKey = ary[0];
GeneralValue = ary[1];
GeneralDes = ary[2];
}
}
调用:
public class Test: MonoBehaviour {
void Start()
{
GeneralTable.TryGet("MainActor");
}
}
因为static TxtConfig()
方法在会在第一次调用静态方法的时候调用一次,所以调用ParseTable()
方法进行数剧解析。
配置表: