How the data in the C # Unity game development--excel is in the game (i)

Source: Internet
Author: User
Tags rowcount

Introduction

Now do the game development of the few without Excel, with the most is planning. In particular, numerical planning, Excel provides users with powerful tools, a variety of shortcut keys, a variety of plug-ins, various functions. But as a program, it is not Excel but the final form of data, and in the program data is actually binary, such as an int is 4 byte, a letter of 2 byte. However, it is not possible to put Excel files in the game (because Excel itself is a part of the extra space), it is not possible to package the class library processing Excel into the program, so now most of the Excel read and then serialize the data and write to the file and then package, When the program runs, it reads the data files directly and deserializes them.

There are several advantages to doing so: 1. Save space. 2. Put the game-related actions out of the game. 3. It is also convenient to use during the development of the game, because the data after the deserialization is a variable of a type that you define. 3. Reduce the amount of data transmitted by the front-end communication. 4. Easy to encrypt data.

In fact, the core of the process is a one-time operation, as long as the implementation can be reused later.

Process


The implementation process can be broadly divided into three parts: 1. Read the Excel file. 2. Serialize the read data and write to the file. 3. Deserialization. #4. Encryption

Read Excel

Originally read Excel is a very simple thing, but started to find, for C #, Excel operation of the class library, so the use of which class library is a problem, in fact, these operations are in the game outside the class library performance can be ignored, but the general people certainly like to use a better performance.

Another problem is relatively important, many Excel libraries are based on the Windows development environment, but also need to install Office, switch to other platforms are not supported, and unity is the platform game engine, so based on these points, I finally chose a cross-platform operation for Excel's Open source class library Excelreader

usingUnityengine;usingUnityeditor;usingSystem.Collections;usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingExcel;usingSystem.Data; Public classExportexcel {[MenuItem ("Frame/exportexcel")]     Public Static voidexportexceltobinary () {Dictionary<string, list<list<property>>> DataMap =Newdictionary<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); introw =0, col =0; Try        {            foreach(stringPathinchfiles) {Info=NewFileInfo (path); Stream=info.                Open (FileMode.Open, FileAccess.Read); Excelreader=Excelreaderfactory.createopenxmlreader (stream); Result=Excelreader.asdataset (); Classes=NewList<list<property>>(); intRowCount = result. tables[0].                Rows.Count; intColCount = result. tables[0].                Columns.count;  for(row =1; Row < RowCount; row++) {Properties=NewList<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 (NewProperty (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 ("array subscript out of range! "); }    }}

First read the data here dictionary<string, List<list<property>>> DataMap, and later how to use meta data to do mapping, how to serialize.

There is the Excel file is a format requirements Oh, the file name is the class name, the first sheet is the data from the second line to read. The first line of the second sheet is the field name, the second row is the field type, and the first column of the third row is the reserved class name. Such as

There is also a property class that is used for mapping and serialization later

usingSystem; Public classProperty { Public stringname;  Public stringvalue;  Public stringtype;  PublicProperty (stringNamestringValuestringtype) {         This. Name =name;  This. Value =value;  This. Type =type; }     PublicType GetType () {return NULL; //todo..    }}
View Code

Read a lot of posts, more useful first to everyone posted out. 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

How the data in the C # Unity game development--excel is in the game (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.