標籤:
引言
現在做遊戲開發的沒有幾個不用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.. }}View Code
看了很多文章,比較有用的先給大家貼出來。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
C# Unity遊戲開發——Excel中的資料是如何到遊戲中的 (一)