首页 > 技术文章 > AssetBundle 复习

revoid 2021-02-09 08:28 原文

https://docs.unity.cn/cn/current/ScriptReference/AssetBundle.html

https://docs.unity.cn/cn/current/ScriptReference/BuildPipeline.html

https://docs.unity.cn/cn/current/ScriptReference/BuildAssetBundleOptions.html

 

 

AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/2mainmenu");

private IEnumerator IE_Start() {
    AssetBundleCreateRequest ab = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/2mainmenu");
        
    while(!ab.isDone) {
        Debug.Log(ab.progress);
        yield return null;
    }

    if (ab.isDone) {
        Debug.Log(ab.progress);
    }
}
LoadFromFile&LoadFromFileAsync
private IEnumerator IE_Start() {
    WWW www = new WWW(Application.streamingAssetsPath + "/2mainmenu");

    while (!www.isDone) {
        Debug.Log(www.progress);
        yield return null;
    }

    AssetBundle ab = AssetBundle.LoadFromMemory(www.bytes);

    Debug.Log(ab.name);
}

private IEnumerator IE_Start() {
    WWW www = new WWW(Application.streamingAssetsPath + "/2mainmenu");

    while (!www.isDone) {
        Debug.Log(www.progress);
        yield return null;
    }

    AssetBundleCreateRequest ab = AssetBundle.LoadFromMemoryAsync(www.bytes);

    while (!ab.isDone) {
        yield return null;
    }

    if (ab.isDone) {
        Debug.Log(ab.assetBundle.name);
    }
}
LoadFromMemory&LoadFromMemoryAsync
var fileStream = new FileStream(Application.streamingAssetsPath + "/2mainmenu", FileMode.Open, FileAccess.Read);
var ab = AssetBundle.LoadFromStream(fileStream);

var fileStream = new FileStream(Application.streamingAssetsPath + "/2mainmenu", FileMode.Open, FileAccess.Read);
AssetBundleCreateRequest ab = AssetBundle.LoadFromStreamAsync(fileStream);

while (!ab.isDone) {
    yield return null;
}

if (ab.isDone) {
    Debug.Log(ab.assetBundle.name);
}
LoadFromStream&LoadFromStreamAsync
AssetBundle ab =  AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/2mainmenu");
        

// Texture2D
var asset = ab.LoadAsset("bg_Title");
// sprite
asset = ab.LoadAsset("bg_Title", typeof(Sprite));
// sprite
asset = ab.LoadAsset<Sprite>("bg_Title");
// Texture2D
asset = ab.LoadAsset("Assets/Test/2MainMenu/bg_Title.png");
// Texture2D
asset = ab.LoadAsset("assets/test/2mainmenu/bg_title.png");
// null
asset = ab.LoadAsset("Assets/Test/2MainMenu/bg_Title");
// null
asset = ab.LoadAsset("assets/test/2mainmenu/bg_title");
// null
asset = ab.LoadAsset("assets/test/2mainmenu/bg_title", typeof(Sprite));

var assets = ab.LoadAllAssets();
// e.g. 46
Debug.Log(assets.Length);
foreach (var item in assets) {
    // e.g. bg_Title
    Debug.Log(item.name);
}

var assetsName = ab.GetAllAssetNames();
// e.g 23   说明有些asset没有名字, 比如 sprite
Debug.Log(assetsName.Length);
foreach (var item in assetsName) {
    // e.g. assets/test/2mainmenu/bg_title.png
    Debug.Log(item);
}
LoadAsset相关
// 将卸载捆绑包中资源的压缩文件数据, 但已从该捆绑包中加载的任何实际对象将保持不变。当然,您将无法再从该捆绑包加载任何其他对象
ab.Unload(false);
// 也将销毁从该捆绑包加载的所有对象。如果场景中有游戏 对象引用这些资源,则对它们的引用将丢失
ab.Unload(true);
Unload

 

推荐阅读