cocos creator提取预设文本内容

发表于 2020-06-24  142 次阅读


"cc.Label"的对象,提取其"N$string"或"string"的内容即可。
大体思路遍历一遍prefab的文件夹,一行行读取正则匹配中文然后输出文本列表
直接上代码,python编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# find the line of containing chinese in files

import re
import os

def start_find_chinese(path):
    outstr=''
    with open(path, 'rb') as infile:
        while True:
            content = infile.readline()
            contentStr=str(content,encoding='utf-8')
            sub='_string'
            if re.match(r'(.*[\u4E00-\u9FA5]+)|([\u4E00-\u9FA5]+.*)', content.decode('utf-8'))  and contentStr.find(sub) != -1:
                idx=contentStr.index(sub)
                contentStr=contentStr[idx+11:-3]
                contentStr="'"+contentStr+"'"+":'',\n"
                outstr+=contentStr
            if not content:
                return outstr


def walkFile(file):
    out=''
    for root, dirs, files in os.walk(file):

        # root 表示当前正在访问的文件夹路径
        # dirs 表示该文件夹下的子目录名list
        # files 表示该文件夹下的文件list

        # 遍历文件
        for f in files:
            path=os.path.join(root, f)
            if path.find('.prefab') != -1 and path.find('.meta') == -1:
                out+=start_find_chinese(path)
    with open('tc_untranslated.txt', 'wb') as outfile:
        outfile.write(bytes(out, encoding = "utf8") )


        # # 遍历所有的文件夹
        # for d in dirs:
        #     print(os.path.join(root, d))

# start to find
if __name__ == '__main__':
    walkFile("P:\\xxx\\assets\\resources\\prefabs")

这里洋葱小鱿,上班摸鱼写写技术贴跟唠叨都放这里啦~