/// <summary> /// 加载所有assetbundle /// </summary> /// <returns></returns> static IEnumerator IE_LoadAll() { var outpath = BApplication.BDEditorCachePath + "/AssetBundle"; if (!Directory.Exists(outpath)) { Directory.CreateDirectory(outpath); } loadDataMap.Clear(); //加载 var allRuntimeAssets = BApplication.GetAllRuntimeAssetsPath(); foreach (var asset in allRuntimeAssets) { var type = AssetBundleEditorToolsV2.GetMainAssetTypeAtPath(asset); if (type == null) { Debug.LogError("无法获得资源类型:" + asset); continue; } var idx = asset.IndexOf(AssetBundleBuildingContext.RUNTIME_PATH, StringComparison.OrdinalIgnoreCase); var runtimePath = asset.Substring(idx + AssetBundleBuildingContext.RUNTIME_PATH.Length); runtimePath = runtimePath.Replace(Path.GetExtension(runtimePath), ""); runtimePath = runtimePath.Replace("\\", "/"); //Debug.Log("【LoadTest】:" + runtimePath); List <LoadTimeData> loadList = null; if (!loadDataMap.TryGetValue(type.FullName, out loadList)) { loadList = new List <LoadTimeData>(); loadDataMap[type.FullName] = loadList; } var loadData = new LoadTimeData(); loadData.LoadPath = runtimePath; loadList.Add(loadData); //计时器 Stopwatch sw = new Stopwatch(); if (type == typeof(GameObject)) { //加载 sw.Start(); var obj = AssetBundleLoader.Load <GameObject>(runtimePath); sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; //实例化 if (obj != null) { sw.Restart(); var gobj = GameObject.Instantiate(obj); sw.Stop(); loadData.InstanceTime = sw.ElapsedTicks; //UI var rectTransform = gobj.GetComponentInChildren <RectTransform>(); if (rectTransform != null) { gobj.transform.SetParent(UI_ROOT, false); } else { gobj.transform.SetParent(SCENE_ROOT); } //抓屏 保存 var outpng = string.Format("{0}/{1}_ab.png", outpath, runtimePath.Replace("/", "_")); yield return(null); //渲染 GameView.Repaint(); GameView.Focus(); yield return(null); //抓屏 //TODO 这里有时候能抓到 有时候抓不到 ScreenCapture.CaptureScreenshot(outpng); //删除 GameObject.DestroyImmediate(gobj); } else { UnityEngine.Debug.LogError("【Prefab】加载失败:" + runtimePath); } } else if (type == typeof(TextAsset)) { //测试打印AssetText资源 sw.Start(); var textAsset = AssetBundleLoader.Load <TextAsset>(runtimePath); sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!textAsset) { UnityEngine.Debug.LogError("【TextAsset】加载失败:" + runtimePath); } else { UnityEngine.Debug.Log(textAsset.text); } } else if (type == typeof(Texture)) { sw.Start(); var tex = AssetBundleLoader.Load <Texture>(runtimePath); sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!tex) { UnityEngine.Debug.LogError("【Texture】加载失败:" + runtimePath); } break; } else if (type == typeof(Texture2D)) { sw.Start(); var tex = AssetBundleLoader.Load <Texture2D>(runtimePath); sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!tex) { UnityEngine.Debug.LogError("【Texture2D】加载失败:" + runtimePath); } } else if (type == typeof(Sprite)) { sw.Start(); var sp = AssetBundleLoader.Load <Sprite>(runtimePath); sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!sp) { UnityEngine.Debug.LogError("【Sprite】加载失败:" + runtimePath); } } else if (type == typeof(Material)) { sw.Start(); var mat = AssetBundleLoader.Load <Material>(runtimePath); sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!mat) { UnityEngine.Debug.LogError("【Material】加载失败:" + runtimePath); } } else if (type == typeof(Shader)) { sw.Start(); var shader = AssetBundleLoader.Load <Shader>(runtimePath); sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!shader) { UnityEngine.Debug.LogError("【Shader】加载失败:" + runtimePath); } } else if (type == typeof(AudioClip)) { sw.Start(); var ac = AssetBundleLoader.Load <AudioClip>(runtimePath); sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!ac) { UnityEngine.Debug.LogError("【AudioClip】加载失败:" + runtimePath); } } else if (type == typeof(AnimationClip)) { sw.Start(); var anic = AssetBundleLoader.Load <AnimationClip>(runtimePath); sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!anic) { UnityEngine.Debug.LogError("【AnimationClip】加载失败:" + runtimePath); } } else if (type == typeof(Mesh)) { sw.Start(); var mesh = AssetBundleLoader.Load <Mesh>(runtimePath); sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!mesh) { UnityEngine.Debug.LogError("【Mesh】加载失败:" + runtimePath); } } else if (type == typeof(Font)) { sw.Start(); var font = AssetBundleLoader.Load <Font>(runtimePath); sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!font) { UnityEngine.Debug.LogError("【Font】加载失败:" + runtimePath); } } else if (type == typeof(SpriteAtlas)) { sw.Start(); var sa = AssetBundleLoader.Load <SpriteAtlas>(runtimePath); sw.Stop(); if (!sa) { UnityEngine.Debug.LogError("【SpriteAtlas】加载失败:" + runtimePath); } loadData.LoadTime = sw.ElapsedTicks; } else if (type == typeof(ShaderVariantCollection)) { sw.Start(); var svc = AssetBundleLoader.Load <ShaderVariantCollection>(runtimePath); svc?.WarmUp(); sw.Stop(); if (!svc) { UnityEngine.Debug.LogError("【ShaderVariantCollection】加载失败:" + runtimePath); } loadData.LoadTime = sw.ElapsedTicks; } else if (type == typeof(AnimatorController)) { sw.Start(); var aniCtrl = AssetBundleLoader.Load <AnimatorController>(runtimePath); sw.Stop(); if (!aniCtrl) { UnityEngine.Debug.LogError("【AnimatorController】加载失败:" + runtimePath); } loadData.LoadTime = sw.ElapsedTicks; } else { sw.Start(); var gobj = AssetBundleLoader.Load <Object>(runtimePath); sw.Stop(); if (!gobj) { UnityEngine.Debug.LogError("【Object】加载失败:" + runtimePath); } UnityEngine.Debug.LogError("待编写测试! -" + type.FullName); } //打印 Debug.LogFormat("<color=yellow>{0}</color> <color=green>【加载】:<color=yellow>{1}ms</color>;【初始化】:<color=yellow>{2}ms</color> </color>", loadData.LoadPath, loadData.LoadTime / 10000f, loadData.InstanceTime / 10000f); yield return(null); } yield return(null); foreach (var item in loadDataMap) { Debug.Log("<color=red>【" + item.Key + "】</color>"); foreach (var ld in item.Value) { Debug.LogFormat( "<color=yellow>{0}</color> <color=green>【加载】:<color=yellow>{1}ms</color>;【初始化】:<color=yellow>{2}ms</color> </color>", ld.LoadPath, ld.LoadTime / 10000f, ld.InstanceTime / 10000f); } } yield return(null); EditorUtility.RevealInFinder(outpath); }
/// <summary> /// 加载所有assetbundle /// </summary> /// <returns></returns> static IEnumerator IE_01_LoadAll(bool isAsyncLoad = false) { var outpath = BApplication.BDEditorCachePath + "/AssetBundle"; if (!Directory.Exists(outpath)) { Directory.CreateDirectory(outpath); } loadDataMap.Clear(); //加载 foreach (var assetdata in AssetBundleLoader.AssetConfigLoder.AssetbundleItemList) { if (string.IsNullOrEmpty(assetdata.LoadPath)) { continue; } var typeName = AssetBundleLoader.AssetConfigLoder.AssetTypes.AssetTypeList[assetdata.AssetType]; var runtimePath = assetdata.LoadPath; //加载 //Debug.Log("【LoadTest】:" + runtimePath); if (!loadDataMap.ContainsKey(typeName)) { loadDataMap[typeName] = new List <LoadTimeData>(); } var loadList = loadDataMap[typeName]; // var loadData = new LoadTimeData(); loadData.LoadPath = runtimePath; loadList.Add(loadData); //计时器 Stopwatch sw = new Stopwatch(); if (typeName == typeof(GameObject).FullName) { //加载 sw.Start(); GameObject obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <GameObject>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <GameObject>(); } } //同步 else { obj = AssetBundleLoader.Load <GameObject>(runtimePath); } sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; //实例化 if (obj != null) { sw.Restart(); var gobj = GameObject.Instantiate(obj); sw.Stop(); loadData.InstanceTime = sw.ElapsedTicks; //UI var rectTransform = gobj.GetComponentInChildren <RectTransform>(); if (rectTransform != null) { gobj.transform.SetParent(UI_ROOT, false); } else { gobj.transform.SetParent(SCENE_ROOT); } //抓屏 保存 var outpng = string.Format("{0}/{1}_ab.png", outpath, runtimePath.Replace("/", "_")); yield return(null); //渲染 // GameView.Repaint(); // GameView.Focus(); yield return(null); //抓屏 //TODO 这里有时候能抓到 有时候抓不到 ScreenCapture.CaptureScreenshot(outpng); //删除 GameObject.DestroyImmediate(gobj); } else { UnityEngine.Debug.LogError("【Prefab】加载失败:" + runtimePath); } } else if (typeName == typeof(TextAsset).FullName) { //测试打印AssetText资源 sw.Start(); TextAsset obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <TextAsset>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <TextAsset>(); } } //同步 else { obj = AssetBundleLoader.Load <TextAsset>(runtimePath); } sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!obj) { UnityEngine.Debug.LogError("【TextAsset】加载失败:" + runtimePath); } else { UnityEngine.Debug.Log(obj.text); } } else if (typeName == typeof(Texture).FullName) { sw.Start(); Texture obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <Texture>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <Texture>(); } } //同步 else { obj = AssetBundleLoader.Load <Texture>(runtimePath); } sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!obj) { UnityEngine.Debug.LogError("【Texture】加载失败:" + runtimePath); } break; } else if (typeName == typeof(Texture2D).FullName) { sw.Start(); Texture2D obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <Texture2D>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <Texture2D>(); } } //同步 else { obj = AssetBundleLoader.Load <Texture2D>(runtimePath); } sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!obj) { UnityEngine.Debug.LogError("【Texture2D】加载失败:" + runtimePath); } else { spriteRendererNode.gameObject.SetActive(true); spriteRendererNode.sprite = Sprite.Create(obj, new Rect(Vector2.zero, obj.texelSize), new Vector2(0.5f, 0.5f), 128); yield return(null); spriteRendererNode.gameObject.SetActive(false); } } else if (typeName == typeof(Sprite).FullName) { sw.Start(); Sprite obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <Sprite>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <Sprite>(); } } //同步 else { obj = AssetBundleLoader.Load <Sprite>(runtimePath); } sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!obj) { UnityEngine.Debug.LogError("【Sprite】加载失败:" + runtimePath); } else { imageNode.gameObject.SetActive(true); imageNode.overrideSprite = obj; imageNode.SetNativeSize(); yield return(null); imageNode.gameObject.SetActive(false); } } else if (typeName == typeof(Material).FullName) { sw.Start(); Material obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <Material>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <Material>(); } } //同步 else { obj = AssetBundleLoader.Load <Material>(runtimePath); } sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!obj) { UnityEngine.Debug.LogError("【Material】加载失败:" + runtimePath); } } else if (typeName == typeof(Shader).FullName) { sw.Start(); var obj = AssetBundleLoader.Load <Shader>(runtimePath); //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <Shader>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <Shader>(); } } //同步 else { obj = AssetBundleLoader.Load <Shader>(runtimePath); } sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!obj) { UnityEngine.Debug.LogError("【Shader】加载失败:" + runtimePath); } } else if (typeName == typeof(AudioClip).FullName) { sw.Start(); AudioClip obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <AudioClip>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <AudioClip>(); } } //同步 else { obj = AssetBundleLoader.Load <AudioClip>(runtimePath); } sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!obj) { UnityEngine.Debug.LogError("【AudioClip】加载失败:" + runtimePath); } } else if (typeName == typeof(AnimationClip).FullName) { sw.Start(); AnimationClip obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <AnimationClip>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <AnimationClip>(); } } //同步 else { obj = AssetBundleLoader.Load <AnimationClip>(runtimePath); } sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!obj) { UnityEngine.Debug.LogError("【AnimationClip】加载失败:" + runtimePath); } } else if (typeName == typeof(Mesh).FullName) { sw.Start(); Mesh obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <Mesh>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <Mesh>(); } } //同步 else { obj = AssetBundleLoader.Load <Mesh>(runtimePath); } sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!obj) { UnityEngine.Debug.LogError("【Mesh】加载失败:" + runtimePath); } } else if (typeName == typeof(Font).FullName) { sw.Start(); Font obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <Font>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <Font>(); } } //同步 else { obj = AssetBundleLoader.Load <Font>(runtimePath); } sw.Stop(); loadData.LoadTime = sw.ElapsedTicks; if (!obj) { UnityEngine.Debug.LogError("【Font】加载失败:" + runtimePath); } } else if (typeName == typeof(SpriteAtlas).FullName) { sw.Start(); SpriteAtlas obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <SpriteAtlas>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <SpriteAtlas>(); } } //同步 else { obj = AssetBundleLoader.Load <SpriteAtlas>(runtimePath); } sw.Stop(); if (!obj) { // UnityEngine.Debug.LogError("【SpriteAtlas】加载失败:" + runtimePath); } loadData.LoadTime = sw.ElapsedTicks; } else if (typeName == typeof(ShaderVariantCollection).FullName) { sw.Start(); ShaderVariantCollection obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <ShaderVariantCollection>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <ShaderVariantCollection>(); } } //同步 else { obj = AssetBundleLoader.Load <ShaderVariantCollection>(runtimePath); } obj?.WarmUp(); sw.Stop(); if (!obj) { UnityEngine.Debug.LogError("【ShaderVariantCollection】加载失败:" + runtimePath); } loadData.LoadTime = sw.ElapsedTicks; } else { sw.Start(); Object obj = null; //异步 if (isAsyncLoad) { var ret = AssetBundleLoader.CreateAsyncLoadTask <Object>(runtimePath); yield return(ret); if (ret.IsSuccess) { obj = ret.GetAssetBundleInstance <Object>(); } } //同步 else { obj = AssetBundleLoader.Load <Object>(runtimePath); } sw.Stop(); if (!obj) { UnityEngine.Debug.LogError("【Object】加载失败:" + runtimePath); } UnityEngine.Debug.LogError("待编写测试! -" + typeName); } //打印 Debug.LogFormat("<color=yellow>{0}</color> <color=green>【加载】:<color=yellow>{1}ms</color>;【初始化】:<color=yellow>{2}ms</color> </color>", loadData.LoadPath, loadData.LoadTime / 10000f, loadData.InstanceTime / 10000f); yield return(null); } yield return(null); // foreach (var item in loadDataMap) // { // Debug.Log("<color=red>【" + item.Key + "】</color>"); // foreach (var ld in item.Value) // { // Debug.LogFormat("<color=yellow>{0}</color> <color=green>【加载】:<color=yellow>{1}ms</color>;【初始化】:<color=yellow>{2}ms</color> </color>", ld.LoadPath, ld.LoadTime / 10000f, ld.InstanceTime / 10000f); // } // } // var content = JsonMapper.ToJson(loadDataMap); FileHelper.WriteAllText(BenchmarkResultPath, content); yield return(null); // #if UNITY_EDITOR // EditorUtility.RevealInFinder(outpath); // #endif }