单一AssetBundle包加载与管理

一、AssetLoader(AB包内资源加载):完成AB包内存资源加载、(包内)资源缓存处理、卸载与释放AB包、查看当前AB包内资源。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace ABFW
{
    public class AssetLoader :System.IDisposable
    {
        //当前Assetbundle
        private AssetBundle _CurrentAssetBundle;
        //缓存容器集合
        private Hashtable _Ht;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="abObj">给定www加载的AssetBundle实例</param>
        public AssetLoader(AssetBundle abObj)
        {
            if (abObj!=null)
            {
                _CurrentAssetBundle = abObj;
                _Ht = new Hashtable();
            }
            else
            {
                Debug.Log(GetType()+"/构造函数AssetBundle()/参数abObj==null!,请检查");
            }
        }
        /// <summary>
        /// 加载当前AB包的资源,带缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="assetName">资源的名称</param>
        /// <param name="isCeshe">是否需要缓存处理</param>
        /// <returns></returns>
        private T LoadResource<T>(string assetName,bool isCeshe) where T:UnityEngine.Object
        {
            //是否缓存集合已经存在
            if (_Ht.Contains(assetName))
            {
                return _Ht[assetName] as T;
            }
            //正式加载
            T tmpTResoure = _CurrentAssetBundle.LoadAsset<T>(assetName);
            //加入缓存集合
            if (tmpTResoure!=null &&isCeshe)
            {
                _Ht.Add(assetName, tmpTResoure);
            }
            else if (tmpTResoure == null)
            {
                Debug.Log(GetType() + "/LoadResoure<T>()/参数 tmpTResoure==null,请检查");
            }
            return tmpTResoure;
        }
        /// <summary>
        /// 加载包中指定的资源
        /// </summary>
        /// <param name="assetName">资源的名称</param>
        /// <param name="isCache">是否开启缓存</param>
        /// <returns></returns>
        public UnityEngine.Object LoadAsset(string assetName, bool isCache = false)
        {
            return LoadResource<UnityEngine.Object>(assetName, isCache);
        }
        /// <summary>
        /// 卸载指定的资源
        /// </summary>
        /// <param name="asset">要卸载的资源</param>
        /// <returns></returns>
        public bool UnLoadAsset(UnityEngine.Object asset)
        {
            if (asset!=null)
            {
                Resources.UnloadAsset(asset);
                return true;
            }
            Debug.LogError(GetType() + "/UnLoadAsset()/参数 asset==null,请检查");
            return false;
        }
        /// <summary>
        /// 释放当前AB内存镜像资源
        /// </summary>
        public void Dispose()
        {
            _CurrentAssetBundle.Unload(false);
        }
        /// <summary>
        /// 释放当前AB内存镜像资源,且释放内存资源
        /// </summary>
        public void DisposeAll()
        {
            _CurrentAssetBundle.Unload(true);
        }
        /// <summary>
        /// 查询当前AssetBundle中包含的所有资源名称。
        /// </summary>
        /// <returns></returns>
        public string[] RetriveAllAssetName()
        {
            return _CurrentAssetBundle.GetAllAssetNames();
        }
    }
}

二、SingleABLoader(WWW加载AB包):完成WWW加载、定义(加载完毕)回调函数、以及通过引用AssetLoader脚本调用卸载与释放AB包、查看当前AB包内资源等。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace ABFW
{
    public class SingleABLoader :System.IDisposable
    {
        //引用类:资源加载类
        private AssetLoader _AssetLoader;
        //委托:
        private DelLoadComplete _LoadCompleteHandle;

        //AssetBundle名称:
        private string _ABName;
        //AssetBundle下载的路径
        private string _ABDownLoadPath;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="abName"></param>
        public SingleABLoader(string abName,DelLoadComplete loadComplete)
        {
            _AssetLoader = null;
            _ABName = abName;
            //委托定义
            _LoadCompleteHandle = loadComplete;
            //AB包下载路径
            _ABDownLoadPath = PathTools.GetWWWPath()+"/"+ _ABName;
      
        }

        /// <summary>
        /// 加载AssetBundle资源包
        /// </summary>
        /// <returns></returns>
        public IEnumerator LoadAssetBundle()
        {
            
            using (WWW www=new WWW(_ABDownLoadPath))
            { 
                yield return www;
                //WWW下载AB包完成
                if (www.progress >= 1)
                {
                    //获取AssetBundle的实例
                    AssetBundle abObj = www.assetBundle;
                    if (abObj!=null)
                    {
                       
                        _AssetLoader = new AssetLoader(abObj);
                        //AssetBundle下载完毕,调用委托
                        if (_LoadCompleteHandle!=null)
                        {
                            _LoadCompleteHandle(_ABName);
                        }
                    }
                    else
                    {
                        Debug.LogError(GetType()+ "/LoadAssetBundle()/www 下载出错,请检查!AssetBundle URL:"+ _ABDownLoadPath);
                    }

                }
            }
        }

        /// <summary>
        /// 加载(AB包内)资源
        /// </summary>
        /// <param name="assetName"></param>
        /// <param name="isCache"></param>
        /// <returns></returns>
        public UnityEngine.Object LoadAsset(string assetName,bool isCache)
        {
            if (_AssetLoader!=null)
            {
                return _AssetLoader.LoadAsset(assetName,isCache);
            }
            Debug.LogError(GetType() + "/LoadAsset()/参数_AssetLoader==null,请检查");
            return null;
        }

        //卸载(AB包中)资源
        public void UnLoadAsset(UnityEngine.Object asset)
        {
            if (_AssetLoader!=null)
            {
                _AssetLoader.UnLoadAsset(asset);
            }
            else
            {
                Debug.LogError(GetType()+ "/UnLoadAsset()/参数_AssetLoader=Null,请检查");
            }
        }

        //释放资源
        public void Dispose()
        {
            if (_AssetLoader!=null)
            {
                _AssetLoader.Dispose();
                _AssetLoader = null;
            }
            else
            {
                Debug.LogError(GetType()+ "/Dispose()/参数 参数_AssetLoader=Null,请检查");
            }
        }


        //释放当前AssetBundle资源包,且卸载所有资源
        public void DisposeAll()
        {
            if (_AssetLoader != null)
            {
                _AssetLoader.DisposeAll();
                _AssetLoader = null;
            }
            else
            {
                Debug.LogError(GetType() + "/DisposeAll()/参数 参数_AssetLoader=Null,请检查");
            }

        }


        //查询资源
        public string[] RetrivalAllAssetName()
        {
            if (_AssetLoader != null)
            {
                return _AssetLoader.RetriveAllAssetName();
            }
            Debug.LogError(GetType() + "/RetrivalAllAssetName()/参数 参数_AssetLoader=Null,请检查");
            return null;
        }
       
    }
}

Unity AB包全面教学​pan.baidu.com

 

提取码:1ekr

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值