C# Unity游戏开发——Excel中的数据是如何到游戏中的

引言

现在做游戏开发的没有几个不用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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值