/// <summary> /// Page GameObject Name 必须和LuaLogic的文件名一致,避免不必要的配置 /// </summary> /// <param name="pagePath"></param> /// <param name="param"></param> public void Show(string pagePath, object param = null) { if (!cachePagePool.ContainsKey(pagePath)) { new Promise <LoadOperation>((s, j) => { LoadOperation loader = GResource.LoadAssetAsync(pagePath); loader.OnFinish += s; AppInter.StartCoroutine(loader); }).Then((loader) => { GameObject prefab = loader.GetAsset <GameObject>(); GameObject gObj = GameObject.Instantiate(prefab); gObj.name = Path.GetFileNameWithoutExtension(pagePath); LuaPageBehaviour pageBehaviour = gObj.AddComponent <LuaPageBehaviour>(); UIPage page = new UIPage(pageBehaviour, pagePath); //缓存记录 cachePagePool.Add(pagePath, page); showUIPage(page, param); }).Catch((e) => { Debug.LogError("加载Page异常! Path:" + pagePath); Debug.LogException(e); }); } else { UIPage page = cachePagePool[pagePath]; showUIPage(page, param); } }
/// <summary> /// 执行资源预加载 /// </summary> /// <param name="luaCallback"></param> public static void OnInitPreload(LuaFunction luaCallback) { Action callback = null; if (luaCallback != null) { callback = (Action)DelegateFactory.CreateDelegate(typeof(Action), luaCallback); } AppInter.StartCoroutine(AssetLoader.Instance.InitPreLoad(callback)); }
/// <summary> /// 通过指定资源名加载GameObject资源 /// </summary> /// <param name="resName">资源名称</param> /// <param name="callback">加载实例完成后的回调</param> public static void LoadGameObjectByName(string resName, EAssetType assetType, Action <GameObject> callback) { AppInter.StartCoroutine(Instance.SwapAssetByName <UnityEngine.Object>(EAssetType.ALL.ToString(), resName, assetType, (obj) => { if (obj == null) { callback.Invoke(null); return; } //实例 GameObject gObj = GameObject.Instantiate(obj) as GameObject; callback.Invoke(gObj); })); }
/// <summary> /// 加载资源 /// </summary> /// <param name="resName">资源的名称 eg:res01.prefab</param> public void AsyncLoadBundleByName(string resName, EAssetType assetType, Action <AssetBundle> callback) { string finalName = GetBundleName(resName, assetType); if (string.IsNullOrEmpty(finalName)) { Debugger.LogWarning("<<AssetLoader , SwapAssetByName>> Cant find file ! name :--->> " + resName + " , bundle :" + finalName); if (callback != null) { callback(null); } return; } AppInter.StartCoroutine(AsyncLoadBundleAtPath(finalName, callback, null)); }
/// <summary> /// 加载AssetBundle及其依赖的资源 /// </summary> /// <param name="cachePool">缓存池</param> /// <param name="bundleName">目标AssetBundle资源</param> /// <param name="callback">最终的资源加载完成时的回调</param> /// <returns></returns> private IEnumerator loadBundleAndDependenices(ARCCache <string, Asset> cachePool, string bundleName, Action <AssetBundle> callback, Action <float, float> progress) { string[] depAssetArr = manifest.GetAllDependencies(bundleName); foreach (string depAsset in depAssetArr) { //重新获取Bundle名,因为可能依赖的名称内带有目录信息 string depBundleName = Path.GetFileName(depAsset); yield return(AppInter.StartCoroutine(AsyncLoadBundleAtPath(depBundleName, (obj) => { if (obj != null) { Asset asset = new Asset(depBundleName, obj); cachePool.Put(depBundleName, asset); } }))); } yield return(AppInter.StartCoroutine(AsyncLoadBundleAtPath(bundleName, callback, progress))); }
/// <summary> /// 初始化预加载 /// </summary> public IEnumerator InitPreLoad(Action finish) { bool isFinishMap = false; string assetMapName = (!isBundle ? "dev" : "") + AppPathUtils.AssetBundleMap; yield return(AppInter.StartCoroutine(AsyncLoadAtPath(assetMapName, EAssetBaseType.Text, (obj) => { string[] assetArr = ((string)obj).Split('\n'); int chunkIndex = 0; foreach (string assetInfo in assetArr) { string asset = assetInfo.Trim(); if (string.IsNullOrEmpty(asset)) { continue; } if (asset == AppPathUtils.AssetForMap) { chunkIndex = 1; continue; } string[] fileInfos = asset.Split(';'); string assetType = fileInfos[0]; string fileName = Path.GetFileName(fileInfos[1]); //去掉目录 fileName = fileName.Replace(".", "_" + assetType + "."); if (chunkIndex == 0) { if (isBundle) { nameToPathMap[fileName] = fileInfos[2]; //fileInfos[2] -> Md5值 } else { string srcfileName = Path.GetFileName(fileInfos[2]); nameToPathMap[srcfileName + assetType] = fileInfos[2]; } } else if (chunkIndex == 1) { string bundleName = Path.GetFileName(fileInfos[2]); //去掉目录 pathToBundleMap[fileName] = bundleName; } } isFinishMap = true; }))); while (!isFinishMap) { yield return(null); } if (isBundle) { //加载assetBundleManifest yield return(AppInter.StartCoroutine(AsyncLoadBundleAtPath("bin_none" + AppPathUtils.BundleSuffix, (bundle) => { if (bundle != null) { Debugger.LogWarning("<<AssetLoader , InitPreLoad>> Cant find AssetBundleManifest"); return; } manifest = bundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); }))); } if (finish != null) { finish.Invoke(); } Debugger.Log("AssetLoader , InitPreload is finish "); }
// /// <summary> // /// 从缓存中获取资源 // /// </summary> // /// <param name="poolName">缓存池的名称</param> // /// <param name="path">资源相对路径</param> // /// <param name="callback">加载完成的回调</param> // public void SwapAssetAtPath(string poolName , string path , Action<UnityEngine.Object> callback) // { // ARCCache<string, AssetBundle> cachePool = swapnCachePool(poolName); // AssetBundle bundle = cachePool.Get(path); // if (bundle != null) // { // if (callback != null) // callback(bundle.mainAsset); // return; // } // // //异步加载 // AppInter.StartCoroutine(AsyncLoadAtPath(path,EAssetBaseType.AssetBundle, (obj) => // { // bundle = obj as AssetBundle; // if (bundle == null) // { // Debugger.LogWarning("<<AssetLoader , SwapAsset>> load faile ! Path -->> " + path); // return; // } // cachePool.Put(path , bundle); // // if(callback != null) // callback(bundle.mainAsset); // })); // } /// <summary> /// 从AssetBundle缓存中获取资源 /// </summary> /// <param name="poolName">缓存池的名称</param> /// <param name="resName">资源名</param> /// <param name="callback">加载完成的回调</param> public IEnumerator SwapAssetByName <T>(string poolName, string resName, EAssetType assetType, Action <UnityEngine.Object> callback, Action <float, float> progress) where T : UnityEngine.Object { ARCCache <string, Asset> cachePool = swapnCachePool(poolName); string bundleName = GetBundleName(resName, assetType); if (string.IsNullOrEmpty(bundleName)) { callback.Invoke(null); Debugger.LogWarning("<<AssetLoader , SwapAssetByName>> load faile ! Res -->> " + resName); yield break; } Asset bundle = cachePool.Get(bundleName); #if UNITY_EDITOR if (!isBundle) { if (bundle == null) { string path = GetBundleEncrypeName(resName + assetType.ToString().ToLower()); UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath <T>(path); if (obj == null) { Debugger.LogWarning("<<AssetLoader , SwapAssetByName>> load faile ! Path -->> " + path); callback.Invoke(obj); yield break; } bundle = new Asset(bundleName, obj); cachePool.Put(bundleName, bundle); } callback.Invoke(bundle.Resource); yield break; } #endif if (bundle != null) { if (callback != null) { callback(bundle.Get <AssetBundle>().LoadAsset <T>(resName)); } yield break; } //异步加载 yield return(AppInter.StartCoroutine(loadBundleAndDependenices(cachePool, bundleName, (obj) => { if (obj == null) { Debugger.LogWarning("<<AssetLoader , SwapAssetByName>> load faile ! Path -->> " + bundleName); callback.Invoke(null); return; } Asset asset = new Asset(bundleName, obj); cachePool.Put(bundleName, asset); if (callback != null) { callback(obj.LoadAsset <T>(resName)); } }, progress))); }