| 
									
										
										
										
											2022-07-04 11:17:39 +00:00
										 |  |  |  | using System.Data; | 
					
						
							|  |  |  |  | using System.IO; | 
					
						
							|  |  |  |  | using System.Collections.Generic; | 
					
						
							|  |  |  |  | using UnityEngine; | 
					
						
							| 
									
										
										
										
											2022-08-31 11:45:55 +00:00
										 |  |  |  | #if UNITY_EDITOR | 
					
						
							|  |  |  |  | using Excel; | 
					
						
							|  |  |  |  | #endif | 
					
						
							| 
									
										
										
										
											2022-07-04 11:17:39 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace MMO | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-09 14:35:49 +00:00
										 |  |  |  |     public abstract class MMOTableBase<T, U, V> : ScriptableObject where T : ScriptableObject where U : MMODataBase | 
					
						
							| 
									
										
										
										
											2022-07-04 11:17:39 +00:00
										 |  |  |  |     { | 
					
						
							|  |  |  |  |         private static T mInstance; | 
					
						
							|  |  |  |  |         public static T Instance | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             get | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 if (mInstance == null) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     mInstance = Resources.Load<T>("MMOTable/" + typeof(T).Name); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                 return mInstance; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-09 14:35:49 +00:00
										 |  |  |  |         public int Count => mDataList.Count; | 
					
						
							|  |  |  |  |         [SerializeField] protected List<U> mDataList; | 
					
						
							| 
									
										
										
										
											2022-07-04 11:17:39 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-09 14:35:49 +00:00
										 |  |  |  |         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); | 
					
						
							| 
									
										
										
										
											2022-07-04 11:17:39 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         public void ParseExcel(string pFilePath) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             int tColNum = 0, tRowNum = 0; | 
					
						
							|  |  |  |  |             Debug.Log(pFilePath); | 
					
						
							|  |  |  |  |             DataRowCollection tCollection = ReadExcelContext(pFilePath, ref tColNum, ref tRowNum); | 
					
						
							| 
									
										
										
										
											2022-09-09 14:35:49 +00:00
										 |  |  |  |             Debug.Log(typeof(T).Name + ": 列数_" + tColNum + " ,行数_" + tRowNum); | 
					
						
							| 
									
										
										
										
											2022-07-04 11:17:39 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |             // 第一行变量名,循环列 | 
					
						
							|  |  |  |  |             //for (int i = 0; i < tColNum; i++) | 
					
						
							|  |  |  |  |             //{ | 
					
						
							|  |  |  |  |             //} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             //第二行开始才是数据,循环行 | 
					
						
							|  |  |  |  |             mDataList = new List<U>(tRowNum - 1); | 
					
						
							|  |  |  |  |             for (int i = 1; i < tRowNum; i++) | 
					
						
							|  |  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-09-09 14:35:49 +00:00
										 |  |  |  |                 U tData = typeof(U).Assembly.CreateInstance(typeof(U).FullName) as U; | 
					
						
							|  |  |  |  |                 tData.ParseData(tCollection[i]); | 
					
						
							|  |  |  |  |                 mDataList.Add(tData); | 
					
						
							| 
									
										
										
										
											2022-07-04 11:17:39 +00:00
										 |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         /// <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) | 
					
						
							|  |  |  |  |         { | 
					
						
							| 
									
										
										
										
											2022-08-31 11:45:55 +00:00
										 |  |  |  | #if UNITY_EDITOR | 
					
						
							| 
									
										
										
										
											2022-07-04 11:17:39 +00:00
										 |  |  |  |             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; | 
					
						
							| 
									
										
										
										
											2022-08-31 11:45:55 +00:00
										 |  |  |  | #else | 
					
						
							| 
									
										
										
										
											2022-09-09 14:35:49 +00:00
										 |  |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2022-08-31 11:45:55 +00:00
										 |  |  |  | #endif | 
					
						
							| 
									
										
										
										
											2022-07-04 11:17:39 +00:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-09 14:35:49 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     public abstract class MMODataBase | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         public abstract void ParseData(DataRow pCollection); | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-04 11:17:39 +00:00
										 |  |  |  | } |