參考資源
http://www.cnblogs.com/freebird92/archive/2013/03/12/2955888.html
http://www.taidous.com/bbs/article-445-1.html
AssetBundle 與 Resources的區別
Resources相當於Unity一個缺省的AssetBundle。
AssetBundle可以在使用時動態加載。
Resources.load();在沒有第一次Instantiate之前沒有完全加載Asset資源。所以相對AssetBundle去實例一個資源,Resources會在第一次Instantiate時出現卡頓現象。
AssetBundle 加載與釋放
AssetBundle的加載方式有:AssetBundle.LoadFromFile(“”); AssetBundle.LoadFromMemorty(byte[]);及WWW方式。
AssetBunddle.LoadFromFile(“”);只有在standalone上可以使用。WWW在加載AssetBundle時,會在內部創建assetBundle實例。
當AssetBundle被加載到內存后,其實內存中只有AssetBundle文件的鏡像內存。//todo 該鏡像文件是AssetBundle原文件大小?
當需要具體某個Prefab等資源時,使用assetbundle.Load(“”);加載具體資源。此時內存中會存在此資源的 GameObject、shaders、材質、貼圖等內存。當具體Instantiate一個資源時,會對此資源的Asset資源進行clone+引用操作。Clone的資源包括GameObject、Tranform。引用的資源包括Texture、TerrainData、Shader。引用與Clone同時存在的包括 Mesh、material、PhysicMaterial、noxss。引用的資源只是指針的指向。Clone的會重新生成新的內存。
根據AssetBundle資源的加載方式,當資源釋放時,如果只是Destroy,只會釋放生成這個資源時Clone的那部分內存。assetBundle.Load(“”);加載的Asset內存沒有被釋放,AssetBundle的鏡像內存也沒有被釋放。所以如果在該資源被銷毀時需要使用Resources.UnloadUnuseAssets();去釋放沒有被使用的Asset資源內存。如果該AssetBundle不會被再使用可以使用AssetBundle.Unload(true)銷毀內存。AssetBundle(true);代表銷毀該AssetBundle鏡像內存及所加載的Asset內存。參數為false時,代表只銷毀鏡像內存。
下面的圖片可以更形象的理解AssetBundle的加載與釋放。
備注
當場景在切換時,Unity 會自動 把托管的內存釋放,這其中包括Asset內存、Instantiate內存。但不會被釋放的是AssetBundle鏡像內存。
assetBundle.Load如果多次加載相同的資源,除第一次,其它都會返回第一次的對象。