1. 程式人生 > >Unity3d txt配置表讀取

Unity3d txt配置表讀取

父類:

//方法可擴充套件
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()方法進行數劇解析。

配置表:
這裡寫圖片描述