C# Unity遊戲開發——Excel中的數據是如何到遊戲中的 (一)

引言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..
    }
}
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

,加密:http://wenku.baidu.com/view/031ede00964bcf84b9d57b6e.html

 本文固定連接:http://www.cnblogs.com/fly-100/p/4538975.html

相關文章
相關標籤/搜索