引言
現在做游戲開發的沒有幾個不用Excel的,用的最多的就是策划。尤其是數值策划,Excel為用戶提供強大的工具,各種快捷鍵,各種插件,各種函數。但是作為程序來說其實關注的不是Excel而是它最終形成的數據,而在程序中數據其實就是二進制,比如說一個int型就是4個byte,一個字母占2個byte。但是游戲中不可能把excel文件放進去(因為Excel本身就會占一部分額外的空間),也不可能把處理Excel的類庫打包到程序,所以現在大多是對Excel進行讀取然后將數據進行序列化並寫入文件再打包,程序運行的時候直接讀取數據文件在進行反序列化。
這樣做有幾個好處:1.節省了空間。2.把和游戲無關的操作放在了游戲之外。3.游戲開發過程中用起來也比較方便,因為反序列換后的數據就是自己定義的某個類型的一個變量。3.減少了前后端通訊傳輸的數據量。4.方便對數據進行加密。
其實這個過程的核心部分都是一次性的操作,只要實現一遍以后就可以重復使用。
流程
實現的流程大致可以分為三個部分:1.讀取Excel文件。2.將讀取的數據進行序列化並寫入文件。3.反序列化。#4.加密
讀取Excel
本來讀取Excel是一件非常簡單的事情,但是開始做才發現,對於C#來說Excel操作的類庫很多,所以用哪個類庫就成了一個問題,其實這些操作都是在游戲之外進行的類庫的性能可以忽略,但是一般人肯定都喜歡用性能好一點的。
另外一個問題就相對比較重要了,很多Excel類庫都是基於windows開發環境的,而且還需要安裝office,換到其他平台就不支持,而且Unity是換平台游戲引擎,所以基於這幾點,我最后選擇了一個跨平台操作Excel的開源類庫ExcelReader
using UnityEngine; using UnityEditor; using System.Collections; using System; using System.Collections.Generic; using System.IO; using Excel; using System.Data; public class ExportExcel { [MenuItem("Frame/ExportExcel")] public static void ExportExcelToBinary() { Dictionary<string, List<List<Property>>> DataMap = new Dictionary<string, List<List<Property>>>(); List<List<Property>> classes; List<Property> properties; FileInfo info; FileStream stream; IExcelDataReader excelReader; DataSet result; string[] files = Directory.GetFiles(Application.dataPath + "/EasyUI/ExcelFiles", "*.xlsx", SearchOption.TopDirectoryOnly); int row = 0, col = 0; try { foreach (string path in files) { info = new FileInfo(path); stream = info.Open(FileMode.Open, FileAccess.Read); excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); result = excelReader.AsDataSet(); classes = new List<List<Property>>(); int rowCount = result.Tables[0].Rows.Count; int colCount = result.Tables[0].Columns.Count; for (row = 1; row < rowCount; row++) { properties = new List<Property>(); for (col = 0; col < colCount; col++) { //string name = result.Tables[0].Rows[0][j].ToString(); //string value = result.Tables[0].Rows[i][j].ToString(); //string type = result.Tables[1].Rows[1][col].ToString(); //Debug.Log(result.Tables[0].Rows[0][col].ToString()+":"+result.Tables[0].Rows[row][col].ToString()); properties.Add( new Property( result.Tables[0].Rows[0][col].ToString(), result.Tables[0].Rows[row][col].ToString(), result.Tables[1].Rows[1][col].ToString() )); Debug.Log(result.Tables[1].Rows[1][col].ToString()); } classes.Add(properties); } DataMap.Add(info.Name, classes); excelReader.Close(); stream.Close(); } } catch(IndexOutOfRangeException exp) { Debug.LogError("數組下標超出范圍!"); } } }
先把數據讀到這里面Dictionary<string, List<List<Property>>> DataMap;后面會講怎么利用meta data來做映射,怎么序列化。
還有就是Excel文件是有格式要求的哦,文件名就是類名,第一個sheet是數據從第二行開始讀。第二個sheet的第一行是字段名稱,第二行是字段類型,第三行的第一列是預留的類名。如下圖
還有一個Property類是為了以后映射和序列化使用的

using System; public class Property { public string name; public string value; public string type; public Property(string name,string value,string type) { this.name = name; this.value = value; this.type = type; } public Type GetType() { return null; //todo.. } }
看了很多帖子,比較有用的先給大家貼出來。http://www.mamicode.com/info-detail-494944.html , http://www.xuanyusong.com/archives/2429/ , http://www.cnblogs.com/shanyou/archive/2009/11/21/1607548.html
,加密:http://wenku.baidu.com/view/031ede00964bcf84b9d57b6e.html
本文固定鏈接:http://www.cnblogs.com/fly-100/p/4538975.html