public T LoadBundleAssets <T>(string bundleName, string assetName) where T : Object { string bundleResName = bundleName; if (Path.GetExtension(bundleName) != ".data") { bundleResName = bundleName + ".data"; } bundleResName = bundleResName.ToLower(); BundleLoaderSharedObject blso; if (!mBundleLoaderSharedObjectDict.TryGetValue(bundleResName, out blso)) { blso = new BundleLoaderSharedObject(); if (blso.Load(bundleResName, false)) { mBundleLoaderSharedObjectDict.Add(bundleResName, blso); } else { return(null); } } if (blso != null) { return(blso.AssetObject(assetName) as T); } return(null); }
//异步加载资源,首先加载依赖项 public void LoadAssetAsyncBundle(string bundleName, LoadCallBackHandler loadCallBack, HandlerParam HP, bool loadAssets = true, bool isScene = false) { if (mAssetBundleManifest == null) { HP.isSucess = false; if (loadCallBack != null) { loadCallBack(HP); } return; } bundleName = HP.fullAssetName; if (Path.GetExtension(HP.fullAssetName) != ".data") { bundleName = HP.fullAssetName + ".data"; } bundleName = bundleName.ToLower(); LoadAssetBundleDependenciesAsync(bundleName); BundleLoaderSharedObject blso; bool isHave = mBundleLoaderSharedObjectDict.TryGetValue(bundleName, out blso); if (!isHave) { blso = new BundleLoaderSharedObject(); mBundleLoaderSharedObjectDict.Add(bundleName, blso); ResourceCallBackHander resCallBack = ResourceLoadCall; if (string.IsNullOrEmpty(bundleName)) { int nn = 0; } BundleManager.Instance.StartCoroutine(blso.LoadAsync(this, blso, bundleName, loadCallBack, resCallBack, HP, loadAssets)); return; } if (blso != null) { if (blso.mAssetBundle == null) { return; } HP.isSucess = true; if (isScene == false) { HP.assetObj = blso.AssetObject(true); } if (loadCallBack != null) { loadCallBack(HP); } return; } }
public void UnloadAllAssetBundle() { Dictionary <string, BundleLoaderSharedObject> .Enumerator etor = mBundleLoaderSharedObjectDict.GetEnumerator(); while (etor.MoveNext()) { BundleLoaderSharedObject blso = etor.Current.Value; if (blso != null) { blso.Release(); } } mBundleLoaderSharedObjectDict.Clear(); }
//同步加载资源 public UnityEngine.Object LoadAssetBundle(string bundleName, out UnityEngine.Object obj, bool loadAsset = true) { if (mAssetBundleManifest == null || string.IsNullOrEmpty(bundleName)) { obj = null; return(obj); } string bundleResName = bundleName; if (Path.GetExtension(bundleName) != ".data") { bundleResName = bundleName + ".data"; } bundleResName = bundleResName.ToLower(); //check dependant LoadAssetBundleDependencies(bundleResName); BundleLoaderSharedObject blso; if (!mBundleLoaderSharedObjectDict.TryGetValue(bundleResName, out blso)) { blso = new BundleLoaderSharedObject(); if (blso.Load(bundleResName, loadAsset)) { mBundleLoaderSharedObjectDict.Add(bundleResName, blso); } else { obj = null; return(obj); } } if (blso != null) { obj = blso.AssetObject(loadAsset); return(obj); } obj = null; return(obj); }
//依赖项递归查找,查找所有依赖项 private void LoadAssetBundleDependenciesAsync(string bundleResName) { string[] dependBundles = mAssetBundleManifest.GetAllDependencies(bundleResName); int dependCount = dependBundles.Length; for (int i = 0; i < dependCount; ++i) { if (string.IsNullOrEmpty(dependBundles[i])) { continue; } if (mBundleLoaderSharedObjectDict.ContainsKey(dependBundles[i]) == false) { BundleLoaderSharedObject blso = new BundleLoaderSharedObject(); mBundleLoaderSharedObjectDict.Add(dependBundles[i], blso); LoadAssetBundleDependenciesAsync(dependBundles[i]); BundleManager.Instance.StartCoroutine(blso.LoadAsync(this, blso, dependBundles[i], null, null, null, false)); } } }
public IEnumerator LoadAsync(BundleLoader bundleloader, BundleLoaderSharedObject blso, string bundleResName, LoadCallBackHandler loadCallBack, ResourceCallBackHander resCallBack, HandlerParam HP, bool loadAssets) { mBundleName = bundleResName; AssetBundleCreateRequest bundleRequest; string path = BundleLoader.LoadBundleAsyncFromSDCard(mBundleName); if (File.Exists(path)) { bundleRequest = AssetBundle.LoadFromFileAsync(path); yield return(bundleRequest); mAssetBundle = bundleRequest.assetBundle; } if (mAssetBundle == null) { #if UNITY_ANDROID && !_TEST if (BundleLoader.mStreamBundleList.Contains(mBundleName)) #endif { bundleRequest = BundleLoader.LoadBundleAsyncFromStreamingAssets(mBundleName); yield return(bundleRequest); mAssetBundle = bundleRequest.assetBundle; } #if UNITY_ANDROID if (mAssetBundle == null) { bundleRequest = BundleLoader.LoadBundleAsyncFromApkStreamingAssets(mBundleName); yield return(bundleRequest); mAssetBundle = bundleRequest.assetBundle; } #endif if (mAssetBundle == null) { // DebugConsole.sendmsgServer(mBundleName + "not loaded!"); bundleloader.RemoveAssetBundle(mBundleName); BundleManager.Instance.StartCoroutine(BundleLoader.LoadBundleFromWWW(bundleResName)); } } int startIdx = mBundleName.LastIndexOf("/"); int endIdx = mBundleName.LastIndexOf("."); try { assName = mBundleName.Substring(startIdx + 1, endIdx - startIdx - 1); } catch (System.Exception e) { } if (loadAssets && loadCallBack != null) { mMainObject = mAssetBundle.LoadAsset(assName); } if (resCallBack != null) { resCallBack(blso, bundleResName, mAssetBundle, loadCallBack, HP, loadAssets); } }