public void UnLoadAssetBundle(string abName) { AssetBundleItem abItem = null; uint crc = CrcHelper.StringToCRC32(abName); if (m_assetBundleItemDict.TryGetValue(crc, out abItem) && abItem != null) { abItem.refCount--; if (abItem.refCount <= 0 && abItem.assetBundle != null) { abItem.assetBundle.Unload(true); abItem.Reset(); m_assetBundleItemPool.Recycle(abItem); m_assetBundleItemDict.Remove(crc); } } }
//异步加载资源协程 IEnumerator AsyncLoadAssetCoroutine() { List <AsyncCallBack> callbackList = null; long lastYieldTime = System.DateTime.Now.Ticks; while (true) { bool haveYield = false; for (int i = 0; i < (int)LoadAssetPriority.NUM; i++) { List <AsyncLoadAssetParam> loadingList = m_loadingAssetList[i]; if (loadingList.Count <= 0) { continue; } //取出第一个要加载的资源参数 AsyncLoadAssetParam param = loadingList[0]; loadingList.RemoveAt(0); callbackList = param.m_callbackList; Object obj = null; ResourceItem item = null; #if UNITY_EDITOR if (!m_loadFromAssetBundle) { obj = LoadAssetByEditor <Object>(param.m_path); //模拟异步加载 yield return(new WaitForSeconds(0.5f)); item = AssetBundleManager.Instance.FindResourceItem(param.m_crc); } #endif if (obj == null) { item = AssetBundleManager.Instance.LoadResourceItem(param.m_crc); if (item != null && item.Ab != null) { AssetBundleRequest request = null; if (param.m_Sprite) //图片资源需要特殊处理,因为object不能转Sprite { request = item.Ab.LoadAssetAsync <Sprite>(item.AssetName); } else { request = item.Ab.LoadAssetAsync <Object>(item.AssetName); } yield return(request); if (request.isDone) { obj = request.asset; } lastYieldTime = System.DateTime.Now.Ticks; } } CacheAsset2AssetCache(param.m_path, ref item, param.m_crc, obj, callbackList.Count); for (int j = 0; j < callbackList.Count; j++) { AsyncCallBack callback = callbackList[j]; if (callback != null && callback.m_gameObjDealFinished != null) { GameObjectItem gameObjectItem = callback.m_gameObjectItem; gameObjectItem.ResourceItem = item; callback.m_gameObjDealFinished(param.m_path, gameObjectItem, gameObjectItem.Param1, gameObjectItem.Param2, gameObjectItem.Param3); callback.m_gameObjDealFinished = null; } if (callback != null && callback.m_resourceObjDealFinished != null) { callback.m_resourceObjDealFinished(param.m_path, obj, callback.m_param1, callback.m_param2, callback.m_param3); callback.m_resourceObjDealFinished = null; } callback.Reset(); m_asyncCallBackPool.Recycle(callback); } obj = null; callbackList.Clear(); m_loadingAssetDict.Remove(param.m_crc); param.Reset(); m_asyncLoadAssetParamPool.Recycle(param); if (System.DateTime.Now.Ticks - lastYieldTime > MAXLOADASSETTIME) { yield return(null); lastYieldTime = System.DateTime.Now.Ticks; haveYield = true; } } //下边的yield return null, 作用于没有异步加载资源请求时 if (!haveYield || System.DateTime.Now.Ticks - lastYieldTime > MAXLOADASSETTIME) { lastYieldTime = System.DateTime.Now.Ticks; yield return(null); } } }
//卸载资源 public void ReleaseGameObjectItem(GameObject obj, int maxCacheCount = -1, bool destoryCache = false, bool recycleParent = true) { if (obj == null) { return; } GameObjectItem gameObjectItem = null; int tempGuid = obj.GetInstanceID(); if (!m_gameObjectItemDict.TryGetValue(tempGuid, out gameObjectItem) || gameObjectItem == null) { Debug.Log(obj.name + "并非对象池技术创建,不能回收到对象池!"); return; } if (gameObjectItem.AlreadyRelease) { Debug.Log(obj.name + "该对象已经放回对象池, 检查是否清空该对象的引用!"); return; } #if UNITY_EDITOR obj.name += "(Recycle)"; #endif if (maxCacheCount == 0) //表示不缓存 { m_gameObjectItemDict.Remove(tempGuid); GameObject.Destroy(gameObjectItem.Obj); ResourceMgr.Instance.UnLoadGameObjectItem(gameObjectItem, destoryCache); gameObjectItem.Reset(); m_gameObjectItemClassPool.Recycle(gameObjectItem); } else { //回收到对象池 List <GameObjectItem> list = null; if (!m_gameObjectItemPoolDict.TryGetValue(gameObjectItem.Crc, out list) || list == null) { list = new List <GameObjectItem>(); m_gameObjectItemPoolDict.Add(gameObjectItem.Crc, list); } if (gameObjectItem.Obj) { if (recycleParent) { gameObjectItem.Obj.transform.SetParent(m_goPool); } else { gameObjectItem.Obj.SetActive(false); } } if (maxCacheCount < 0 || list.Count < maxCacheCount) //<0表示可以无限缓存, <maxCacheCount表示需要放入缓存 { list.Add(gameObjectItem); gameObjectItem.AlreadyRelease = true; ResourceMgr.Instance.DecrementResourceRef(gameObjectItem, 1); } else //不需要缓存GameObject到对象池 { m_gameObjectItemDict.Remove(tempGuid); GameObject.Destroy(gameObjectItem.Obj); ResourceMgr.Instance.UnLoadGameObjectItem(gameObjectItem); gameObjectItem.Reset(); m_gameObjectItemClassPool.Recycle(gameObjectItem); } } }