cocos2d-x 2.2.0 控制lua腳本加載時的搜索路徑

在lua語言中,require語句搜尋模塊有一個內置的順序,而且能夠經過package.path來維護模塊的搜索策略。
可是在cocos2d-x中,不是這樣!android

cocos2d-x重載了本來的lua的require加載方式。(見Cocos2dxLuaLoader.cpp )
Cocos2dxLuaLoader邏輯的生效是在package.path以前,而且package.path在安卓上則不能很好的處理加載pkg包內部文件的問題。
因此在實際使用中,咱們只使用cocos2d-x重載的方法就能夠了。ui

怎麼作呢?lua

Cocos2dxLuaLoader內部是使用CCFileUtils::getFileData()來加載lua代碼的。
因此咱們要想添加本身的lua腳本搜索路徑,那麼只要調用CCFileUtils::addSearchPath()就能夠了。code


如下C++代碼實現了在iOS和android平臺上,程序先從下載路徑下的scripts文件夾尋找lua文件,再從程序內置資源路徑下的scripts文件夾尋找lua文件的目標。ip

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    std::string downloadedScriptPath = CCFileUtils::sharedFileUtils()->getWritablePath() + "scripts";
    CCFileUtils::sharedFileUtils()->addSearchPath(downloadedScriptPath.c_str());
    std::string scriptPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("scripts");
    CCFileUtils::sharedFileUtils()->addSearchPath(scriptPath.c_str());
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    std::string downloadedScriptPath = CCFileUtils::sharedFileUtils()->getWritablePath() + "/scripts";
    CCFileUtils::sharedFileUtils()->addSearchPath(downloadedScriptPath.c_str());
    CCFileUtils::sharedFileUtils()->addSearchPath("assets/scripts");
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
    //TODO
#else
    _ERROR_
#endif
相關文章
相關標籤/搜索