• Unity 配置静态excel 工作流程


    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表

    1

    3.配置读取参数

    {
    	"db":{
    	"host":"127.0.0.1",
    	"user":"root",
    	"passwd":"",
    	"port":3306,
    	"db":"testdb"
    	},
    	"exp_sep":"	",
    	"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+'
    ')
                    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+'
    ')
                        else:
                             my_txt.write(line)
    
    with open("config.json","r") as jsonFile:
        curconifg = json.load(jsonFile)
        CreateConfig()
    

    结果如图:

    2

    improvement

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

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

    ***.txt

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

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

    参考工程 Pratices1

  • 相关阅读:
    Java并发编程:CountDownLatch、CyclicBarrier和Semaphore (总结)
    Java线程面试题 Top 50 (个人总结)(转)
    rabbitMQ windows 安装 入门(转)
    Java并发编程:volatile关键字解析(学习总结-海子)
    关于Sychronized和volatile自己总结的一点点理解(草稿)
    FWORK-数据存储篇 -- 范式与反模式 (学习和理解)
    Synchronized的原理及自旋锁,偏向锁,轻量级锁,重量级锁的区别(摘抄和理解)
    vcfc之zk+postsql+keystore(cassandra)框架分析
    CAP理论-解析
    java多线程通信 例子
  • 原文地址:https://www.cnblogs.com/chongxin/p/4950921.html
Copyright © 2020-2023  润新知