Unity3DLua-将xls转化为lua

Unity3DLua-将xls转化为lua。使用lua写unity3d项目,由于加载数据一直觉得不方便,于是写个脚本将xls数据文件转化为lua文件,这样lua逻辑就可以直接读取数据。 为什么要这样做呢?

首先转化为lua后就不再需要解析csv,json等数据了,可以直接使用。

其次,数据转乘lua后,在同步lua脚本时,可以一并同步数据。在同步环节省去了同步数据的麻烦。

再者,使用xls2lua脚本转化为lua数据文件,可以达到自动化校验的效果,省去一部分人为操作失误。

最后,我观察到很多游戏都使用这种方式,其中比较有名的是 《刀塔传奇》。其实这种方式早就很普遍了,只不过我沿着别人的足迹而已走到了这了里而已。

项目已经放在了github上,如果需要可以去拿。如果喜欢可以star(收藏)下。

Github address : https://github.com/luzexi/xls2lua

以下是github的readme部分的文字,描述有英文和中文两个版本。你可以忽略英文部分。

Excute Example (举例执行命令)

python ./xls2lua.py example_building.xls ./data/

NOTICE:(注意点)

The sheet name must start with "output_" , the lua file name will be the name behind "output_".
The **first row** must be **title**. 
The **second row** must be **type** 
The **type must be i , f , s , b , ai , af , as , ab.
i mean int , f mean float , s mean string , b mean bool , ai mean array int , af mean array float , as mean array string , ab mean array bool.
The **first column** must be int , so the type in first column must be i.
The string type with char **"** or **'** will be replace by \" or \' 
The empty col will be a default value like 0 or "" or false or {} 
(sheet名以"output_"开头的才会被识别转换,否则将被忽略) 
(第1行必须是关键字名) 
(第2行必须为类型) 
(类型有:i,f,s,b,ai,af,as,ab这几种) 
(i表示int,f表示float,s表示string,b表示bool,ai表示int数组,af表示float数组,as表示string数组,ab表示bool数组) 
(第1列必须为int类型的唯一关键字) 
(string类型中"和'会自动用\"和\'替代)
(空列将会被默认值代替,例如:0,"",false,{})

Lua script (生成后的Lua文件示例)

-- this file is generated by program!
-- don't change it manaully.
-- source file: example_building.xls
-- created at: Thu Mar 26 02:53:52 2015

local data = {}

data[1] = { id = 1,  name = "house",  use_money = 1000,  use_food = 2.33,  is_init = true,  defense = 100,  aadd = {1,2,3},  aadddss = {1.23,2,3.23},  ddff = {"sdf","23e","s"},  ffdd = {true,false,true}}
data[2] = { id = 2,  name = "house2",  use_money = 123,  use_food = 336.2,  is_init = true,  defense = 0,  aadd = {1,2,3},  aadddss = {1,2.3445,3},  ddff = {"你好","你在哪"},  ffdd = {true,false}}
data[3] = { id = 3,  name = "",  use_money = 456,  use_food = 222.33665,  is_init = false,  defense = 130,  aadd = {3,2,5},  aadddss = {3,2,2.5},  ddff = {"我在这里啊","你在那","呢"},  ffdd = {false,true}}
data[4] = { id = 4,  name = "farm",  use_money = 100,  use_food = 220.0,  is_init = false,  defense = 200,  aadd = {2,3},  aadddss = {200.3,3,234.23},  ddff = {"df","ssd","dd","dd"},  ffdd = {}}
data[5] = { id = 5,  name = "house5",  use_money = 0,  use_food = 22.1,  is_init = false,  defense = 234,  aadd = {3,6,6,7},  aadddss = {3,6.3,6,7},  ddff = {"ss","d","d","d"},  ffdd = {true,true}}
data[6] = { id = 6,  name = "horse3",  use_money = 200,  use_food = 0,  is_init = false,  defense = 333,  aadd = {},  aadddss = {},  ddff = {"2e","w","e","we"},  ffdd = {false,false,false,false}}

return data

How to use lua with data. (如何使用生成的lua数据)

local building = require "building"

print(building[1].name)

The console will print “house”

转载注明出处 http://www.luzexi.com

· Unity3D, 游戏通用模块, 前端技术

感谢您的耐心阅读

Thanks for your reading

  • 版权申明

    本文为博主原创文章,未经允许不得转载:

    Unity3DLua-将xls转化为lua

    Copyright attention

    Please don't reprint without authorize.

  • 微信公众号,文章同步推送,致力于分享一个资深程序员在北上广深拼搏中对世界的理解

    QQ交流群: 777859752 (高级程序书友会)