ios資源加載策略

作了好幾個月的ios,大框架都是別人搭好的,本身只是實現邏輯,非常失落。慢慢開始整理學習一些概念類的東西吧,但願本身能提升點。ios

cocos2d-x從cocos2d-2.0-x-2.0.2開始,考慮到本身是跨平臺的引擎,而且Android有許多不一樣的分辨率,因此再也不使用利用對圖片加後綴hd ipad ipadhd 進行加載的策略。app

本質上來講,資源的加載策略,就是爲了應對不一樣平臺的不一樣分辨率的。框架

新的加載策略以下:iphone

 從CCFileUtils的setResourceDirectory設置的目錄中去尋找。若是找不到則會在Resource/目錄下查找。分佈式

通常狀況下,咱們在AppDelegate.cpp的applicationDidFinishLaunching函數中設置此目錄。ide

 CCFileUtils::sharedFileUtils()->setResourceDirectory( "ipadhd" );

這樣當你建立一個精靈時:函數

CCSprite * test = CCSprite::create("abc.png");

Cocos2d-x會首先在Resources/ipadhd 目錄下查找abc.png文件,若是沒有,則回到Resources 目錄下尋找。
須要注意的是,使用setResourceDirectory設置時,默認父目錄是Resource。學習

若是你是這樣設置的測試

CCFileUtils::sharedFileUtils()->setResourceDirectory( "../ipadhd" );

那麼,Cocos2d-x會首先在和Resources同級的目錄ipadhd中查找文件,注意,是同級。若是沒有,則回到Resources 目錄下尋找。ui

有一個問題,不一樣的平臺咱們能夠設置不一樣的資源文件夾。可是,有可能咱們設置的查找資源文件夾多是多個,而setResourceDirectory沒法知足這個需求的。cocos2d-x官方管這個叫分佈式策略。

因此cocos2d-x從2.1開始,已經不支持setResourceDirectory方法了,而是用新的方法setSearchResolutionsOrder替代。這樣,咱們能夠根據不一樣的平臺,設置cocos2d查找文件的路徑爲多個。注意看這個方法的參數,是vector類型的,這樣知足了咱們的需求了。其查找側略仍是沒有變的。

virtual void setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder);

cocos2d-x文檔中對此方法的描述以下,是從http://blog.csdn.net/some_do/article/details/8914748 這裏借鑑過來的,我也沒有翻譯,不過很簡單,相信你們都能看懂:

1.1. Developer guilde

CCFileUtils::setSearchResolutionsOrder() is added to support distributed strategy. You can set searching resolutions order like this 

std::vector<std::string> resDirOrders;
if (resolution is ipad retina)
{
    resDirOrders.push_back("resources-ipadhd");
    resDirOrders.push_back("resources-ipad");
    resDirOrders.push_back("resources-iphonehd");
}
CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(resDirOrders);

After setting searching resolutions order, suppose we create a sprite like this

CCSprite *sprite = CCSprite::create("images/tex.png"); 

Engine will find tex.png in the following sequence
find it in images/resources-ipadhd/
if not found, find it in images/resources-ipad/
if not found, find it in images/resources-iphonehd/
if not found, find it in images/ (這個必須重點理解,實際上是把目標圖片的第一層父目錄指爲setSearchResolutionsOrder中設置的。)

1.2. Notes
This strategy is not suitable for multi-resolution adaption, because there are too many resolutions on Android. You can not provide all resources for all resolutions, then set searching order based on resolutions, such as

 std::vector<std::string> resDirOrders;
 if (resolution is reslution1)
 {
     resDirOrders->push_back("path1");
     resDirOrders->push_back("path2");
     ...
 }
 else if (resolution is resolution2)
 {
    resDirOrders->push_back("path-a");
    resDirOrders->push_back("path-b");
    ...
}
...

CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(resDirOrders);

2. Centralized strategy

2.1. Why use a different mechanism than cocos2d-iphone

Cocos2d-iphone uses -hd, -ipad, -ipadhd to determine which picture to load. This mechanism is good enough for iOS platform, but is not so suitable for Android, because it has many different resolutions. Cocos2d-x is a cross platform engine, so it should use another mechanism.

2.2. What is the new mechanism

Cocos2d-x uses the new mechanism to load a picture since version cocos2d-2.0-x-2.0.2. It does not use -hd, -ipad, -ipadhd suffixes to indicate images for different resolutions. The mechanism is: Try to find a picture in the paths set by CCFileUtils::setSearchPaths() firstly, if it's not found, then find the picture in Resources/

// set searching paths to "/mnt/sd/example" and "/data/data/org.cocos2dx.example" 
vector<string> searchPaths;
searchPaths.push_back("/mnt/sd/example");
searchPaths.push_back("/data/data/org.cocos2dx.example");
CCFileUtils::setSearchPaths(searchPaths);  
// engine will find "1.png" in /mnt/sd/example, if there it is not found, then engine will find "1.png" in /data/data/org.cocos2dx.example
// if not found, engine will find "1.png" in Resources/ (this path is platform dependent)
CCSprite *pSprite = CCSprite::create("1.png"); 

It is easy to add searching path to engine. Using this method, you can load resources into a path you know, then set this path to engine. Engine will find a resource in this path if needed.

2.3. Developer guide

Do not use -hd, -ipad, -ipadhd suffixes any more. Instead, put all hd files in a directory, then all ipad files in another directory, and so on, then set resource directory to tell the engine where to find a picture. If you want to share some resources between different resolutions, then you can put all shared resources in Resources/, and put resolution specified resources in different directories. You can refer to samples/HelloCpp for more information.

這個例子是我通過測試的:

 std::vector<std::string> pat;
    pat.push_back("i");
    CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(pat);
CCSprite * a = CCSprite::create("images/share/a.png");

若是此時,個人Resourecs/images/share/a.png是存在的,則不會報錯。若是不存在,則報錯。

若是 Resourecs/images/share/i/a.png是存在的,而Resourecs/images/share/a.png不存在,不會報錯。

 目前我正在作的項目,是沒有設置這個setSearchResolutionsOrder的。咱們在建立一個圖片精靈的時候直接使用:

CCSprite *m_pBackSprite = CCSprite::create( "Images/ipadhd/a.png" );

此時,cocos2d-x直接在Resources/Images/ipadhd/a.png 查找此文件,若是找不到,則會報錯


總結了一張圖:

 

相關文章
相關標籤/搜索