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. 【SQL】约束与触发器2

    3.修改约束 3.1给约束命名 按如下格式命名: name ) CONSTRAINT NameIsKey PRIMARY KEY gender ) CONSTRAINT NoAndro CHECK ( ...

  2. Selenium2+python自动化66-装饰器之运行失败截图【转载】

    前言 对于用例失败截图,很多小伙伴都希望在用例执行失败的时候能自动截图,想法是很好的,实现起来并不是那么容易. 这里分享下我的一些思路,当然目前还没找到完美的解决方案,我的思路是用装饰器去解决,希望有 ...

  3. 使用vscode开发调试.net core应用程序并部署到Linux跨平台

    使用VS Code开发 调试.NET Core RC2应用程序,由于.NET Core 目前还处于预览版. 本文使用微软提供的示例进行开发及调试. https://github.com/aspnet/ ...

  4. NAT+穿洞基础知识梳理

    参考:https://www.cnblogs.com/shilxfly/p/6589255.html https://blog.csdn.net/phoenix06/article/details/7 ...

  5. (19)python扩展

    当python程序遇到瓶颈时,可以考略扩展其他语言 例如:程序的某部分,需要高速度,或者与硬件交互时可以用到C语言.当其他语言有现成的程序,重新起来很麻烦时.有些功能用别的语言写更方便时 扩展语言有  ...

  6. codeforces Round #440 B Maximum of Maximums of Minimums【思维/找规律】

    B. Maximum of Maximums of Minimums time limit per test 1 second memory limit per test 256 megabytes ...

  7. POJ1251 Jungle Roads(Kruskal)(并查集)

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23882   Accepted: 11193 De ...

  8. 【树链剖分】【线段树】bzoj2157 旅游

    #include<cstdio> #include<algorithm> using namespace std; #define INF 2147483647 #define ...

  9. 【暴力】bzoj3713 [PA2014]Iloczyn

    没什么好说的. #include<cstdio> using namespace std; typedef long long ll; ]; int main() { scanf(]=; ...

  10. OC语言基础之NSDictionary

    1.NSDictionary字典的创建 1: // key value 2: // key -==> value 3: NSDictionary *dict = [NSDictionary di ...