Unity資源載入與釋放

2022-08-23 15:30:14 字數 1424 閱讀 4172

resources相當於unity乙個預設的assetbundle。

assetbundle可以在使用時動態載入。

resources.load();在沒有第一次instantiate之前沒有完全載入asset資源。所以相對assetbundle去例項乙個資源,resources會在第一次instantiate時出現卡頓現象。

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如果多次載入相同的資源,除第一次,其它都會返回第一次的物件。

來自為知筆記(wiz)

Unity資源載入與釋放 AssetBundle

resources相當於unity乙個預設的assetbundle。assetbundle可以在使用時動態載入。resources.load 在沒有第一次instantiate之前沒有完全載入asset資源。所以相對assetbundle去例項乙個資源,resources會在第一次instantia...

Unity 資源載入

本地載入 不能載入ab包 object obj resources.load 資源名稱或路徑下的名字 不需要副檔名 t t1 resources.load 資源名稱或路徑下的名字 不需要副檔名 表示載入resources目錄下指定的資源 object objs resources.loadall 資...

Unity載入資源方法

unity中的物體可以分為 資源和例項物件 兩種,一種是需要例項化出來的,一種是不需要例項化出來的,例如 動畫控制器 runtimeanimatorcontroller 精靈 sprite 音訊資源 audioclip 貼圖 texture2d 等.他們都是被unityengine 下的object...