Objective:
Before using the Npoi plugin to write the Guide table tool, in fact, the data is directly serialized , parsing also need to be deserialized, the steps are more complicated, recently saw a Google open-source project Protobuf , it can be used not only for the export of Excel tabular data, but also for the customization of network communication protocol directly.
First, Protobuf Introduction:
Protobuf is an open source project published by Google and is a convenient and versatile data transfer protocol. So we can also use PROTOBUF in unity for data storage and network protocol Development, here first of all the data storage part of the operation, that is:
serialize the. xls tabular data through protobuf and use it in Unity .
1. Download resources:
- Python2.7 installation method: Python Installation and configuration
- Protobuf-2.5.0.zip:http://pan.baidu.com/s/1kvk5lhp
- Protobuf-net github:https://github.com/mgravell/protobuf-net
2. Flowchart:
The basic steps can be seen from the following:
- . xls table file, first through xls_deploy_tool.py Generate the corresponding. data file and the. proto file, where the. data file is the result of the serialization of the table, and the. proto file is the intermediate state of the parsing class used to generate the deserialization;
- Parse class. Proto is converted to a. desc file by Protoc.exe , which is used to translate tools such as protobuf-net into a specific language, where we need to get the C # parsing class, which is the. CS class;
- Import the Protobuf-net.dll Library in unity and call the above generated. CS parsing class in C # code to parse the data in.
Second, the Guide table environment configuration:
1.Python Related configuration:
Due to the need to rely on the Proto Library and the xlrd library for generating. Data and. Proto,python from an. xls file, install the configuration steps:
- Setuptools: This is a Python component Installation Manager that needs to be installed before installing the Protobuff component, downloading the plugin's installation package to the Setuptools website, extracting it into the specified directory, and then using the command line to enter the installation package directory. Execution instructions:python setup.py install;
- Protobuff: First, we will download the good source package Protobuf-2.5.0.zip and compile the package protoc-2.5.0-win32.zip Unzip the package to the specified directory, the path is best not to include Chinese;
- Here I decompression Protobuf-2.5.0.zip to the position is "E:\Unity_Workplace\protobuf_250";
- Then copy the Protoc-2.5.0-win32.zip extracted from the protoc.exe to the PROTOBUF_250\SRC directory;
- Create a folder under Protobuf-2.5.0\python\google\protobuf named compiler(two files will be generated in this directory after the installation is complete __init__.py and Plugin_ pb2.py);
- use the command line to go to the extracted directory below the Python directory , execute: python setup.py Install ;
- xlrd (xls reader): This is actually read the XLS tabular data of a tool plug-in, to xlrd official website Download Xrld installation package, unzip the installation package and then use the command line into the installation package directory, execute instructions:python setup.py Install.
2. Guide External tools:
- xls_deploy_tool.py: This tool is actually an open source compliant Protobuff standard on GitHub based on Excel automatically generates a matching PB definition (. proto file ) and a tool that generates binary data or text data (. data files ) after serializing the data, github:xls_deploy_tool.py.
- Protoc.exe and Protogen.exe: through the tools above, We got two files: the. Data file that stores the information and the. proto file that is used to parse the data, but when we really use the parsing class to parse the data file, it must be a high-level language, and of course protobuf-net offers many kinds of high-level language support. As we do in unity we use the C # language, which requires two tools, one is protobuf-2.5.0 protoc.exe convert. Proto files to "Filedescriptorset" intermediate format The other is to use the Protogen.exein protobuf-net to convert the intermediate format file to the final state, the parsing class . cs file for the high-level language.
- Can be downloaded to GitHub protobuf-net Source: protobuf-net, download and unzip to the local, and then into the Protobuf-net-master\protobuf-net directory after decompression, through the visual Studio Open Protobuf-net.csproj:
- After compiling, the compiled file is generated in the Bin\release directory below the current directory, where we need protobuf-net.dll:
- Copy the Protobuf-net.dll to the Protobuf-net-master\protogen directory, open the protogen.csproj with Visual Studio, and compile the Protogen project with the steps above to get The Protobuf-net-master\protogen\bin\release directory below the Protogen.exe and some additional files, but all the files under this directory are required when really used:
Three, examples:
1. Create the form. XLS:
Of course the table using this tool for the Guide table needs to conform to the specified format, according to Xls_deploy_tool.py's notes:
# Description: # The first four lines of Excel are used for the structure definition, the rest is data, according to the first line, respectively: # required must have attribute # Optional optional attribute # Second line: Property Type # Third line: Property name # line fourth: Comment # data row: Property value # Repeated indicates that the next property is repeated, which is the array # second row: Repeat the maximum number of times, Excel repeats the list The property # 2011-11-29 made a modification to the second line if it is a type definition, it indicates that the column is repeated# but currently only supports shaping # third line: Useless # line fourth: Comment # Data row: Actual number of repetitions # REQUIRED_STRUCT required structure Properties # OPTIONAL_STRUCT Optional Structure Properties # second row: number of structure elements # third row: struct name # Line four: property name in the upper structure # Data line: No Fill # 1 | required/optional | Repeated | Required_struct/optional_struct |# | ------------------| ---------: |---------------------------------: |# 2 | Property Type | | Number of structural elements |# 3 | Property name | | struct type name |# 4 | Comment Description | | Attribute names in the upper structure |# 5 | Property values | | |
Of course, you can refer to the sample table downloaded from github, download the TNT project, and then copy the contents of the Python directory below, where the XLS file has a Goods_info.xls sample table:
2.xls_deploy_tool.py conversion gets. Data and. Proto:
The operation of the Guide table can be done with a single instruction in the command line:
Python xls_deploy_tool.py sheet_name Xls_path
It contains two parameters: Sheet_name is the name of the table page in. xls for the Guide table, Xls_path is the path to the. xls file for the Guide table. Create a test engineering test_protobuf to put two of the files and Protoc.exe in 1:
On the command line, navigate to the directory, and then run the directive:
Call Python xls_deploy_tool.py goods_info Xls/goods_info.xls
At the end of the run, there were a few more files in the directory, but we really needed only two files, the. data file and the. Proto Parsing class:
3. Get the final parsing class:
Protoc.exe Get the intermediate format file, assuming that the suffix is. Protodesc, use the directive:
Protoc input file path (. proto file)--descriptor_set_out= output file path (. protodesc)
Continue to execute the instructions on the basis of the test project in step 2:
Protoc Tnt_deploy_goods_info.proto--descriptor_set_out=goods_info.protodesc
after running this step, there is another. protodesc file in the project that corresponds to the. Proto:
protogen.exe get. CS parsing class, using directives:
Protogen-i: Input file path (. protodesc)-o: Output file path (. cs)
copy all the files under the Protobuf-net-master\protogen\bin\release directory that were previously generated protogen.exe to the current project, with a folder Protogen to store them, If you do not want to perform such a tedious process, you can also directly use my compiled Protogen file directory compression package: Protogen.zip, the current project under the root directory to execute the following command:
Call Protogen\protogen-i:goods_info.protodesc-o:goods_info.cs
The result of the execution, the final state of the resolved class is generated in the current directory Goods_info.cs:
Of course, the above three steps can be done directly with batch processing, directly under the current project root directory to create a new file, named Generator.bat, the content is:
Call Python xls_deploy_tool.py goods_info xls/goods_info.xlscall protoc tnt_deploy_goods_info.proto--descriptor_set_ Out=goods_info.protodesccall Protogen\protogen-i:goods_info.protodesc-o:goods_info.cspause
simply double-click the file to complete all of the above actions to generate the final . Data and. cs files .
4.Unity Import library file:
Add several files to the Unity project, place the. data file under the Assets\streamingassets\dataconfig directory, and place the Protobuf-net.dll and Goods_info.cs in the assets directory:
Create a Test.cs test script that uses protobuf in the script to import the libraries in Protobuf-net.dll, and then use the using tnt_deploy The. CS Tabular Data parsing class that is generated by the Import Guide table, the script specific code content is:
Using unityengine;using system.collections;using protobuf;using system.io;using tnt_deploy;public class Test: monobehaviour {void Start () {Goods_info_array Goods_infos = readonedataconfig<goods_info_array> ("GOODS_INFO "); Debug.Log ("goods_id==================" + goods_infos.items[0].goods_id);} Private T readonedataconfig<t> (string FileName) {FileStream FileStream; FileStream = Getdatafilestream (FileName); if (null! = FileStream) {T t = serializer.deserialize<t> (FileStream); Filestream.close (); return t; } return default (T); } Private FileStream Getdatafilestream (string filename) {string filePath = Getdataconfigpath (fileName); if (file.exists (FilePath)) {FileStream FileStream = new FileStream (FilePath, FileMode.Open); return fileStream; } return null; } private String Getdataconfigpath (string filenAME) {return application.streamingassetspath + "/dataconfig/" + FileName + ". Data"; }}
Create a new scene in unity, mount the Test.cs on the main camera, run the scene, see the print results, and explain the success of the parse table data:
5. Platform compatibility issues:
The JIT error (executionengineexception:attempting to JIT compile method) occurs in iOS because the Protobuf-net.dll is placed directly into the project. The reason is that iOS does not allow the JIT (Just in time), only the AOT (Ahead of Time) is allowed.
Workaround:
Copy all the source code below the Protprotobuf-net-master\protobuf-net directory directly under the directory of the Unity project, but since the compilation process of protobuf-net is unsafe to compile , So unity will have a compile error:
need to Add a SMSC.RSP file under the assets directory , the content is simple, there is only one line "-unsafe", after adding it, close unity and reopen unity, everything is fine.
Iv. Summary:
Although the configuration of the Guide table environment is cumbersome, but after the completion of the work efficiency is very high, and Proto has a prominent versatility, can be applied to a variety of language environments.
UNITY--PROTOBUF Excel Tabular Data