构建数据表基类
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 () {
}
}