此类工具搜索过一些,都不要好用,于是决定自己实现一个。
大图集拆开为小图片,由于是工具,于此效率不太重要,直接用Cocos2dx随手写了一个。
基本思想是直接读取,然后把该SpriteFrame输出为文件即可
#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include <iostream>
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <fstream>
#include <stdlib.h>
USING_NS_CC;
using namespace std;
using namespace cocostudio::timeline;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
// 1. super init first
if (!Layer::init())
{
return false;
}
vector<string> files;
const char *TEM_WORK_FILE_NAME = "filelist.txt";
string path = "res";
string cmd_txt = "cmd.exe /c DIR " + path + " /S /B > ";
cmd_txt += TEM_WORK_FILE_NAME;
system(cmd_txt.c_str());
fstream f;
f.open(TEM_WORK_FILE_NAME, ios::in);
vector<string> _queue;
string buf;
while (getline(f, buf))
{
// cout << buf << " ... " << endl;
// cout<<buffer;
if (buf.find(".plist") == string::npos)
{
continue;
}
string file_ = buf;
string file_path = file_.substr(strlen("Resources\\res") + file_.find("Resources\\res") + 1, file_.size() - strlen("Resources\\res") - file_.find("Resources/res"));
while (file_path.find("\\") != string::npos)
{
// file_path.replace('\\', '/');
file_path[file_path.find("\\")] = '/';
}
log(file_path.c_str());
files.push_back(file_path);
}
// this->WriteToFile();
f.close();
//FileUtils::getInstance()->removeFile(TEM_WORK_FILE_NAME);
//system("pause");
//return 0;
//开始转换
// vector<string> files;
// //files.push_back("game_1_1.plist");
//files.push_back("game_1_2.plist");
//files.push_back("game_1_3.plist");
/*files.clear();
files.push_back("ui/awake.plist");
files.push_back("ui/yingxiongfuben_1.plist");
files.push_back("ui/yuanbaobiaoche.plist");
files.push_back("ui/xingkong2.plist");
files.push_back("ui/synthesis_equipment_4.plist");
files.push_back("ui/updateRecommend3.plist");
files.push_back("ui/shenmishangcheng.plist");
*/
for (auto & name : files)
{
string dir = name.substr(0, name.size() - strlen(".plist"));
//log((FileUtils::getInstance()->getWritablePath() + "/" + dir).c_str());
FileUtils::getInstance()->removeDirectory(FileUtils::getInstance()->getWritablePath() + "/" + dir);
FileUtils::getInstance()->createDirectory(FileUtils::getInstance()->getWritablePath() + "/" + dir);
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(name);
auto mapp = SpriteFrameCache::getInstance()->_spriteFrames;
for (auto & cp : mapp)
{
auto sp = Sprite::createWithSpriteFrameName(cp.first.c_str());// Sprite::createWithSpriteFrameName("accomplishTitle.png");
sp->setPosition(sp->getTextureRect().size.width / 2, sp->getTextureRect().size.height / 2 );
this->addChild(sp);
if (sp->getTextureRect().size.width <= 0 || sp->getTextureRect().size.height <= 0)
{//invalid sprite
continue;;
}
log((" out:" + dir + "/" + cp.first).c_str());
auto re = RenderTexture::create(sp->getTextureRect().size.width, sp->getTextureRect().size.height);
re->begin();
sp->visit(Director::getInstance()->getRenderer(), Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE), 0);
re->end();
string path = dir + "/" + cp.first;
re->saveToFile(path);
}
SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(name);
TextureCache::getInstance()->removeAllTextures();
SpriteFrameCache::getInstance()->removeUnusedSpriteFrames();
}
//Director::getInstance()->end();
return true;
}