txt文件解析简易框架

构建数据表基类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class BaseTable  {

    protected string fileDirPath;

    protected string fileName;
    protected Dictionary<int,BaseData> dataDic = new Dictionary<int, BaseData> ();

    public string FileName {
        get {
            return fileName;
        }
        set {
            fileName = value;
        }
    }



    protected virtual  void Init(){
        fileDirPath = Application.streamingAssetsPath+"/";
    }

    /// <summary>
    /// 加载数据文件
    /// </summary>
    public virtual void LoadData(){
        string filePath = string.Format (fileDirPath+"{0}.txt",FileName);

        using(FileStream fs = new FileStream (filePath,FileMode.Open)){
            StreamReader sr = new StreamReader (fs);
            string buff=sr.ReadLine ();
            buff=sr.ReadLine ();
            while (buff!=null) {
                SaveData (buff);
                buff=sr.ReadLine ();
            }
        }


    }

    /// <summary>
    /// 按行保存数据
    /// </summary>
    /// <param name="data">Data.</param>
    protected virtual void SaveData(string data){
        if (string.IsNullOrEmpty (data))
            return;
    }

    //获取数值
    protected virtual T GetData<T>(int key) where T:BaseData{
        if (dataDic.ContainsKey (key)) {
            return dataDic [key] as T;
        }
        return null;
    }



}

构建数据基类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseData {
    protected string[] datas;
    public void Init(string data){
        LoadData (data);
    }
    protected virtual void LoadData(string data){
        if (string.IsNullOrEmpty (data.Trim()))
            return;
        datas = data.Split (' ');
    }
}

构建数据表管理器

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ConfigManage  {

    private static ConfigManage instance;
    Dictionary<string,BaseTable> tableDic=new Dictionary<string, BaseTable>();

    public static ConfigManage Instance {
        get {
            if(instance==null){
                instance = new ConfigManage ();
            }
            return instance;
        }
    }

    public void Init(){
        RegisterTable<HeroTable> ();
        RegisterTable<InventoryTable> ();
    }


    void RegisterTable<T>()where T:BaseTable,new(){
        T value = new T ();
        string key = value.GetType ().FullName;
        Debug.Log ("key="+key);
        if (tableDic.ContainsKey (key)) {
            tableDic [key] = value;
        } else {
            tableDic.Add (key,value);
        }

    }

    //获取数据表
    public T GetTable<T>()where T:BaseTable{
        string key = typeof(T).FullName;
        if(tableDic.ContainsKey(key)){
            return tableDic [key] as T;
        }
        return null;
    }


    //获取具体的数据类
//  public T GetTabelData<T>(int id)where T:BaseData {
//      string key = typeof(T).FullName;
//      if(tableDic.ContainsKey(key)) {
//          return  tableDic [key].GetData<T> (id);
//      }
//      return null;
//  }



}

构建子数据表
HeroTable.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeroTable : BaseTable {
    public class HeroData : BaseData{
        public int id;
        public int type;
        public string icon;
        public string name;


        protected override void LoadData (string data)
        {
            base.LoadData (data);
            if(datas!=null){
                Debug.Log (data+"--"+datas.Length);
                id = int.Parse(datas [0]);
                type = int.Parse (datas [1]);
                icon = datas[2];
                name = datas [3];
            }

        }
    }

    public HeroTable(){
        Init ();
    }

    /// <summary>
    /// 初始化
    /// </summary>
    protected override void Init ()
    {
        base.Init ();
        FileName = "hero";
        LoadData ();
    }



    protected override void SaveData(string data){
        base.SaveData (data);
        HeroData hero = new HeroData ();
        hero.Init (data);
        dataDic.Add (hero.id, hero);
    }

    public HeroData GetDataById(int id){
        return  GetData<HeroData> (id);
    }



}

InventoryTable.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryTable : BaseTable {

    public class InventoryData:BaseData{
        public int id;
        public int type ;
        public string name ;
        public int price ;
        public int demage ;
        public int limit;

        protected override void LoadData (string data)
        {
            base.LoadData (data);
            id = int.Parse (datas [0]);
            type = int.Parse (datas [1]);
            name = datas [2];
            price = int.Parse (datas [3]);
            demage = int.Parse (datas [4]);
            limit =  int.Parse (datas [5]);
        }
    }


    public InventoryTable(){
        Init ();
    }

    /// <summary>
    /// 初始化
    /// </summary>
    protected override void Init ()
    {
        base.Init ();
        FileName = "inventory";
        LoadData ();
    }

    protected override void SaveData(string data){
        base.SaveData (data);
        InventoryData inventory = new InventoryData ();
        inventory.Init (data);
        dataDic.Add (inventory.id, inventory);
    }

    public InventoryData GetDataById(int id){
        return  GetData<InventoryData> (id);
    }
}

配置文件示例
hero.txt

id type icon name
1001 1 img_01 aaa
1002 2 img_02 bbb
1003 3 img_03 ccc
1004 4 img_04 ddd

inventory.txt

id type name price demage limit
1001 1 wuqi1 100 1000 10
1002 2 wuqi2 200 1000 10
1003 3 wuqi3 300 1000 20
1004 4 wuqi4 400 1000 20

测试脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
        ConfigManage.Instance.Init ();
        HeroTable.HeroData data = ConfigManage.Instance.GetTable<HeroTable> ().GetDataById(1001);
        InventoryTable.InventoryData idata = ConfigManage.Instance.GetTable<InventoryTable> ().GetDataById(1001);
        Debug.Log (data.name);
        Debug.Log (idata.name);
    }

    // Update is called once per frame
    void Update () {

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天涯过客TYGK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值