cocos2d-x 2.2.0 控制lua脚本加载时的搜索路径

原文链接:http://segmentfault.com/blog/mituba/1190000000454114


        在lua语言中,require语句搜寻模块有一个内置的顺序,并且可以通过package.path来维护模块的搜索策略。
        但是在cocos2d-x中,不是这样!

        cocos2d-x重载了原本的lua的require加载方式。(见Cocos2dxLuaLoader.cpp )
        Cocos2dxLuaLoader逻辑的生效是在package.path之前,并且package.path在安卓上则不能很好的处理加载pkg包内部文件的问题。
        所以在实际使用中,我们只使用cocos2d-x重载的方法就可以了。

        怎么做呢?

        Cocos2dxLuaLoader内部是使用CCFileUtils::getFileData()来加载lua代码的。
        所以我们要想添加自己的lua脚本搜索路径,那么只要调用CCFileUtils::addSearchPath()就可以了。


        以下C++代码实现了在iOS和android平台上,程序先从下载路径下的scripts文件夹寻找lua文件,再从程序内置资源路径下的scripts文件夹寻找lua文件的目标。

#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




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值