cocos2d-x 遍历assets下资源
来源:互联网 发布:苹果微信加附近人软件 编辑:程序博客网 时间:2024/06/07 11:19
首先说明,本人用的2dx版本是2.2.6。
但是遍历资源跟版本的跨度关系不大,使用的类是ZipFile。
第一步在:在ZipFile添加方法,因为_dataThread是个私有的,尽量不改变源码的情况下,添加一个get方法是最好的
这个getAllFile可以返回所有的文件目录
std::vector<std::string>ZipFile::getAllFile(){
std::vector<std::string> vec;
ZipFilePrivate::FileListContainer::iterator it1;
for(it1=_dataThread->fileList.begin();it1!=_dataThread->fileList.end();++it1) {
vec.push_back(it1->first.c_str());
}
return vec;
}
第二步:使用getAllFile的返回值做遍历,这里直接帖出IOS和android的同时遍历吧,同时搜索png和jpg的图片,可以用于加载资源,记得导入头文件
#include "support/zip_support/ZipUtils.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include <dirent.h>
#include <sys/stat.h>
#else
#include "platform/CCCommon.h"
#include "support/zip_support/unzip.h"
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#endif
void ResourceLoadingLayer::getAllFile(std::string folderPath,int depth, std::vector<std::string> *list,std::string head){
#if CC_PLATFORM_IOS == CC_TARGET_PLATFORM
DIR *dp;
structdirent *entry;
structstat statbuf;
if((dp =opendir(folderPath.c_str())) ==NULL) {
fprintf(stderr,"cannot open directory: %s\n", folderPath.c_str());
return;
}
chdir(folderPath.c_str());
while((entry =readdir(dp)) !=NULL) {
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) ==0)
continue;
getAllFile(entry->d_name,depth+4,list,head+entry->d_name);
} else {
if (head.length() ==0) {
string name = entry->d_name;
if (name.length()>3 && name.rfind(".") > 0 && (name.substr(name.rfind(".")+1,3) == "jpg" || name.substr(name.rfind(".")+1,3) == "png")) {
list->push_back(entry->d_name);
}
} else {
string filename = head+"/" +entry->d_name;
if (filename.length()>3 && filename.rfind(".") > 0 && (filename.substr(filename.rfind(".")+1,3) == "jpg" || filename.substr(filename.rfind(".")+1,3) == "png")) {
list->push_back(filename);
}
}
}
}
chdir("..");
closedir(dp);
#else
ZipFile* pFile = new ZipFile(getApkPath(),"assets/");
vector<string> vec = pFile->getAllFile();
for (int i=0; i<vec.size(); i++) {
string file = vec.at(i);
if (file.compare("assets/")) {
file = file.substr(7,file.length());
}
if(file.substr(0,folderPath.length()) == folderPath ){
string filename = file.substr(file.rfind("/")+1,file.length());
if (filename.length()>3 && filename.rfind(".") >0 && (filename.substr(filename.rfind(".")+1,3) =="jpg" || filename.substr(filename.rfind(".")+1,3) =="png") {
list->push_back(filename);
}
}
}
#endif
}
第三步,使用:
首先得拿到资源文件夹的完整路径,方法有很多,只举其一:
在HelloWorld的工程下的资源根目录有一张HelloWorld.png图片,我们可以直接获取它在程序中的完整路径,即
string fullpath = CCFileUtils::sharedFileUtils()->fullPathForFilename("HelloWorld.png");
所以 string resPath = fullpath.substr(0,fullpath.rfind("/")+1);
得到了完整的资源路径后,
vector<string> vec;
getAllFile(resPath, 0, &vec, "");
这样就大功告成了,vec会保存所有的png或jpg的资源,会带head名的哦,比如说bg/welcome.jpg
上面是遍历所有的文件夹,如果单独遍历资源文件下的某个目录,即是
getAllFile(resPath+"xxx",0,&vec,""); xxx 为资源根目录下的子目录,这样就可以分别加载某个目录了
下面帖上ZipFile文件下载地址
http://pan.baidu.com/s/1gd8n3hT
- cocos2d-x 遍历assets下资源
- 获取assets下资源
- 【存储】Cocos2d-x将资源目录(Assets)文件拷贝到可写目录
- assets目录下资源使用方法
- cocos2d-x 资源集
- cocos2d-x demo资源
- cocos2d-x学习资源
- cocos2d-x demo资源
- cocos2d-x 资源集合
- cocos2d-x资源
- Cocos2d-x博客资源
- cocos2d-x 学习资源
- Cocos2d-x学习资源
- Cocos2d-x学习资源
- cocos2d-x 资源优化
- 关于cocos2d-x项目移植到android平台的时候明明已经将资源复制到assets还是出现的assets is nullptr的问题
- 【cocos2d-x 6】xcode 下开发 cocos2d-x 现有资源和现有文件的附加注意事项
- android应用Assets目录下的资源
- cookie 和session 的区别详解
- 【IOS 开发学习总结-OC-47】★ios开发之UI控件——UIAlertView与 UIActionSheet
- JavaMockito的使用
- 将List数组转化为datatable
- 大数据量的算法
- cocos2d-x 遍历assets下资源
- dubbo 常见的问题bug梳理
- NSSearchPathForDirectoriesInDomains用法,关于沙盒路径
- 微信手Q分享js定制
- Disable HTML a links in different browser
- 集合 --- HashSet
- 后缀数组 DA(倍增)算法求 SA[N] 与 Rank[N] (时间O(NlogN),空间O(N))
- thrift的概念理解
- /etc/init.d/rcs分析