cocos2dx的android版FileUtils的坑

时间:2022-09-11 04:56:58

cocos2dx3.13,FileUtils-android.cpp中可以看到:

FileUtils::Status FileUtilsAndroid::getContents(const std::string& filename, ResizableBuffer* buffer)
{
static const std::string apkprefix("assets/");
if (filename.empty())
return FileUtils::Status::NotExists; string fullPath = fullPathForFilename(filename); if (fullPath[0] == '/')
return FileUtils::getContents(fullPath, buffer); string relativePath = string();
size_t position = fullPath.find(apkprefix);
if (0 == position) {
// "assets/" is at the beginning of the path and we don't want it
relativePath += fullPath.substr(apkprefix.size());
} else {
relativePath = fullPath;
}
...
}

如果路径以assets/开头,则assets会被去掉。这就意味着你的Resources下不能有assets文件夹,否则下面所有的文件都会由于这个规则而无法取到。

另外,android版本的FileUtil不可以在静态变量中进行初始化。也就是说FileUtil::getInstance()这个函数不能在静态变量中调用。因为其中将调用getAPKPath函数,而这个函数在程序开始时会返回NULL,用它初始化一个string会崩溃。

记录一下