1. 程式人生 > >cocos2d-x 資源搜尋路徑 addSearchPath addSearchResolutionsOrder

cocos2d-x 資源搜尋路徑 addSearchPath addSearchResolutionsOrder

今天遇到了資源找不到的問題,看了程式碼,瞭解了下cocos2d的資源搜尋方式

新建一個專案,我的路徑F:/projects/luatest/


CCFileUtils::getInstance()->addSearchPath("F:/projects/luatest2");
CCFileUtils::getInstance()->addSearchPath("src");
/*
此時
searchPath 有3個
1. "F:/projects/luatest/"  預設專案路徑
2. "F:/projects/luatest2"
3. "F:/projects/luatest/src/"

*/
CCFileUtils::getInstance()->addSearchResolutionsOrder("res");

/*
resourceDirectory 有2個
1. ""    預設,空
2. "res/"
*/

//測試:尋找close.png,這個檔案不存在,看看cocos2d搜尋了哪些路徑CCFileUtils::getInstance()->fullPathForFilename("Images/close.png");
/*
cocos2d搜尋的時候,會把檔名分拆成兩部分,具體看CCFileUtils.cpp的FileUtils::getPathForFilename
file_path = "Images/"
filename ="close.png"
*/

/*
//搜尋路徑組合方式: searchPath + file_path + resourceDirectory + filename

最終搜尋的所有路徑
F:/projects/luatest/Images/close.png
F:/projects/luatest/Images/res/close.png
F:/projects/luatest2/Images/close.png
F:/projects/luatest2/Images/res/close.png
F:/projects/luatest/src/Images/close.png
F:/projects/luatest/src/Images/res/close.png
*/