public void AsyncLoad(string path, Action <AssetBundle> callback, bool isdepend = false) { if (m_manifest == null) { ResMgr.Log(Tag, "AsyncLoad", "manifest is null"); return; } string abname; if (isdepend) { abname = path; } else { abname = XGamePath.Path2ResName(path); } int hash = abname.GetHashCode(); AbAbstractLoader loader = null; if (m_Ab_Dic.TryGetValue(hash, out loader)) { if (callback != null) { callback(loader.Bundle); } return; } string[] depends = AbMgr.GetInstance().GetDepends(abname); for (int i = 0; i < depends.Length; i++) { ResMgr.Log(Tag, "AsyncLoad|depends", depends[i]); int hash2 = depends[i].GetHashCode(); AbAbstractLoader loader2 = null; if (m_Ab_Dic.TryGetValue(hash2, out loader2)) { continue; } if (m_loading_Dic.ContainsKey(hash2)) { AbAsyncLoader asyncloader = m_loading_Dic[hash2] as AbAsyncLoader; asyncloader.request.priority++; continue; } AbMgr.GetInstance().AsyncLoad(depends[i], null, true); } loader = new AbAsyncLoader(path, abname, callback); path = XGamePath.GetStreamingAbPath(abname); loader.LoadAsset(path); m_loading_Dic.Add(hash, loader as AbAsyncLoader); }
public IEnumerator Init() { ResMgr.Log(Tag, "Init", "Start Async Init"); string path = XGamePath.GetStreamingAbPath(m_manifest_name); ResMgr.Log(Tag, "Init", "==>" + path); if (File.Exists(path)) { //同步加载 //FileStream fs = File.Open(path, FileMode.Open); //m_manifest_ab = AssetBundle.LoadFromStream(fs); //m_manifest = m_manifest_ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); //if (m_Ab_Dic.Count > 0) //{ // Debug.LogError("不应该发生的事情发生了"); //} //m_Ab_Dic.Clear(); //loader = new AbSyncLoader(); //异步加载 AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path); yield return(request); if (request.isDone) { m_manifest = request.assetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); if (m_Ab_Dic.Count > 0) { ResMgr.LogError(Tag, "Init", "this should never happen !!!!"); } //string[] depends = m_manifest.GetAllAssetBundles(); //foreach (var item in depends) //{ // Debug.Log("depends log ==>" + item); //} m_Ab_Dic.Clear(); ResMgr.Log(Tag, "Init", "load manifest success"); } else { ResMgr.Log(Tag, "Init", "load manifest fail"); } } else { ResMgr.Log(Tag, "Init", "manifest not exist"); } }
public AssetBundle SyncLoad(string path, bool isdepend = false) { if (m_manifest == null) { return(null); } string abname; if (isdepend) { abname = path; } else { abname = XGamePath.Path2ResName(path); } string fullpath = XGamePath.GetStreamingAbPath(abname); int hash = abname.GetHashCode();//path.GetHashCode(); string[] depends = AbMgr.GetInstance().GetDepends(abname); for (int i = 0; i < depends.Length; i++) { ResMgr.Log(Tag, "SyncLoad|depends", depends[i]); int hash2 = depends[i].GetHashCode(); AbAbstractLoader loader2 = null; if (m_Ab_Dic.TryGetValue(hash2, out loader2)) { continue; } if (m_loading_Dic.ContainsKey(hash2)) { AbAsyncLoader asyncloader = m_loading_Dic[hash2] as AbAsyncLoader; asyncloader.request.priority++; continue; } AbMgr.GetInstance().SyncLoad(depends[i], true); } AbAbstractLoader loader; if (m_Ab_Dic.TryGetValue(hash, out loader)) { switch (loader.State) { case AbBundleState.success: return(loader.Bundle); case AbBundleState.loading: if (!loader.isSync) { //todo 转同步加载 } break; case AbBundleState.none: default: break; } return(loader.Bundle); } loader = new AbSyncLoader(fullpath, abname); loader.LoadAsset(fullpath); m_Ab_Dic.Add(hash, loader); return(m_Ab_Dic[hash].Bundle); }