Unity AssetBundle 学习

AssetBundle

AssetBundle是Unity用户存储资源的一种压缩格式的打包集合,他可以存任意一种Unity引擎可以识别的资源: 模型,音频,纹理图,动画, 开发者自定义的二进制文件; 安装包小,更新资源;

AssetBundle开发步骤

(1): 创建AssetBundle: 项目的资源打包AssetBundle的集合里面;
(2): 部署到web服务器, 让客户端下载我们的AssetBundle;
(3): 加载AssetBundle, 加载里面的资源;
(4): 卸载AssetBundle, 压缩包,镜像;

AssetBundle创建

1:Assets窗口的资源才可以打包;
2: 创建一个AssetBundle文件,它的名字固定式小写如果有大写系统也会换成小写;
3: AssetBundle可以设置一个Varaint,就是一个后缀。可以通过后缀来设置不同分辨率的资源;
4: 将一个资源打入到AssetsBundle: 点击资源,选择对应的AssetBundle就可以了;在这里插入图片描述
在这里插入图片描述
5: 编写代码导出AssetBundle文件,调用Unity的Api: BuildPipeline.BuildAssetBundles(outpath, BuildAssetBundleOptions, BuildTarget); 文件夹的路径需要手动创建,否者会报错;在这里插入图片描述

public class export_assetbundle : Editor {
    [MenuItem("build/build_assetbundle")]
    static void run() { 
        // 调用函数来打包asset bundle;
        // 输出路径: "Assets/AssetBundles", 手动创建好
        BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.None, 			BuildTarget.StandaloneWindows64);
        // end 

        /*     第二种打包方式
         AssetBundleBuild[] buildMap = new AssetBundleBuild[2];    //定义AssetBuild数组
        buildMap[0].assetBundleName = "resources";                //打包的资源包名称,开发者可以随便命名
        string[] resourcesAssets = new string[2];                  //定义字符串,用来记录此资源包文件名称
        resourcesAssets[0] = "resources/1.prefab";                 //将需要打包的资源名称赋给数组
        resourcesAssets[1] = "resources/MainO.cs";
        buildMap[0].assetNames = resourcesAssets;                  //将资源名称数组赋给AssetBuild   
        BuildPipeline.BuildAssetBundles("Assets/AssetBundles", buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64)
         */
    }
}

在这里插入图片描述
在这里插入图片描述
这里包含了资源的引用和一些基本信息。

AssetBundle下载

把我们打包好的资源放到web服务器上,服务器提供一个URL,用WWW实例下载AssetBundle

1:非缓冲下载: 创建一个WWW的实例来下载AssetBundle;
(1)使用协程下载AssetBundle,
(2) 使用WWW的URL接口来下载;

2: 缓冲下载:使用WWW类的LoadFromCacheOrDownload来实现下载AssetBundle, 当再次下载的时候,只有当版本低或不存在的时候才下载;web平台缓冲的大小是50M, IOS/android缓冲的大小为4GB;

using UnityEngine;
using System.Collections;

public class game_scene : MonoBehaviour {
    public string url;
    public int version; // 自己控制的,
	// Use this for initialization
	void Start () {
        // this.StartCoroutine(this.nocache_load());
        this.StartCoroutine(this.cache_load());
	}

    //下载assetbundle,不缓存在本地
    IEnumerator nocache_load() {
        WWW w = new WWW(this.url);
        yield return w;

        // 下载完成;
        Debug.Log("download end");
        if (w.error != null) {
            Debug.Log(w.error);
        }
        // 内存镜像, assetbundle, 
        AssetBundle bundle = w.assetBundle;
        Debug.Log("download success...");
        // end 

        // 使用里面的资源

        Object prefab = bundle.LoadAsset("Assets/res/Cube.prefab");
        GameObject obj = (GameObject)Instantiate(prefab);
        obj.transform.parent = this.transform;
        // end 

        // 协助AssetBundle镜像, 压缩镜像
        bundle.Unload(false); // 只会卸载assetbundle内存镜像
        // bundle.Unload(true); // 会卸载内存镜像,也会释放掉从这个assetbunle里面加载起来的资源;
        // end 
    }

    IEnumerator cache_load() {
        WWW w = WWW.LoadFromCacheOrDownload(this.url, this.version);
        yield return w;
        // 下载完成;
        Debug.Log("download end");
        if (w.error != null) {
            Debug.Log(w.error);
        }
        // 内存镜像, assetbundle, 
        AssetBundle bundle = w.assetBundle;
        Debug.Log("download success...");
        // end 

        // 使用里面的资源

        Object prefab = bundle.LoadAsset("Assets/res/Cube.prefab");
        GameObject obj = (GameObject)Instantiate(prefab);
        obj.transform.parent = this.transform;
        // end 

        // 协助AssetBundle镜像, 压缩镜像
        bundle.Unload(false); // 只会卸载assetbundle内存镜像
        // bundle.Unload(true); // 会卸载内存镜像,也会释放掉从这个assetbunle里面加载起来的资源;
        // end 
    }
    // 下载assetbundle, nocache, cache, version
    // end 
	// Update is called once per frame
	void Update () {
	
	}
}

简单说明一些:
AssetBundle加载
1:AssetBundle.LoadAsset(路径); // 资源所在的路径名字
3: AssetBundle.LoadAllAsset()加载所有的资源

AssetBundle卸载
1:AssetBoundle.Unload(false); false: 卸载内存镜像不卸载Asset内存实例;
2:AssetBoundle.Unload(true); true: 卸载内存镜像以及Asset的内存实例;
3: 不能使用WWW对象去下载一个已经被加载进来的AssetBundle;

案例源码下载

发布了37 篇原创文章 · 获赞 11 · 访问量 6175

猜你喜欢

转载自blog.csdn.net/weixin_42422809/article/details/90242749