/// <summary> /// 测试加载所有的AssetBundle /// </summary> static public void TestLoadAssetbundleOnEditor(string abPath) { //打开场景、运行 EditorSceneManager.OpenScene(ScenePath); //运行场景 //EditorApplication.ExecuteMenuItem("Edit/Play"); //执行 //初始化加载环境 UnityEngine.AssetBundle.UnloadAllAssetBundles(true); //dev加载器 DevLoder = new DevResourceMgr(); DevLoder.Init(""); AssetBundleLoader = new AssetBundleMgrV2(); AssetBundleLoader.Init(abPath); //节点 UI_ROOT = GameObject.Find("UIRoot").transform; SCENE_ROOT = GameObject.Find("3dRoot").transform; //相机 Camera = GameObject.Find("Camera").GetComponent <Camera>(); Camera.cullingMask = -1; Camera.gameObject.hideFlags = HideFlags.DontSave; //获取gameview var assembly = typeof(UnityEditor.EditorWindow).Assembly; System.Type GameViewType = assembly.GetType("UnityEditor.GameView"); GameView = EditorWindow.GetWindow(GameViewType); //开始加载 EditorCoroutineExtensions.StartCoroutine(IE_LoadAll(), new object()); }
public LoadTaskGroup(AssetBundleMgrV2 loder, Type type, string mainAssetLoadPath, AssetBundleItem mainAssetBundleItem) { //赋值 this.loder = loder; this.MainAssetType = type; this.MainAssetBundleLoadPath = mainAssetLoadPath; this.MainAssetBundleItem = mainAssetBundleItem; //1.依赖资源队列 var dependAssetList = loder.AssetConfigLoder.GetDependAssets(mainAssetBundleItem); if (dependAssetList != null) { waitingLoadAssetBundleList = new List <AssetBundleItem>(dependAssetList.Count + 1); //添加依赖 waitingLoadAssetBundleList.AddRange(dependAssetList); } else { waitingLoadAssetBundleList = new List <AssetBundleItem>(); } //添加主资源 waitingLoadAssetBundleList.Add(mainAssetBundleItem); }
/// <summary> /// 初始化 /// </summary> private void Init() { //初始化加载环境 UnityEngine.AssetBundle.UnloadAllAssetBundles(true); //dev加载器 // DevLoder = new DevResourceMgr(); // DevLoder.Init(""); AssetBundleLoader = new AssetBundleMgrV2(); var abPath = Application.isEditor ? BApplication.DevOpsPublishAssetsPath : Application.persistentDataPath; AssetBundleLoader.Init(abPath); AssetBundleLoader.WarmUpShaders(); //节点 UI_ROOT = GameObject.Find("UIRoot").transform; SCENE_ROOT = GameObject.Find("3dRoot").transform; imageNode = UI_ROOT.transform.GetComponentInChildren <Image>(); spriteRendererNode = SCENE_ROOT.transform.GetComponentInChildren <SpriteRenderer>(); imageNode.gameObject.SetActive(false); spriteRendererNode.gameObject.SetActive(false); //相机 Camera = GameObject.Find("Camera").GetComponent <Camera>(); Camera.cullingMask = -1; Camera.gameObject.hideFlags = HideFlags.DontSave; //获取gameview // var assembly = typeof(UnityEditor.EditorWindow).Assembly; // System.Type GameViewType = assembly.GetType("UnityEditor.GameView"); }
/// <summary> /// 添加混淆 /// </summary> public void MixAssetBundle(string outpath, RuntimePlatform platform) { var mixAssets = GetMixAssets(); //构建ab管理器对象 AssetBundleMgrV2 abv2 = new AssetBundleMgrV2(); abv2.Init(outpath); // var mixAssetbundleItems = abv2.AssetConfigLoder.AssetbundleItemList.Where((i) => mixAssets.Contains(i.AssetBundlePath)).ToArray(); Debug.Log("<color=green>--------------------开始混淆Assetbundle------------------------</color>"); //开始混淆AssetBundle for (int i = 0; i < abv2.AssetConfigLoder.AssetbundleItemList.Count; i++) { //源AB var sourceItem = abv2.AssetConfigLoder.AssetbundleItemList[i]; //非混合文件、ab不存在、mix过 if (mixAssetbundleItems.Contains(sourceItem) || sourceItem.AssetBundlePath == null || sourceItem.Mix > 0) { continue; } var idx = (int)(Random.Range(0, (mixAssetbundleItems.Length - 1) * 10000) / 10000); var mixItem = mixAssetbundleItems[idx]; // var mixBytes = File.ReadAllBytes(IPath.Combine(outpath, BApplication.GetPlatformPath(platform), BResources.ART_ASSET_ROOT_PATH, mixItem.AssetBundlePath)); var abpath = IPath.Combine(outpath, BApplication.GetPlatformPath(platform), BResources.ART_ASSET_ROOT_PATH, sourceItem.AssetBundlePath); var abBytes = File.ReadAllBytes(abpath); //拼接 var outbytes = new byte[mixBytes.Length + abBytes.Length]; Array.Copy(mixBytes, 0, outbytes, 0, mixBytes.Length); Array.Copy(abBytes, 0, outbytes, mixBytes.Length, abBytes.Length); //写入 FileHelper.WriteAllBytes(abpath, outbytes); var hash = FileHelper.GetMurmurHash3(abpath); //相同ab的都进行赋值,避免下次重新被修改。 foreach (var item in abv2.AssetConfigLoder.AssetbundleItemList) { if (sourceItem.AssetBundlePath.Equals(item.AssetBundlePath)) { item.Mix = mixBytes.Length; item.Hash = hash; } } //sourceItem.Mix = mixBytes.Length; //混淆 Debug.Log("【Assetbundle混淆】" + sourceItem.AssetBundlePath); } //重新写入配置 abv2.AssetConfigLoder.OverrideConfig(); Debug.Log("<color=green>--------------------混淆Assetbundle完毕------------------------</color>"); }
public LoaderTaskGroup(AssetBundleMgrV2 loder, string mainAssetName, ManifestItem manifestItem, List <LoaderTaskData> taskList, OnTaskCompleteCallbackDelegate onAllTaskCompleteCallbackDelegate) { this.loder = loder; this.taskList = taskList; //主资源 this.MainAssetName = mainAssetName; this.manifestItem = manifestItem; this.totalTaskNum = taskList.Count; this.OnAllTaskCompleteCallback += onAllTaskCompleteCallbackDelegate; }
/// <summary> /// 初始化 /// </summary> /// <param name="abModel"></param> /// <param name="callback"></param> static public void Init(AssetLoadPathType loadPathType) { BDebug.Log("【BResource】加载路径:" + loadPathType.ToString()); if (loadPathType == AssetLoadPathType.Editor) { #if UNITY_EDITOR //防止编译报错 ResLoader = new DevResourceMgr(); ResLoader.Init(""); #endif } else { var path = GameConfig.GetLoadPath(loadPathType); ResLoader = new AssetBundleMgrV2(); ResLoader.Init(path); } }
/// <summary> /// 初始化 /// </summary> /// <param name="abModel"></param> /// <param name="callback"></param> static public void Load(AssetLoadPath loadPath, string customRoot = null) { if (loadPath == AssetLoadPath.Editor) { #if UNITY_EDITOR //防止编译报错 ResLoader = new DevResourceMgr(); ResLoader.Init(""); BDebug.Log("资源加载:AssetDataBase editor only"); #endif } else { var path = ""; if (Application.isEditor) { if (!string.IsNullOrEmpty(customRoot)) { path = customRoot; } else { if (loadPath == AssetLoadPath.Persistent) { path = Application.persistentDataPath; } else if (loadPath == AssetLoadPath.StreamingAsset) { path = Application.streamingAssetsPath; } } } else { //真机环境config在persistent,跟dll和db保持一致 path = Application.persistentDataPath; } // ResLoader = new AssetBundleMgrV2(); ResLoader.Init(path); } }
public AsyncLoadTaskGroupResult(AssetBundleMgrV2 loder, AssetBundleItem assetBundleItem) { //赋值 this.loder = loder; this.AssetBundleItem = assetBundleItem; //1.依赖资源队列 this.taskQueue = new Queue <LoaderTaskData>(); var dependAssetList = loder.AssetConfigLoder.GetDependAssets(assetBundleItem); foreach (var dependAsset in dependAssetList) { var task = new LoaderTaskData(dependAsset, typeof(Object)); this.taskQueue.Enqueue(task); } //2.主资源添加队列 var mainTask = new LoaderTaskData(assetBundleItem, typeof(Object), true); this.taskQueue.Enqueue(mainTask); }
/// <summary> /// 初始化 /// </summary> /// <param name="abModel"></param> /// <param name="callback"></param> static public void Load(string root = "", Action onLoaded = null) { if (root != "") { var config = GameObject.Find("BDFrame").GetComponent <Config>(); if (config != null) { var version = config.Data.AssetBundleManagerVersion; switch (version) { case AssetBundleManagerVersion.V1: { ResLoader = new AssetBundleMgr(); ResLoader.Init(root, onLoaded); } break; case AssetBundleManagerVersion.V2_experiment: { ResLoader = new AssetBundleMgrV2(); ResLoader.Init(root, onLoaded); } break; } } else { ResLoader = new AssetBundleMgr(); ResLoader.Init(root, onLoaded); } } else { #if UNITY_EDITOR ResLoader = new DevResourceMgr(); ResLoader.Init("", onLoaded); BDebug.Log("资源加载:AssetDataBase editor only"); #endif } }
/// <summary> /// 加载assetbundle 文件 /// </summary> private bool AsyncLoadAssetbundleFile() { //1.loadABFile,循环添加任务 while (!isCancel && AssetBundleMgrV2.IsCanAddGlobalTask && curLoadIdx < waitingLoadAssetBundleList.Count - 1) { curLoadIdx++; var abi = waitingLoadAssetBundleList[curLoadIdx]; //没有被加载过 var abw = loder.GetAssetBundleFromCache(abi.AssetBundlePath); if (abw == null) { //判断是否在加载中 var loadTask = AssetBundleMgrV2.GetExsitLoadTask(abi.AssetBundlePath); if (loadTask != null) { loadingTaskList.Add(new KeyValuePair <string, LoadTask>(abi.AssetBundlePath, loadTask)); } else { //创建任务 var abPath = loder.FindMultiAddressAsset(abi.AssetBundlePath); loadTask = new LoadTask(abPath, 0, (ulong)abi.Mix); //加入Global任务 AssetBundleMgrV2.AddGlobalLoadTask(loadTask); //添加到loading表 loadingTaskList.Add(new KeyValuePair <string, LoadTask>(abi.AssetBundlePath, loadTask)); //开始加载 loadTask.AysncLoad(); BDebug.Log($"【AsyncLoadTaskGroup】 加 载: {abi.AssetBundlePath}"); } } else { BDebug.Log($"【AsyncLoadTaskGroup】 无需加载: {abi.AssetBundlePath}"); } } //2.loadABFile,检测加载状态 if (loadingTaskList.Count > 0) { for (int i = loadingTaskList.Count - 1; i >= 0; i--) { var loadingTask = loadingTaskList[i]; var assetbundleFileName = loadingTask.Key; var loadTask = loadingTask.Value; //判断是否结束 if (loadTask.IsDone) { //添加到返回列表 if (loadTask.AssetBundle != null) { loder.AddAssetBundleToCache(assetbundleFileName, loadTask.AssetBundle); } else { BDebug.LogError("【AsyncLoadTaskGroup】ab资源为空:" + assetbundleFileName); } //移除成功任务 loadingTaskList.RemoveAt(i); //解锁 AssetBundleMgrV2.RemoveGlobalLoadTask(loadTask); BDebug.Log($"【AsyncLoadTaskGroup】--> 加载完成:{assetbundleFileName} 剩余:{loadingTaskList.Count + waitingLoadAssetBundleList.Count - (curLoadIdx + 1)}/{waitingLoadAssetBundleList.Count}"); } } } //3.任务执行完毕检测 if (!isCancel && loadingTaskList.Count == 0 && curLoadIdx == waitingLoadAssetBundleList.Count - 1) { //加载完成,主资源只要保证在 实例化之前加载完毕即可 //加载完则使用 BDebug.Log($"<color=green>【AsyncLoadTaskGroup】所有加载完成:{MainAssetBundleItem.AssetBundlePath}</color>"); return(true); } return(false); }
/// <summary> /// 执行加载 Assetbundle /// </summary> /// <returns>是否继续执行</returns> private bool DoLoadAssetBundle() { //有加载任务或者任务未完成 if (loaderHandleList.Count > 0 || taskQueue.Count > 0) { //1.循环添加任务 while (taskQueue.Count > 0 && loaderHandleList.Count < ASYNC_TASK_NUM) { var task = taskQueue.Dequeue(); //没有被加载过 if (!loder.AssetbundleMap.ContainsKey(task.AssetBundleItem.AssetBundlePath)) { //判断是否在加载中 var loderHandle = AssetBundleMgrV2.GetLoaderHandle(task.AssetBundleItem.AssetBundlePath); // AB_LOAD_LOCK_MAP.TryGetValue(, out var exsitLoadingStatus); if (loderHandle != null) { loaderHandleList.Add(new KeyValuePair <string, AssetBundleCreateRequest>(task.AssetBundleItem.AssetBundlePath, loderHandle)); } else { //开始加载逻辑 var filePath = loder.FindMultiAddressAsset(task.AssetBundleItem.AssetBundlePath); //加载偏移 var abcr = AssetBundle.LoadFromFileAsync(filePath, 0, (ulong)task.AssetBundleItem.Mix); //添加到loding表 AssetBundleMgrV2.LockLoadAssetBundle(task.AssetBundleItem.AssetBundlePath, abcr); //添加到状态表 loaderHandleList.Add(new KeyValuePair <string, AssetBundleCreateRequest>(task.AssetBundleItem.AssetBundlePath, abcr)); } } else { BDebug.Log("【AsyncLoadTaskGroup】--> 已存在depend:" + task.AssetBundleItem); } } //2.检测加载状态 for (int i = loaderHandleList.Count - 1; i >= 0; i--) { var item = loaderHandleList[i]; var assetbundlePath = item.Key; var abcr = item.Value; //判断是否成功 if (abcr.isDone) { //添加到返回列表 if (abcr.assetBundle != null) { loder.AddAssetBundle(assetbundlePath, abcr.assetBundle); } else { BDebug.LogError("【LoadGroup】ab资源为空:" + assetbundlePath); } //移除列表 loaderHandleList.RemoveAt(i); //解锁 AssetBundleMgrV2.UnLockLoadAssetBundle(assetbundlePath); BDebug.Log("【AsyncLoadTaskGroup】--> depend:" + assetbundlePath); } } BDebug.LogFormat("【AsyncLoadTaskGroup】剩余未完成任务:{0} - frame: {1}", loaderHandleList.Count + taskQueue.Count, Time.renderedFrameCount); } else { this.isCompleteLoad = true; //加载完成,主资源只要保证在 实例化之前加载完毕即可 if (!isCancel) { this.OnAllTaskCompleteCallback?.Invoke(this.AssetBundleItem.LoadPath); } } //是否继续执行 return(!this.IsSuccess); }