public override void Init(string assetBundlePath, string assetPath, LoadMode loadMode, params object[] args) { base.Init(assetBundlePath, assetPath, loadMode, args); LoaderCallback callback = (isOk, asset) => { if (IsReadyDisposed) // 中途释放 { OnFinish(null); return; } if (!isOk) { OnFinish(null); Debuger.LogError(LOG_TAG, "Error on AssetLoader loaded... {0}|{1}", assetBundlePath, assetPath); return; } Object prefab = asset as Object; Object instance = Object.Instantiate(prefab); instance.name = prefab.name; if (Application.isEditor) { LoadedAssetDebugger.Create("InstanceAsset", GetUniqueKey(), instance); } OnFinish(instance); }; _assetLoader = BaseLoader.Load <AssetLoader>(assetBundlePath, assetPath, loadMode, callback, true); }
/// <summary> /// 请求异步加载 /// </summary> public void RequestAsyncLoad(string assetPath, string kName, LoaderCallback kEventReceiver) { if (assetPath == null || assetPath == string.Empty || assetPath.Contains("xx")) { return; } int num = IsLoading(assetPath); if (num != -1) { this.m_kLoadingQueue[num].kEventReceivers.Add(kEventReceiver); return; } if (this.m_kLoadedAsset.ContainsKey(assetPath)) { kEventReceiver.OnLoaded(this.m_kLoadedAsset[assetPath], true); return; } for (int i = 0; i < this.m_kExecQueue.Count; i++) { if (this.m_kExecQueue[i] != null && this.m_kExecQueue[i].kAssetPath == assetPath) { this.m_kExecQueue[i].kEventReceivers.Add(kEventReceiver); return; } } if (this.important(kName)) { this.m_kLoadingQueue.Insert(0, new LoadingTask(assetPath, kName, kEventReceiver)); } else { this.m_kLoadingQueue.Add(new LoadingTask(assetPath, kName, kEventReceiver)); } }
/// <summary> /// Loads the instance asset async. /// 'assetBundlePath',只需要填相对路径,不用加后缀,如 UI/Panel/MenuPanel,除非ab不是按AssetConfig里面配置的后缀来设置的. /// 'assetName',只需要填相对路径,需要加后缀,如 UI/Panel/MenuPanel.prefab /// </summary> public InstanceAssetLoader LoadInstanceAssetAsync(string assetBundleRelativePath, string assetName, OnLoadInstanceAsset callback) { // 如果是加载ab中的资源,需要填完成的路径,这跟 BuildAssetBundleOptions 有关,参考BundleBuilder.BuildAssetBundle() if (!string.IsNullOrEmpty(assetBundleRelativePath)) { assetName = AssetConfig.GetAssetFullPathInAB(assetName); } string fullRelativePath = AssetConfig.GetAssetBundleFullRelativePath(assetBundleRelativePath); string assetBundleFullPath = AssetConfig.GetGameAssetFullPath(fullRelativePath); LoaderCallback internelHandler = (bool isOk, object resultObject) => { if (isOk) { callback.Invoke(resultObject as Object); } else { callback.Invoke(null); } }; return(BaseLoader.Load <InstanceAssetLoader>(assetBundleFullPath, assetName, callback: internelHandler)); }
/// <summary> /// 在IsFinisehd后执行的回调 /// </summary> /// <param name="callback"></param> public void AddCallback(LoaderCallback callback) { if (callback == null) { return; } if (IsCompleted) { // TODO: 这里如果原先是Async加载方式,这样调用就会变成Sync,会有问题. callback(IsSuccess, ResultObject); } else { _callbacks.Add(callback); } }
/// <summary> /// Loads the asset bundle async. /// 'assetBundlePath',只需要填相对路径,不用填后缀,如 UI/Panel/MenuPanel,除非ab不是按AssetConfig里面配置的后缀来设置的. /// </summary> public AssetBundleLoader LoadAssetBundleAsync(string assetBundleRelativePath, OnLoadAssetBundle callback) { string fullRelativePath = AssetConfig.GetAssetBundleFullRelativePath(assetBundleRelativePath); string assetBundleFullPath = AssetConfig.GetGameAssetFullPath(fullRelativePath); LoaderCallback internelHandler = (bool isOk, object resultObject) => { if (isOk) { callback.Invoke(resultObject as AssetBundle); } else { callback.Invoke(null); } }; return(BaseLoader.Load <AssetBundleLoader>(assetBundleFullPath, "", callback: internelHandler)); }
public LoadingTask(string kPath, string kMain, LoaderCallback kobj) { this.kMainName = kMain; this.kAssetPath = kPath; this.kEventReceivers.Add(kobj); }
IEnumerator DoLoadAssetBundle(string name, LoaderCallback callback) { if (GameDefines.IsUseStream()) { AssetBundle ab = null; if (!m_mBundle.TryGetValue(name, out ab)) { string url = GameDefines.GetUrlBase() + name + ".assetbundle"; Debug.Log("url:" + url); WWW www; //int cacheID = GetCacheID(name); int cacheID = GameDefines.AssetBundleVersion; if (cacheID >= 0) { www = WWW.LoadFromCacheOrDownload(url, cacheID); } else { www = new WWW(url); } while (!www.isDone) { yield return(0); } if (www.error != null) { Debug.Log("Error: " + name + " " + www.error); if (callback != null) { callback(null); } yield break; } else { Debug.Log("OK: " + name + "" + www.assetBundle.ToString()); if (!m_mBundle.ContainsKey(name)) { m_mBundle.Add(name, www.assetBundle); } if (callback != null) { ab = www.assetBundle; bool useMainAsset = true; if (useMainAsset) { callback(ab.mainAsset); } else { callback(ab.LoadAsset(name)); } } yield break; } } else { if (callback != null) { bool useMainAsset = true; if (useMainAsset) { callback(ab.mainAsset); } else { callback(ab.LoadAsset(name)); } } yield break; } } else { Object o = Resources.Load(name); if (callback != null) { callback(o); } yield break; } }
public void LoadAssetBundle(string name, LoaderCallback callback) { StartCoroutine(DoLoadAssetBundle(name, callback)); }
public IEnumerator GetLoader(string bundName, string name, bool useMainAsset, LoaderCallback callback) { if (GameDefines.IsUseStream()) { string url = ""; string bundle = bundName; if (useMainAsset) { bundle += "_" + name; } AssetBundle ab = null; if (!m_mBundle.TryGetValue(bundle, out ab)) { url = GameDefines.GetUrlBase() + GetCachePath(bundle) + bundle + ".assetbundle"; Debug.Log("url:" + url); WWW www; int cacheID = GetCacheID(bundle); if (cacheID >= 0) { www = WWW.LoadFromCacheOrDownload(url, cacheID); } else { www = new WWW(url); } while (!www.isDone) { yield return(0); } if (www.error != null) { Debug.Log("Error: " + www.error); if (callback != null) { callback(null); } yield break; } else { Debug.Log("OK: " + bundle + "" + www.assetBundle.ToString()); m_mBundle.Add(bundle, www.assetBundle); ab = www.assetBundle; } } if (useMainAsset) { if (callback != null) { callback(ab.mainAsset); } } else { if (callback != null) { callback(ab.LoadAsset(name)); } } } else { string res = bundName + "/" + name; Object o = Resources.Load(res); if (callback != null) { callback(o); } yield break; } }
/// <summary> /// 统一的对象工厂 /// </summary> public static T AutoNew <T>(string assetBundlePath, string assetPath, LoadMode loadMode, LoaderCallback callback, bool forceCreateNew = false, params object[] initArgs) where T : BaseLoader, new() { // 唯一key. string uniqueKey = BaseLoader.GenerateKey(assetBundlePath, assetPath); Dictionary <string, BaseLoader> typesDict = GetTypeDict(typeof(T)); BaseLoader loader; if (forceCreateNew || !typesDict.TryGetValue(uniqueKey, out loader)) { loader = new T(); if (!forceCreateNew) { typesDict[uniqueKey] = loader; //NOTE: add to remove loader都用 loader._uniqueKey作为key } loader.IsForceNew = forceCreateNew; loader.Init(assetBundlePath, assetPath, loadMode, initArgs); // 创建新的BaseLoader if (Application.isEditor) { BaseLoaderDebugger.Create(typeof(T).Name, uniqueKey, loader); } } else { if (loader.RefCount < 0) { // 转死回生的可能 Debuger.LogError(LOG_TAG, "Error RefCount!"); } } // 引用计数+1 loader.AddRef(); // RefCount++了,重新激活,在队列中准备清理的Loader if (UnuseLoaders.ContainsKey(loader)) { UnuseLoaders.Remove(loader); loader.Revive(); } loader.AddCallback(callback); return(loader as T); }
public Loader(LoaderCallback DataLoaderFunction) { this.dataloader = DataLoaderFunction; }
/// <summary> /// Registers assembly and resource loaders. /// </summary> /// <remarks> /// If you want to customize the way assemblies or resources are loaded, invoke /// this method providing your own implementations. /// </remarks> static public void RegisterLoader (LoaderCallback cb, ResourceLoaderCallback rcb) { loader_callback = cb; resource_loader_callback = rcb; }
/// <summary> /// Load the specified assetBundlePath, assetPath, loadMode, callback, forceCreateNew and initArgs. /// 加载 assetBundle 时: assetPath 为null或者"" /// 加载 assetBundle中的asset 时: assetBundlePath和assetPath都要填 /// 加载 非assetBundle中的asset 时: assetBundlePath 为null或者"" /// </summary> public static T Load <T>(string assetBundlePath, string assetPath, LoadMode loadMode = LoadMode.Async, LoaderCallback callback = null, bool forceCreateNew = false, params object[] initArgs) where T : BaseLoader, new() { return(LoaderCache.AutoNew <T>(assetBundlePath, assetPath, loadMode, callback, forceCreateNew, initArgs)); }
/// <summary> /// Registers assembly and resource loaders. /// </summary> /// <remarks> /// If you want to customize the way assemblies or resources are loaded, invoke /// this method providing your own implementations. /// </remarks> static public void RegisterLoader(LoaderCallback cb, ResourceLoaderCallback rcb) { loader_callback = cb; resource_loader_callback = rcb; }