如果看过我上一篇博客的人可能会记得我之前进行批量处理图片大小的经历,就在那之后,经过一系列的步骤之后,我们终于得到了梦寐以求的Bundle包,只可惜是中文的,500多个包啊。全部是中文的。但是Unity对于中文路径的资源是 可以 写 不可以 读。所以需要改成对应的拼音,(如果有高手大哥可以转换成英文的话,可以给我留言哦,谢谢),下面就是我的python脚本了,修改文件夹下以“.unity3d”为后缀的所有文件的中文名-》拼音名字;
备注:
1.导入pypinyin库,可以使用里面的lazy_pinyin类。参考:https://pypi.python.org/pypi/pypinyin/0.5.0
2.导入了send2trash库,同样是删除不需要的文件到垃圾箱内(就是unity的配置文件.meta文件啦)
# -*- coding: gb18030 -*-
import sys, os, glob
from pypinyin import pinyin, lazy_pinyin
import pypinyin
from send2trash import send2trash
reload(sys)
sys.setdefaultencoding('utf-8')
rename_postfix=".unity3d"
delete_postfix=".meta"
def Py_Log(_string):
print "----"+_string.decode('utf-8')+"----"
def rename_file(file_path):
mPath, ext = os.path.splitext(file_path)
if(ext==rename_postfix):
ch_name=get_ch_name(mPath)
if(has_chinese(ch_name)):
py_name=get_py_name(ch_name);
print "名字转换: "+unicode(ch_name,'gbk')+" ==> "+py_name
names = os.path.split(file_path)
new_path = names[0]+"\\"+py_name+rename_postfix
if (os.path.exists(new_path)):
print "\""+py_name+"\"文件已经存在请修改后重试"
raw_input('press enter key to continue')
else:
os.rename(file_path,new_path)
elif(ext==delete_postfix):
send2trash(file_path)
print "Delete "+delete_postfix+" file :"+file_path
def has_chinese(text):
hz_yes = False
for ch in unicode(text,"gbk"):
if ch >= u'\u4e00' and ch<=u'\u9fa5':
hz_yes=True
else:
continue
return hz_yes
def get_ch_name(mPath):
tmp = mPath.split('\\')
ch_name=tmp[len(tmp)-1]
ch_name=ch_name.strip('0123456789')
return ch_name
def get_py_name(ch_name):
queue=""
py_name=lazy_pinyin(unicode(ch_name,'gbk'))
for pn in py_name:
queue+=pn.capitalize()
return queue
def BFS_Dir(dirPath, dirCallback = None, fileCallback = None):
queue = []
ret = []
queue.append(dirPath);
while len(queue) > 0:
tmp = queue.pop(0)
if(os.path.isdir(tmp)):
ret.append(tmp)
for item in os.listdir(tmp):
queue.append(os.path.join(tmp, item))
if dirCallback:
dirCallback(tmp)
elif(os.path.isfile(tmp)):
ret.append(tmp)
if fileCallback:
fileCallback(tmp)
return ret
def printDir(dirPath):
print "dir: " + dirPath
def printFile(dirPath):
print "file: " + dirPath
rename_file(dirPath)
if __name__ == '__main__':
while True:
path = raw_input("Path:")
try:
b = BFS_Dir(path , printDir, printFile)
Py_Log ("\r\n **********\r\n"+"*********文件处理完毕*********"+"\r\n **********\r\n")
except:
print "Unexpected error:", sys.exc_info()
raw_input('press enter key to exit')