#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
auto xx_imgs = FileUtils::getInstance()->fullPathForFilename(filename);
return xx_imgs;
#else
// 资源路径
std::string sourcePath = cocos2d::FileUtils::getInstance()->fullPathForFilename(filename);
// 可写路径
std::string destPath = cocos2d::FileUtils::getInstance()->getWritablePath() + filename;
if (cocos2d::FileUtils::getInstance()->isFileExist(destPath)) {
return destPath;
}
if (cocos2d::FileUtils::getInstance()->isFileExist(destPath + ".tmp")) {
cocos2d::FileUtils::getInstance()->removeFile(destPath + ".tmp");
}
// write
cocos2d::Data data = cocos2d::FileUtils::getInstance()->getDataFromFile(sourcePath);
FILE *fp = fopen(destPath.c_str(), "w+");
if (fp)
{
size_t size = fwrite(data.getBytes(), sizeof(unsigned char), data.getSize(), fp);
fclose(fp);
if (size > 0)
{
return destPath;
}
}
CCLOG("copy file %s failed.", filename.c_str());
return "";
#endif
该段代码展示了在Cocos2d-x游戏引擎中,针对iOS和非iOS平台进行文件读写操作的方法。在iOS上直接返回文件全路径,而在其他平台上,首先检查可写路径下文件是否存在,若存在则返回,否则尝试从资源路径复制文件到可写路径,并写入。如果写入成功,则返回可写路径,否则记录错误并返回空字符串。
1324

被折叠的 条评论
为什么被折叠?



