TP:FCEE652B

cause

在游戏开发的过程中,很多时候需要策划填的一些静态数据表(比如英雄表,技能表等等),而策划一般都习惯使用excel。

excel在unity里面是不能直接读取的,所以我们一般要另存为txt读取,然后一行一行解析进行读取。

***.txt

id	name	price
1 张三 3.0
2 李四 4.0

但是有一点问题,策划改表是很频繁的,总不能每次策划改动我们另存为一次吧。

这个时候我们可以采用python写一个脚本动态批处理即可

how

1.安装mysql并且安装MySQLdb模块

测试环境win7 推荐python 编辑器pycharm

MySql_Win_Install

安装MySQL-Python(MySQLdb)

2.安装Navicat Premium导入excel表

3.配置读取参数

{
"db":{
"host":"127.0.0.1",
"user":"root",
"passwd":"",
"port":3306,
"db":"testdb"
},
"exp_sep":"\t",
"exp_suffix":".txt",
"tables":[
"test_table"
],
"vos":[
"test_table"
]
}

4.动态生成txt和静态配置类

#! /usr/bin/env python
#coding:utf8 import sys
import MySQLdb
import json reload(sys)
sys.setdefaultencoding('utf-8') # tables = ('test_table',)
# vo = ('test_table',)
# ('test_table') is interpreted as using algebraic grouping and simply as max_price and not a tuple.
# Adding a comma, i.e. ('test_table',) forces it to make a tuple. head = '''using UnityEngine;
using System;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
'''
class_part1 = '''public class %s
{
'''
field = ''' public %s %s;
'''
ctor_par1 = ''' public %s(string[] args)
{
'''
ctor_field = ''' this.%s=%s(arr[%s]);
'''
ctor_par2 = ''' }
'''
class_part2 = '}' def ConvetType(typeid):
if typeid == 3:
return 'int'
elif typeid == 4:
return 'float'
else:
return 'string' def ConvertCtorType(typeid):
if typeid == 3:
return 'int.Parse'
elif typeid == 4:
return 'float.Parse'
else:
return '' # description (('id', 3, 1, 11, 11, 0, 0), ('name', 253, 6, 765, 765, 0, 0), ('price', 4, 1, 12, 12, 31, 0))
def CreateCS(index, description):
vo = curconifg['vos']
with open('%s.cs'% vo[index],"w+") as my_cs:
my_cs.write(head)
my_cs.write(class_part1 % vo[index])
for colIndex in range(len(description)):
curCol = description[colIndex]
my_cs.write(field % (ConvetType(curCol[1]),curCol[0]))
my_cs.write(ctor_par1 % vo[index])
for colIndex in range(len(description)):
curCol = description[colIndex]
my_cs.write(ctor_field % (curCol[0],ConvertCtorType(curCol[1]),colIndex))
my_cs.write(ctor_par2)
my_cs.write(class_part2) def CreateConfig():
con = MySQLdb.connect(curconifg['db']['host'],curconifg['db']['user'],curconifg['db']['passwd'],curconifg['db']['db'],charset='utf8');
# notice ,charset='utf8'
with con:
cur = con.cursor()
tables = curconifg['tables']
for index in range(len(tables)):
table = tables[index]
# cur.execute("select * from %s where id = %s",('test_table',1))
# DB API requires you to pass in any parameters as a sequence
# but sql query is select * from \\'test_table\\' where id = 1
# like below error
# http://blog.xupeng.me/2013/09/25/mysqldb-args-processing
# so you can do it below,but not safe
query = 'select * from %s '%(table);
cur.execute(query)
with open('%s.txt'%(table),'w+') as my_txt:
# create file if not exits
description = cur.description
CreateCS(index,description)
# description (('id', 3, 1, 11, 11, 0, 0), ('name', 253, 6, 765, 765, 0, 0), ('price', 4, 1, 12, 12, 31, 0))
# create txt
line = curconifg['exp_sep'].join(str(curCol[0]) for curCol in description)
# id name price
my_txt.write(line+'\n')
for i in range(cur.rowcount):
row = cur.fetchone()
line = curconifg['exp_sep'].join(str(col) for col in row)
# 1 张三 3.0
if(i <= (cur.rowcount - 1)):
my_txt.write(line+'\n')
else:
my_txt.write(line) with open("config.json","r") as jsonFile:
curconifg = json.load(jsonFile)
CreateConfig()

结果如图:

improvement

向上面那样考虑生成txt然后一行一行解析其实还是有点麻烦的,因为这个解析规则还是依赖于我们

其实我们可以考虑在生成的时候转换一下

***.txt

id	name	price
1 张三 3.0 {"id":1,"name":"张三","price":3.0}

是不是有很多想法啦,骚年!

参考工程 Pratices1

Unity 配置静态excel 工作流程的更多相关文章

  1. Sitecore 8.2 工作流程

    假设您的新Sitecore项目的所有开发都已完成.现在的下一步是在网站上填写内容并准备上线.客户通知您他们希望使用专门的网站管理员团队负责整个内容管理流程,并要求您为他们准备实例以便能够执行此操作. ...

  2. ecshop 工作流程加载配置介绍

    ecshop 工作流程加载配置介绍 分类: ecshop2014-09-14 09:36 729人阅读 评论(2) 收藏 举报 模板引擎工作流 这里简单介绍下echsop工作流程: 首先,你会发现一般 ...

  3. 工作流程,编程,调试,性能:Unity游戏开发者应该学习的20个改进技巧

    Unity 是一个备受欢迎的游戏开发平台.它的功能令人印象深刻,同时也迎合了不同的游戏开发需求.游戏开发者可以使用 Unity 创建任何类型的游戏,从世界级的 RPG 游戏到最流行的增强现实游戏 Po ...

  4. KaliLinux常用服务配置教程DHCP服务工作流程

    KaliLinux常用服务配置教程DHCP服务工作流程 DHCP服务工作流程如图1.1所示. 具体的工作流程如下所示: (1)DHCP客户端以广播的方式发出DHCP Discover报文. (2)所有 ...

  5. SpringMVC-DispatcherServlet工作流程及web.xml配置

    工作流程: Web中,无非是请求和响应: 在SpringMVC中,请求的第一站是DispatcherServlet,充当前端控制器角色: DispatcherServlet会查询一个或多个处理器映射( ...

  6. GIT使用—安装配置及工作流程

    一.Git 与 SVN 区别 GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等. 1.GIT是分布式的,SVN不是:这是GIT和其它非分布式的版本控制系统,例如SVN,CV ...

  7. Excel催化剂开源第10波-VSTO开发之用户配置数据与工作薄文件一同存储

    在传统的VBA开发中,若是用的是普通加载项方法,是可以存储数据在xlam上的,若用的是Com加载项方法同时是Addins程序级别的项目开发的,配置文件没法保存到工作薄中,一般另外用配置文件来存放供调用 ...

  8. Git 安装配置及工作流程

    在使用Git前我们需要先安装 Git.Git 目前支持 Linux/Unix.Solaris.Mac和 Windows 平台上运行. Git 各平台安装包下载地址为:http://git-scm.co ...

  9. 03.AOF持久化机制配置与工作流程

    一.AOF持久化的配置 配置文件redis.conf,AOF持久化默认是关闭的,默认是打开RDB持久化 appendonly yes     二.工作流程: 打开AOF持久化机制之后,redis每次接 ...

随机推荐

  1. 忘记root密码怎么办

    忘记root密码有两种解决办法.一种是emergency模式,另一种是rescue模式. 1.emergency模式 这个模式又有人称为单用户模式.使用这种模式,前提是要知道grub密码.一般适用于对 ...

  2. 驱动模块和装模块的概念——Junit单元测试案例

    驱动模块是用来模拟被测试模块的上一级模块,相当于被测模块的主程序.它接收数据,将相关数据传送给被测模块,启用被测模块,并打印出相应的结果. 桩模块(Stub)是指模拟被测试的模块所调用的模块,而不是软 ...

  3. win32 listctrl控件右键菜单的实现

    HMENU Menu_list,Menu_all; POINT point; HINSTANCE hInstance;//下面代码放到BOOL WINAPI DialogProc下 case WM_C ...

  4. poj 3254(状态压缩+动态规划)

    http://poj.org/problem?id=3254 题意:有一个n*m的农场(01矩阵),其中1表示种了草可以放牛,0表示没种草不能放牛,并且如果某个地方放了牛,它的上下左右四个方向都不能放 ...

  5. apt-get常用命令及工作原理

    https://blog.csdn.net/mosquito_zm/article/details/63684608

  6. [BZOJ2648] SJY摆棋子 kd-tree

    2648: SJY摆棋子 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 5421  Solved: 1910[Submit][Status][Disc ...

  7. swipper插件引起的a链接失效问题

    在使用swiper过程中,发现a链接失效,此处没有效果,问题是 swiper是基于移动端触摸的,会有一个全局的click事件,这个事件屏蔽了A标签的链接,是为了防止手机滑动的时候不小心触发A标签而设定 ...

  8. python3图片验证码识别

    http://my.cnki.net/elibregister/CheckCode.aspx每次刷新该网页可以得到新的验证码进行测试 以我本次查看的验证码图片为例,右键保存图片为image.jpg 下 ...

  9. Codeforces #445 Div2 D

    #445 Div2 D 题意 给出一些字符串,要求构造一个最短的且字典序最小的字符串,使得给出的字符串都为目标字符串的子串,且这些字符串作为子串出现的次数都是最多的,如果不存在目标字符串输出 &quo ...

  10. 洛谷——P1088 火星人

    P1088 火星人 题目描述 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法.这种交流方法是这样的,首先,火星人把一个非常 ...