Unity AssetBundle方面

AssetBundle加载API概述

AssetBundle加载API概述.png

1.生成AssetBundle

查看:https://www.jianshu.com/p/d1455bd43b2f

2.UnityWebRequest下载AssetBundle

一般我们把下载写在using中,这样可以避免疏忽某些引用导致内存无法释放
把某文件下载到本地(可以用UnityAction回调下载完成)
    public IEnumerator DownloadAsset()
    {
        string uri = @"www.test.com\downloade\testAsset.ab";
        //写入到本地路径+名字
        string localAssetPath = @"D:\asset\testAsset.ab";

        string directoryName = Path.GetDirectoryName(localAssetPath);
        if (!Directory.Exists(directoryName))
        {
            Debug.Log("创建路径:" + directoryName);
            Directory.CreateDirectory(directoryName);
        }
        //https://docs.unity3d.com/cn/2019.4/ScriptReference/Networking.UnityWebRequest.html
        using (UnityWebRequest unityWeb = UnityWebRequestAssetBundle.GetAssetBundle(uri))
        {
            //把下载的文件直接写入到本地,下载完成之后才能读取此文件
            DownloadHandlerFile downloadHandler = new DownloadHandlerFile(localAssetPath);
            //如果下载被中止(手动中止或因出现错误而中止),则应删除创建的文件
            downloadHandler.removeFileOnAbort = true;

            unityWeb.downloadHandler = downloadHandler;

            unityWeb.SendWebRequest();

            Debug.Log("开始下载" + uri);

            while (!unityWeb.isDone)
            {
                Debug.Log("下载进度:" + unityWeb.downloadProgress);
                yield return null;
            }
            
            if (unityWeb.error != null)
            {
                Debug.Log("下载失败:" + unityWeb.error);
            }
            else
            {
                Debug.Log("下载成功:" + localAssetPath);
            }
        }
    }

3.加载本地AssetBundle

在加载ab包的时候,要注意加载依赖资源
LoadFromFile同步加载
        AssetBundle testAB = AssetBundle.LoadFromFile(@"E:\testAsset.ab");
        GameObject obj = testAB.LoadAsset<GameObject>("MainView");
LoadFromFileAsync异步加载
    IEnumerator LoadAssetBundle()
    {
        //AssetBundleCreateRequest assetBundleCreate = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, "testAsset.ab"));
        AssetBundleCreateRequest assetBundleCreate = AssetBundle.LoadFromFileAsync(@"E:\testAsset.ab");

        yield return assetBundleCreate;

        var myLoadedAssetBundle = assetBundleCreate.assetBundle;
        if (myLoadedAssetBundle != null)
        {
            //这里还要加载依赖资源
            //可以ScriptableObject自己生成一份依赖文件

            GameObject wallPrefab = myLoadedAssetBundle.LoadAsset<GameObject>("MainView");
            var obj = Instantiate(wallPrefab);
            obj.transform.SetParent(_transform);
            obj.transform.localScale = Vector3.one;
            obj.transform.localPosition = Vector3.zero;
        }
    }

3.资源的卸载,链接:https://zhuanlan.zhihu.com/p/265455416

AssetBundle(LoadFromFile)
  没有引用时,需要.Unload(true)
Asset(LoadAsset)
  1.GameObject类型,需要GameObject.Destroy
  2.其他类型Resources.UnloadAsset
Instantiate
  需要GameObject.Destroy
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容