CocosCreator系列教程——如何將.apk解壓出來的.atlas文件轉成.plist文件

CocosCreator系列教程——如何將.apk解壓出來的.atlas文件轉成.plist文件

因爲要用到另外一個項目的圖片,但是解壓出來以後全是.atlas文件,不能直接使用,然後用ide打開文件看了一下這兩個文件的區別,首先是.atlas文件
在這裏插入圖片描述
我們來和.plist文件對比一下就一目瞭然了
在這裏插入圖片描述

在這裏插入圖片描述
明白原理以後,就是字符串和IO操作了,接來下要怎麼搞呢,當然是人生苦短,我選python了,因爲CocosCreator要配置python2.7.5的環境,所以直接上菜鳥教程.看一下基本語法和文件讀寫就直接開搞了,老規矩,直接上代碼,拷貝直接用( ̄▽ ̄)"

#_*_coding:utf-8_*_ 
import os

# 遍歷所有根目錄文件
result = []
def get_all(cwd):
    get_dir = os.listdir(cwd)  
    for path in get_dir:          
        sub_dir = os.path.join(cwd,path)  
        if os.path.isdir(sub_dir):     
            get_all(sub_dir)
        else:
            result.append(cwd + '/'+ path)

# 處理單個atlas
def handleAtlas(atlas):
    lines = atlas.split('\n')
    list = []
    repeatName = {}
    for index in range(len(lines)):
        line = lines[index].replace(' ','').replace('\n','')

        if line.find('.png') != -1:
            list.append({'name': line})
            continue

        if line == '':
            continue
        
        if (line.find(':') == -1):
            if repeatName.has_key(line):
                repeatName[line] += 1
            else:
                repeatName[line] = 0

            if repeatName[line] > 0:
                line += '%d'%repeatName[line]
            list.append({'name': line})
            continue
        
        if (len(list) == 0):
            continue

        if (line.find('size:') != -1):
            dict = list[len(list) - 1]
            dict['size'] = line.replace('size:','')
        elif (line.find('rotate:') != -1):
            dict = list[len(list) - 1]
            dict['rotate'] = line.replace('rotate:','')
        elif (line.find('xy:') != -1):
            dict = list[len(list) - 1]
            dict['xy'] = line.replace('xy:','')
        elif (line.find('orig:') != -1):
            dict = list[len(list) - 1]
            dict['orig'] = line.replace('orig:','')
        elif (line.find('offset:') != -1):
            dict = list[len(list) - 1]
            dict['offset'] = line.replace('offset:','')

    data = list[0]
    head = '<key>metadata</key>\n'
    head += '<dict>\n'
    head += '<key>format</key>\n'
    head += '<integer>2</integer>\n'
    head += '<key>realTextureFileName</key>\n'
    head += '<string>{name}</string>\n'
    head += '<key>size</key>\n'
    head += '<string>{{{size}}}</string>\n'
    head += '<key>textureFileName</key>\n'
    head += '<string>{name}</string>\n'
    head += '</dict>\n'
    head = head.format(name=data['name'], size=data['size'])
    name = data['name'].replace('.png','');

    frames = ''
    list.remove(data)
    for data in list:
        frame = '<key>{name}.png</key>\n'
        frame += '<dict>\n'
        frame += '<key>frame</key>\n'
        frame += '<string>{{{{{xy}}},{{{size}}}}}</string>\n'
        frame += '<key>offset</key>\n'
        frame += '<string>{{{offset}}}</string>\n'
        frame += '<key>rotated</key>\n'
        frame += '<{rotate}/>\n'
        frame += '<key>sourceColorRect</key>\n'
        frame += '<string>{{{{{xy}}},{{{size}}}}}</string>\n'
        frame += '<key>sourceSize</key>\n'
        frame += '<string>{{{size}}}</string>\n'
        frame += '</dict>\n'
        frames += frame.format(name=data['name'], xy=data['xy'],size=data['size'],rotate=data['rotate'],offset=data['offset'])

    plist = '<?xml version="1.0" encoding="UTF-8"?>\n'
    plist += '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n'
    plist += '<plist version="1.0">\n'
    plist += '<dict>\n'

    plist += '<key>frames</key>\n'
    plist += '<dict>\n'
    plist += frames
    plist += '</dict>\n'

    plist += head + '\n'

    plist += '</dict>\n'
    plist += '</plist>\n'

    return {'plist': plist,'name': name}

# 處理單個altas文件
def handle(path):
    f = open(path,"r")

    atlasList = []
    for line in f.readlines():
        if line.find('.png') != -1:
            atlasList.append(line);
        elif len(atlasList) > 0:
            atlasList[len(atlasList) - 1] += line

    for atlas in atlasList:
        data = handleAtlas(atlas)
        fo = open(os.path.join(os.path.dirname(path) , data['name'] + ".plist"), "w")
        fo.write(data['plist']) 

# main
get_all('./')
for path in result:
    if path.lower().endswith('.atlas'):
        handle(path)

使用方法:新建一個parse.py,然後把代碼拷進去,把所有的.atlas文件和parse.py放到同一目錄下,然後在當前目錄輸入cmd打開命令窗口,輸入:python parse.py運行這個python文件,然後就可以看到所有轉換成功的.plist文件了,最後把.plist文件和所對應的.png文件一起拖入項目中就可以直接使用了。
在這裏插入圖片描述
哪裏不會的可以私信我講解一下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章