引言html
如今作游戏开发的没有几个不用Excel的,用的最多的就是策划。尤为是数值策划,Excel为用户提供强大的工具,各类快捷键,各类插件,各类函数。可是做为程序来讲其实关注的不是Excel而是它最终造成的数据,而在程序中数据其实就是二进制,好比说一个int型就是4个byte,一个字母占2个byte。可是游戏中不可能把excel文件放进去(由于Excel自己就会占一部分额外的空间),也不可能把处理Excel的类库打包到程序,因此如今大可能是对Excel进行读取而后将数据进行序列化并写入文件再打包,程序运行的时候直接读取数据文件在进行反序列化。git
这样作有几个好处:1.节省了空间。2.把和游戏无关的操做放在了游戏以外。3.游戏开发过程当中用起来也比较方便,由于反序列换后的数据就是本身定义的某个类型的一个变量。3.减小了先后端通信传输的数据量。4.方便对数据进行加密。github
其实这个过程的核心部分都是一次性的操做,只要实现一遍之后就能够重复使用。windows
流程后端
实现的流程大体能够分为三个部分:1.读取Excel文件。2.将读取的数据进行序列化并写入文件。3.反序列化。#4.加密数组
读取Excelide
原本读取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