97 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			97 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System.Data;
 | ||
| using System.IO;
 | ||
| using System.Collections.Generic;
 | ||
| using UnityEngine;
 | ||
| #if UNITY_EDITOR
 | ||
| using Excel;
 | ||
| #endif
 | ||
| 
 | ||
| namespace MMO
 | ||
| {
 | ||
|     public abstract class MMOTableBase<T, U, V> : ScriptableObject where T : ScriptableObject where U : MMODataBase
 | ||
|     {
 | ||
|         private static T mInstance;
 | ||
|         public static T Instance
 | ||
|         {
 | ||
|             get
 | ||
|             {
 | ||
|                 if (mInstance == null)
 | ||
|                 {
 | ||
|                     mInstance = Resources.Load<T>("MMOTable/" + typeof(T).Name);
 | ||
|                 }
 | ||
| 
 | ||
|                 return mInstance;
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         public int Count => mDataList.Count;
 | ||
|         [SerializeField] protected List<U> mDataList;
 | ||
| 
 | ||
|         public U this[int pIndex]
 | ||
|         {
 | ||
|             get
 | ||
|             {
 | ||
|                 int tIndex = Mathf.Clamp(pIndex, 0, mDataList.Count - 1);
 | ||
|                 return mDataList[tIndex];
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         public List<U> GetAll()
 | ||
|         {
 | ||
|             List<U> tAll = new List<U>(mDataList);
 | ||
|             return tAll;
 | ||
|         }
 | ||
| 
 | ||
|         public abstract U GetData(V pID);
 | ||
| 
 | ||
|         public void ParseExcel(string pFilePath)
 | ||
|         {
 | ||
|             int tColNum = 0, tRowNum = 0;
 | ||
|             Debug.Log(pFilePath);
 | ||
|             DataRowCollection tCollection = ReadExcelContext(pFilePath, ref tColNum, ref tRowNum);
 | ||
|             Debug.Log(typeof(T).Name + ": 列数_" + tColNum + " ,行数_" + tRowNum);
 | ||
| 
 | ||
|             // 第一行变量名,循环列
 | ||
|             //for (int i = 0; i < tColNum; i++)
 | ||
|             //{
 | ||
|             //}
 | ||
| 
 | ||
|             //第二行开始才是数据,循环行
 | ||
|             mDataList = new List<U>(tRowNum - 1);
 | ||
|             for (int i = 1; i < tRowNum; i++)
 | ||
|             {
 | ||
|                 U tData = typeof(U).Assembly.CreateInstance(typeof(U).FullName) as U;
 | ||
|                 tData.ParseData(tCollection[i]);
 | ||
|                 mDataList.Add(tData);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 读Excel
 | ||
|         /// </summary>
 | ||
|         /// <param name="pFilePath">文件路径</param>
 | ||
|         /// <param name="pColNum">行数</param>
 | ||
|         /// <param name="pRowNum">列数</param>
 | ||
|         /// <returns></returns>
 | ||
|         private DataRowCollection ReadExcelContext(string pFilePath, ref int pColNum, ref int pRowNum)
 | ||
|         {
 | ||
| #if UNITY_EDITOR
 | ||
|             FileStream stream = File.Open(pFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
 | ||
|             IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
 | ||
| 
 | ||
|             DataSet result = excelReader.AsDataSet();
 | ||
|             // Tables[0] 下标0表示excel文件中第一张表的数据
 | ||
|             pColNum = result.Tables[0].Columns.Count;
 | ||
|             pRowNum = result.Tables[0].Rows.Count;
 | ||
|             return result.Tables[0].Rows;
 | ||
| #else
 | ||
|         return null;
 | ||
| #endif
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
|     public abstract class MMODataBase
 | ||
|     {
 | ||
|         public abstract void ParseData(DataRow pCollection);
 | ||
|     }
 | ||
| } |