public SDictionary <int, UnityEngine.Object> LoadAll(string bundleName, ELoadType loadType) { if (string.IsNullOrEmpty(bundleName)) { return(null); } LoadedAssetBundle loaded = ABManager.Instance.GetLoadedBundle(bundleName.GetHashCode()); if (loaded != null) { loaded.Count(); } else { loaded = LoadSyn(bundleName); } if (loaded != null) { loaded.mLoadType = loadType; if (loaded.mAssetBundle != null) { loaded.MapAssets(); ABManager.Instance.AfterLoad(loaded); } return(loaded.GetAllAssets()); } return(null); }
/// <summary> /// 加载跟包文件的依赖文件 /// </summary> private void InitLocalManifest() { string bundleName = ABPathHelper.platformFolder; //跟包的ab依赖文件需要一个和下载的ab依赖文件不重名的名字,特加此后缀区分 string diffBundleName = bundleName + "diff"; int nHashCode = diffBundleName.GetHashCode(); LoadedAssetBundle loaded = ABManager.Instance.GetLoadedBundle(nHashCode); if (loaded != null) { loaded.Count(); return; } string strBundleUrl = ABPathHelper.AssetsLocalABURL + bundleName; AssetBundle ab = AssetBundle.LoadFromFile(strBundleUrl); if (ab != null) { loaded = ABManager.Instance.AddLoadedBundle(nHashCode, ab); if (loaded != null) { LocalABBundleManifest = ab.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); loaded.UnLoadBundle(false); LocalABBundleHashSet.Clear(); string[] assetBundles = LocalABBundleManifest.GetAllAssetBundles(); for (int i = 0; i < assetBundles.Length; i++) { LocalABBundleHashSet.Add(assetBundles[i]); } } } }
private IEnumerator LoadBundleByWWW(string bundleName, DependencsLoader callback, LoadFromType loadFromType, ELoadType loadType = ELoadType.Max) { if (string.IsNullOrEmpty(bundleName)) { yield break; } int nHashCode = bundleName.GetHashCode(); LoadedAssetBundle loaded = ABManager.Instance.GetLoadedBundle(nHashCode); if (loaded != null) { loaded.Count(); yield break; } if (!mLoadingReqs.Contains(nHashCode)) { mLoadingReqs.Add(nHashCode); string strBundleUrl = "file://"; if (loadFromType == LoadFromType.LoadFromDownLoad) { strBundleUrl += ABPathHelper.AssetsURL; } else { strBundleUrl = ABPathHelper.AssetsLocalABURL; } strBundleUrl += bundleName; WWW www = new WWW(strBundleUrl); if (www != null) { while (!www.isDone && string.IsNullOrEmpty(www.error)) { yield return(null); } if (string.IsNullOrEmpty(www.error)) { Debug.Log("<color=yellow>AddLoadedBundle:" + bundleName + "</color> " + Time.realtimeSinceStartup); ABManager.Instance.AddLoadedBundle(nHashCode, www.assetBundle); if (callback != null) { ++callback.mDoneCnt; } } www.Dispose(); } mLoadingReqs.Remove(nHashCode); } yield return(null); }
public Object Load(string bundleName, string assetName, ELoadType loadType) { if (string.IsNullOrEmpty(bundleName)) { return(null); } int nHashCode = bundleName.GetHashCode(); LoadedAssetBundle loaded = ABManager.Instance.GetLoadedBundle(nHashCode); if (loaded != null) { loaded.Count(); } else { loaded = LoadSyn(bundleName); } if (loaded != null) { loaded.mLoadType = loadType; Object asset = null; if (loaded.mAssetBundle != null) { AssetBundle ab = loaded.mAssetBundle; if (string.IsNullOrEmpty(assetName)) { asset = ab.mainAsset != null ? ab.mainAsset : ab.LoadAsset(ab.GetAllAssetNames()[0]); if (asset != null) { loaded.SetMainAsset(asset); } } else { loaded.MapAssets(); asset = loaded.LoadAsset(assetName); } ABManager.Instance.AfterLoad(loaded); //Debug.Log("<color=yellow>assetName:" + assetName + "</color>"); } else { asset = loaded.LoadAsset(assetName); //Debug.Log("<color=red>assetName:"+ assetName + "</color>"); } return(asset); } return(null); }
private LoadedAssetBundle LoadBundleByFileStream(string bundleName, LoadFromType loadFromType, AssetEncryptMode encript = AssetEncryptMode.None) { if (string.IsNullOrEmpty(bundleName)) { return(null); } int nHashCode = bundleName.GetHashCode(); LoadedAssetBundle loaded = ABManager.Instance.GetLoadedBundle(nHashCode); if (loaded != null) { loaded.Count(); return(loaded); } //在对应目录里进行AB创建 m_strFullName.Length = 0; if (loadFromType == LoadFromType.LoadFromDownLoad) { m_strFullName.Append(ABPathHelper.AssetsURL); } else { m_strFullName.Append(ABPathHelper.AssetsLocalABURL); } m_strFullName.Append(bundleName); string path = m_strFullName.ToString(); if (File.Exists(path)) { AssetBundle ab = AssetBundle.LoadFromFile(path); if (ab != null) { loaded = ABManager.Instance.AddLoadedBundle(nHashCode, ab); } } else { if (GameUtils.OnlyShowInInner) { Debug.Log("<color=red>资源: " + m_strFullName + "不存在</color>"); } } return(loaded); }
/// <summary> /// 加载下载文件的依赖文件 /// </summary> private void InitManifest() { string bundleName = ABPathHelper.platformFolder; int nHashCode = bundleName.GetHashCode(); LoadedAssetBundle loaded = ABManager.Instance.GetLoadedBundle(nHashCode); if (loaded != null) { loaded.Count(); return; } string strBundleUrl = ABPathHelper.AssetsURL + bundleName; if (File.Exists(strBundleUrl)) { AssetBundle ab = AssetBundle.LoadFromFile(strBundleUrl); if (ab != null) { loaded = ABManager.Instance.AddLoadedBundle(nHashCode, ab); if (loaded != null) { AssetBundleManifest = ab.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); loaded.UnLoadBundle(false); AssetBundleHashSet.Clear(); string[] assetBundles = AssetBundleManifest.GetAllAssetBundles(); for (int i = 0; i < assetBundles.Length; i++) { AssetBundleHashSet.Add(assetBundles[i]); } } } } else { Debug.Log("本地未找到bundle: " + strBundleUrl); } }
public IEnumerator LoadAsync(string strFullName) { if (AssetBundleManifest == null || string.IsNullOrEmpty(strFullName)) { yield break; } InitAsync(); int nHashCode = strFullName.GetHashCode(); LoadedAssetBundle loaded = ABManager.Instance.GetLoadedBundle(nHashCode); if (loaded != null) { loaded.Count(); yield break; } if (mLoadingReqs.Contains(nHashCode)) { yield break; } LoadFromType loadFromType = LoadFromType.LoadFromNone; string[] dependencies = null; if (AssetBundleManifest != null && AssetBundleHashSet.Contains(strFullName)) { loadFromType = LoadFromType.LoadFromDownLoad; dependencies = AssetBundleManifest.GetAllDependencies(strFullName); } else if (LocalABBundleManifest != null && LocalABBundleHashSet.Contains(strFullName)) { loadFromType = LoadFromType.LoadFromLocalAB; dependencies = LocalABBundleManifest.GetAllDependencies(strFullName); } int nLength = dependencies.Length; if (nLength > 0) { DependencsLoader dependCallBack = mDependItemPool.GetObject(); if (dependCallBack != null) { dependCallBack.Reset(); dependCallBack.mLength = nLength; } for (int i = 0; i < nLength; ++i) { ResourceManager.Instance.StartCoroutine(LoadBundleByWWW(dependencies[i], dependCallBack, loadFromType)); } while (!dependCallBack.AllDone) { yield return(null); } mDependItemPool.RemoveObject(dependCallBack); } yield return(ResourceManager.Instance.StartCoroutine(LoadBundleByWWW(strFullName, null, loadFromType))); loaded = ABManager.Instance.GetLoadedBundle(nHashCode); if (loaded != null) { if (nLength > 0) { loaded.AddDpendences(dependencies); } loaded.MapAssets(); ABManager.Instance.AfterLoad(loaded); } yield return(null); }
public LoadedAssetBundle LoadSyn(string strFullName, UpdateProgress updateFunc = null, bool bSend = false) { LoadedAssetBundle loaded = ABManager.Instance.GetLoadedBundle(strFullName.GetHashCode()); if (loaded != null) { loaded.Count(); return(loaded); } AssetManageInfo info = AssetManageConfigManager.Instance.GetInfo(strFullName); //资源是从DownLoad包里还是StreamAsset的包里读取的 LoadFromType loadFromType = LoadFromType.LoadFromNone; string[] dependencies = null; if (AssetBundleManifest != null && AssetBundleHashSet.Contains(strFullName)) {//是否在下载的包里 loadFromType = LoadFromType.LoadFromDownLoad; dependencies = AssetBundleManifest.GetAllDependencies(strFullName); } else if (LocalABBundleManifest != null && LocalABBundleHashSet.Contains(strFullName)) {//下载的包里找不到就到跟包的目录里寻找一遍看是否有依赖 loadFromType = LoadFromType.LoadFromLocalAB; dependencies = LocalABBundleManifest.GetAllDependencies(strFullName); } //只要不是LoadFromNone,说明这个资源是有身份证的,并非来路不明 if (loadFromType != LoadFromType.LoadFromNone) { int nCnt = 1; if (dependencies != null) { nCnt += dependencies.Length; for (int i = 0; i < dependencies.Length; ++i) { LoadBundleByFileStream(dependencies[i], getLoadFromType(dependencies[i]), AssetEncryptMode.None); if (updateFunc != null) { updateFunc((float)(i + 1) / nCnt, bSend); } } } //Debug.Log("<color=red> LoadSyn: " + strFullName + "</color>"); loaded = LoadBundleByFileStream(strFullName, loadFromType, info.encryption); if (loaded != null) { if (dependencies != null && dependencies.Length > 0) { loaded.AddDpendences(dependencies); } if (info.loadType != ELoadType.Music && info.loadType != ELoadType.Scene) { loaded.MapAssets(); ABManager.Instance.AfterLoad(loaded); } if (updateFunc != null) { updateFunc(1.0f, bSend); } } return(loaded); } else { Debug.Log("<color=red>资源:" + strFullName + "来路不明,在bundle表里不存在。</color>"); return(null); } }