Esempio n. 1
0
        public void UnloadAsset(string assetName)
        {
            UnityEngine.Object asset      = null;
            string             bundleName = null;

            if (!cachedAssets.TryGetValue(assetName, out asset) || !assetToBundleNameMap.TryGetValue(assetName, out bundleName))
            {
                Log.Error("[ResourceManager.UnloadAsset] {0} not loaded yet.", assetName);
            }
            if (asset is GameObject)
            {
                UnityEngine.Object.Destroy(asset);
            }
            else
            {
                Resources.UnloadAsset(asset);
            }
            cachedAssets.Remove(assetName);

            var loadedAb = AssetBundleLoader.GetLoadedAssetBundle(bundleName);

            if (loadedAb != null)
            {
                bool isUnloadBundle = true;
                var  allAssetNames  = loadedAb.assetBundle.GetAllAssetNames();
                for (int i = 0; i < allAssetNames.Length; ++i)
                {
                    if (cachedAssets.ContainsKey(allAssetNames[i]))
                    {
                        isUnloadBundle = false;
                        break;
                    }
                }
                if (isUnloadBundle)
                {
                    AssetBundleLoader.UnloadBundle(bundleName);
                }
            }
            else
            {
                Log.Error("[ResourceManager.UnloadAsset] assetbundle {0} has already unloaded.", bundleName);
            }
            assetToBundleNameMap.Remove(assetName);
        }