/// <summary> /// 依据name加载Assetbundle文件,并使用对象池存储AssetBundleItem /// </summary> /// <param name="name"></param> /// <returns></returns> private AssetBundle LoadAssetBundle(string name) { AssetBundleItem item = null; uint crc = CRC32.GetCRC32(name); if (!m_AssetBundleItemDic.TryGetValue(crc, out item)) { string fullPath = Application.streamingAssetsPath + "/" + name; AssetBundle assetBundle = null; if (File.Exists(fullPath)) { assetBundle = AssetBundle.LoadFromFile(fullPath); } if (assetBundle == null) { Debug.Log("Load AssetBundle Error assetbundle is null:" + fullPath); } item = m_AssetBundleItemPool.Spawn(true); item.AssetBundle = assetBundle; m_AssetBundleItemDic.Add(crc, item); } item.RefCount++; return(item.AssetBundle); }
/// <summary> /// 通过crc释放ab包,并通过对象池回收assetbundleitem资源 /// </summary> /// <param name="name"></param> private void UnLoadAssetBundle(string name) { AssetBundleItem item = null; uint crc = CRC32.GetCRC32(name); if (m_AssetBundleItemDic.TryGetValue(crc, out item) && item != null) { item.RefCount--; if (item.RefCount <= 0 && item.AssetBundle != null) { item.AssetBundle.Unload(true); item.Reset(); m_AssetBundleItemPool.Recycle(item); m_AssetBundleItemDic.Remove(crc); } } }