Unity组件开发--AB包打包工具

1.项目工程路径下创建文件夹:ABundles

2.AB包打包脚本:

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;public class AssetBundlePackage {static string[] GetBuildScenes() {List<string> sceneArray = new List<string>();foreach (EditorBuildSettingsScene e in EditorBuildSettings.scenes) {if (e == null) continue;if (e.enabled) sceneArray.Add(e.path);}return sceneArray.ToArray();}[MenuItem("BuildScene/Build")]public static void BuildScene() {//BundleAssetsBundle_Webgl();string folderPath = EditorUtility.OpenFolderPanel("Select Folder", "", "");Debug.Log("Selected Folder: " + folderPath);BuildPipeline.BuildPlayer(new string[] { "Assets/GameStart.unity" }, folderPath, BuildTarget.WebGL, BuildOptions.AutoRunPlayer);}[MenuItem("BuildScene/BuildForMobile")]public static void BuildSceneForMobile(){//BundleAssetsBundle_Webgl();string folderPath = EditorUtility.OpenFolderPanel("Select Folder", "", "");Debug.Log("Selected Folder: " + folderPath);BuildPipeline.BuildPlayer(new string[] { "Assets/GameStartMoibile.unity" }, folderPath, BuildTarget.WebGL, BuildOptions.AutoRunPlayer);}[MenuItem("SceneAsset/BuildCurrent")]public static void BuildCurrentScene() {string rootPath = Application.dataPath.ToLower().Replace("assets", "") + "ABundles/webgl/scenes";string scenePath = EditorSceneManager.GetActiveScene().path;string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath).ToLower();AssetBundleBuild assetBundleBuild = new AssetBundleBuild();assetBundleBuild.assetNames = new []{ scenePath };assetBundleBuild.assetBundleName = sceneName + ".bundle";BuildPipeline.BuildAssetBundles(rootPath, new AssetBundleBuild[] { assetBundleBuild}, BuildAssetBundleOptions.None, BuildTarget.WebGL);}[MenuItem("SceneAsset/BuildAllScene")]public static void BuildAllScene() {bool isOk = EditorUtility.DisplayDialog("确认框", "是否将所有场景打成AB包", "确认", "取消");if (!isOk) {return;}//AB包路径是ABundlesstring rootPath = Application.dataPath.ToLower().Replace("assets","") + "ABundles/webgl/scenes";var allScenesPath = GetBuildScenes();foreach (var scenePath in allScenesPath) {string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath).ToLower();AssetBundleBuild assetBundleBuild = new AssetBundleBuild();assetBundleBuild.assetNames = new[] { scenePath };assetBundleBuild.assetBundleName = sceneName + ".bundle";Debug.Log(sceneName + scenePath);BuildPipeline.BuildAssetBundles(rootPath, new AssetBundleBuild[] { assetBundleBuild }, BuildAssetBundleOptions.None, BuildTarget.WebGL);}}[MenuItem("AssetBundle/BuildWebGL")]public static void BundleAssetsBundle_Webgl() {Debug.Log("BundleAssetsBundle WebGL");BuildAllAssetBundles();}private static void BuildAssetsBundle(BuildTarget target) {//string packagePath = Application.streamingAssetsPath;//if (packagePath.Length <= 0 && !Directory.Exists(packagePath))//{//    return;//}//BuildPipeline.BuildAssetBundles(packagePath, BuildAssetBundleOptions.UncompressedAssetBundle, target);}//Asset/BundleAsset/Prefab/Com/a.bundle  Prefab/Com/apublic static string RemovePrefix(string inputString) {inputString = inputString.Replace("\\", "/");string prefix = "Assets/BundleAsset/";string result = inputString.Replace(prefix, "");return result.Replace(".bundle", "");}static void BuildAllAssetBundles() {string prefabsFolderPath = "Assets/BundleAsset/Prefab";if (!Directory.Exists(prefabsFolderPath)) {Debug.LogError($"Folder {prefabsFolderPath} does not exist!");return;}//AB包路径是ABundlesstring rootPath = Application.dataPath.ToLower().Replace("assets", "") + "ABundles/webgl";if (!Directory.Exists(rootPath)) {Debug.LogError($"Folder {rootPath} does not exist!");return;}string[] prefabGUIDs = AssetDatabase.FindAssets("t:Prefab", new[] { prefabsFolderPath });foreach (var prefabGUID in prefabGUIDs) {string prefabPath = AssetDatabase.GUIDToAssetPath(prefabGUID);GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);if (prefab == null) {continue;}var assetPath = AssetDatabase.GetAssetPath(prefab);var dependencies = GetAllDependencies(assetPath).ToArray();var withoutEx = Path.GetFileNameWithoutExtension(prefabPath);AssetBundleBuild assetBundleBuild = new AssetBundleBuild();assetBundleBuild.assetBundleName = RemovePrefix(withoutEx).ToLower() + ".bundle";assetBundleBuild.assetNames = dependencies;var directName = Path.GetDirectoryName(assetPath);var outPackagePath = $"{rootPath}/{RemovePrefix(directName).ToLower()}";Debug.Log($"prefabPath {prefabPath}");if (!Directory.Exists(outPackagePath)) {Directory.CreateDirectory(outPackagePath);}BuildPipeline.BuildAssetBundles(outPackagePath, new AssetBundleBuild[] { assetBundleBuild }, BuildAssetBundleOptions.None, BuildTarget.WebGL);}Debug.Log("BuildAssetBundles ok");}public static List<string> GetAllDependencies(string assetPath) {var list = new List<string>();var dependencies = AssetDatabase.GetDependencies(assetPath, false);foreach (var dependency in dependencies) {if (Path.GetExtension(dependency) == ".cs" || Path.GetExtension(dependency) == ".meta" || Path.GetExtension(dependency) == ".DS_Store") {continue;}list.Add(dependency);}list.Add(assetPath);return list;}}

3.需要打包的场景添加到打包配置:

4.unity编辑器生成菜单:

5.场景加载AB包管理器:

using Cysharp.Threading.Tasks;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;
using UnityEngine.Networking;
using GLTFast;
using LitJson;
using System.Web;public class SceneLoader : MonoBehaviour {public int TemplateId;public bool useBundle;public bool isDeBug;public bool isShowCase;public bool isMobileTest;[Header("天空盒材质球")]public UnityEngine.Material skyboxMaterial;bool sceneIsLoaded = false;[DllImport("__Internal")]private static extern string GetUA();public virtual void Awake() {#if !UNITY_EDITOR && UNITY_WEBGLstring a = GetUA();if (a == "1"){//PC端Debug.Log("当前运行环境在PC端");PlayerData.Instance.isRunningPC = true;}if (a == "2"){//移动端Debug.Log("当前运行环境在移动端");PlayerData.Instance.isRunningPC = false;}
#endif#if UNITY_EDITORAppConst.UseAssetBundle = useBundle;#endifAppConst.useShowBundlePath = isShowCase;DontDestroyOnLoad(gameObject);EventManager.Instance.AddListener(EventName.LoadSceneAction, OnSceneLoad);EventManager.Instance.AddListener(EventName.OnSceneCfgLoadEnd, RemoveUnuse);}public virtual void Start() {var fps = transform.Find("FPS");if (fps) {fps.gameObject.SetActive(isDeBug);}if (isMobileTest) LoadNetCofig();}void LoadNetCofig() {var configPath = Application.dataPath;#if UNITY_EDITORvar filepath = Path.Combine(Application.dataPath.Replace("Assets", ""), "config.txt");
#elsevar filepath = Path.Combine(Application.dataPath, "config.txt");
#endifDebug.Log("configPath" + filepath);filepath = filepath.Replace("\\", "/");StartCoroutine(LoadFileSetNetwork(filepath));}IEnumerator LoadFileSetNetwork(string filepath) {UnityWebRequest www = UnityWebRequest.Get(filepath);yield return www.SendWebRequest();if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError) {Debug.LogError(www.error);}else {string json = www.downloadHandler.text;var data = LitJson.JsonMapper.ToObject(json);if ((string)data["AssetBundleIP"] != string.Empty) {Host.AssetBundleIP = (string)data["AssetBundleIP"];}Host.gameServer = (string)data["local"];Host.ApiHost = (string)data["ApiHost"];Host.remote = (string)data["remote"];Debug.Log("url config:" + json);StartCoroutine(tempLoad());}}public IEnumerator tempLoad() {Debug.Log("OnBaelogin" + TemplateId);var SceneName = TemplateId.ToString();
#if UNITY_EDITORif (AppConst.UseAssetBundle) {yield return AssetBundleManager.Instance.LoadSceneSync(SceneName);}else {yield return SceneManager.LoadSceneAsync(SceneName);}
#elseyield return AssetBundleManager.Instance.LoadSceneSync(SceneName);
#endifEventManager.Instance.TriggerEvent(EventName.OnSceneLoaded);UIManager.Instance.PushPanel(UIPanelType.MAIN_PANEL); //这里有个坑, 如果把界面放在场景加载之前添加,会出现各种错误乱象UIManager.Instance.PushPanel(UIPanelType.HUD_PANEL);Debug.Log("DownLoadScenConfig");if (HttpHelper.Instance != null) {HttpHelper.Instance.GetDefaultSpaceImg();HttpHelper.Instance.DownLoadScenConfig();}}private void RemoveUnuse(object sender, EventArgs e) {RemoveSceneUnUseDefault();ResetSkyBox();}public void ResetSkyBox() {JsonData sceneJson = JsonMapper.ToObject(SceneModel.Instance.sceneJsonInitData);if (sceneJson["skyBox"] != null) {string imgdata = sceneJson["skyBox"]["body"].ToString();string decodedString = HttpUtility.UrlDecode(JsonMapper.ToObject(imgdata)["imgDatas"].ToString());StartCoroutine(LoadTexturesAndGenerateCubemap(JsonMapper.ToObject<List<skyImgData>>(decodedString)));}}private IEnumerator LoadTexturesAndGenerateCubemap(List<skyImgData> skyImgDataList) {Texture2D[] textures = new Texture2D[skyImgDataList.Count];Cubemap cubemap;for (int i = 0; i < skyImgDataList.Count; i++) {using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(skyImgDataList[i].url)) {yield return www.SendWebRequest();if (www.result == UnityWebRequest.Result.Success) {Texture2D texture = DownloadHandlerTexture.GetContent(www);textures[i] = texture;}else {Debug.LogError("Failed to load image: " + www.error);yield break;}}}Material material = new Material(skyboxMaterial);material.SetTexture("_FrontTex", textures[0]);material.SetTexture("_BackTex", textures[1]);material.SetTexture("_LeftTex", textures[2]);material.SetTexture("_RightTex", textures[3]);material.SetTexture("_UpTex", textures[4]);material.SetTexture("_DownTex", textures[5]);RenderSettings.skybox = material;}/// <summary>/// 移除场景默认设置的那些被删除的板/// </summary>public void RemoveSceneUnUseDefault() {var comVOs = SceneModel.Instance.rootCfg.comCfg.comVOs;var scene = SceneManager.GetActiveScene();GameObject[] roots = scene.GetRootGameObjects();foreach (GameObject root in roots) {var loaders = root.GetComponentsInChildren<ComLoader>();foreach (var loader in loaders) {if (comVOs.TryGetValue(loader.instanceName, out _) == false) {StartCoroutine(waitSeconds(loader.gameObject));}}}}IEnumerator waitSeconds(GameObject go) {yield return new WaitForEndOfFrame();GameObject.Destroy(go);}IEnumerator coLoadSceneAsync() {if (PlayerData.Instance.isRunningPC == false) {yield return new WaitForSeconds(1f);}#if UNITY_EDITORif (SceneModel.Instance.useGlb == false) {PlayerData.Instance.TemplateId = TemplateId;}
#endifvar SceneName = "";if (SceneModel.Instance.useGlb == true) {SceneName = "1000";}else {if (PlayerData.Instance.isRunningPC) {SceneName = PlayerData.Instance.TemplateId.ToString();}else {SceneName = PlayerData.Instance.TemplateId.ToString() + "_mobile";}}Debug.Log("SceneName TemplateId:" + SceneName);
#if UNITY_EDITORif (AppConst.UseAssetBundle) {yield return AssetBundleManager.Instance.LoadSceneSync(SceneName);}else {yield return SceneManager.LoadSceneAsync(SceneName);}#elseyield return AssetBundleManager.Instance.LoadSceneSync(SceneName);
#endif}/// <summary>/// 发布态场景加载完成/// </summary>/// <param name="arg0"></param>/// <param name="arg1"></param>private void OnPublishModeSceneLoadSuccess(Scene arg0, LoadSceneMode arg1) {UIManager.Instance.PushPanel(UIPanelType.EDITOR_MODE_PANEL);UIManager.Instance.PushPanel(UIPanelType.HUD_PANEL);EventManager.Instance.TriggerEvent(EventName.OnSceneLoaded);HttpHelper.Instance.GetDefaultSpaceImg();SceneModel.Instance.setDefaultSceneConfig();if (PlayerController.Instance.gameObject.GetComponent<RoleInfoUICtr>()) {PlayerController.Instance.gameObject.GetComponent<RoleInfoUICtr>().publicModeForName();PlayerController.Instance.gameObject.GetComponent<RoleInfoUICtr>().isShowOwnerObj(true);}if (SceneModel.Instance.useGlb) {EventManager.Instance.TriggerEvent(EventName.onGlbSceneLoad, new GlbLoadEvenArg { url = SceneModel.Instance.glbPath });}}public void OnSceneLoad(object sender, EventArgs e) {if (sceneIsLoaded == true) {return;}sceneIsLoaded = true;var arg = e as SceneLoadActionArgs;Debug.Log("OnSceneLoad:" + arg.state);if (arg.state == AppConst.PublicMode) //创建态{SceneManager.sceneLoaded += OnPublishModeSceneLoadSuccess;}else { //浏览态SceneManager.sceneLoaded += OnViewSceneLoadOk;}StartCoroutine(coLoadSceneAsync());}/// <summary>/// 浏览态场景加载完成/// </summary>/// <param name="arg0"></param>/// <param name="arg1"></param>private void OnViewSceneLoadOk(Scene arg0, LoadSceneMode arg1) {EventManager.Instance.TriggerEvent(EventName.OnSceneLoaded);if (PlayerData.Instance.isRunningPC == true) {UIManager.Instance.PushPanel(UIPanelType.HUD_PANEL);}//ToastPanel.Show("OnViewSceneLoadOk");//AlertPanel.Show("OnViewSceneLoadOk", null);//Debug.Log("DownLoadScenConfig");//if (HttpHelper.Instance != null)//{//    HttpHelper.Instance.GetDefaultSpaceImg();//    //HttpHelper.Instance.DownLoadScenConfig(); //挪到登陆的时候请求场景数据//}if (SceneModel.Instance.useGlb) {EventManager.Instance.TriggerEvent(EventName.onGlbSceneLoad, new GlbLoadEvenArg { url = SceneModel.Instance.glbPath });}if (PlayerData.Instance.isRunningPC) {SceneModel.Instance.ImplementComLoder();UIManager.Instance.PushPanel(UIPanelType.MAIN_PANEL);}else {if (SceneModel.Instance.useGlb) {EventManager.Instance.AddListener(EventName.onGlbSceneLoadOK, (s, e) => {StartCoroutine(waitSeconds(1, () => {UIManager.Instance.PushPanel(UIPanelType.MAIN_PANEL);}));});StartCoroutine(waitSeconds(1, () => {if (SceneModel.Instance.useGlb) {EventManager.Instance.TriggerEvent(EventName.onGlbSceneLoad, new GlbLoadEvenArg { url = SceneModel.Instance.glbPath });}}));}else {StartCoroutine(waitSeconds(1, () => {UIManager.Instance.PushPanel(UIPanelType.MAIN_PANEL);}));}}}private IEnumerator waitSeconds(float scecond, Action call) {yield return new WaitForSeconds(scecond);call();}public virtual void Onlogin() {}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/611043.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue入门三(表单控制|购物车案例|v-model进阶|与后端交互|计算属性|监听属性|Vue生命周期)

文章目录 一、表单控制二、购物车案例三、v-model进阶四、与后端交互跨域问题解决&#xff0c;三种交互方法跨域问题详解1-CORS&#xff1a;后端代码控制&#xff0c;上面案例采用的方式1) 方式一&#xff1a;后端添加请求头2) 方式二&#xff1a;编写中间件3) 方式三&#xff…

代理IP连接不上?网速过慢?自查与解决方法

当您使用代理时&#xff0c;您可能会遇到不同的代理错误代码显示代理IP连不通、访问失败、网速过慢等种种问题。 在本文中中&#xff0c;我们将讨论您在使用代理IP时可能遇到的常见错误、发生这些错误的原因以及解决方法。 一、常见代理服务器错误 当您尝试访问网站时&#…

Mysql系列-1.Mysql基本使用

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱吃芝士的土豆倪&#xff0c;24届校招生Java选手&#xff0c;很高兴认识大家&#x1f4d5;系列专栏&#xff1a;Spring源码、JUC源码、Kafka原理、分布式技术原理、数据库技术&#x1f525;如果感觉博主的文章还不错的…

RT-Thread基于AT32单片机的485应用开发(二)

在上篇RT-Thread基于AT32单片机的485应用开发&#xff08;一&#xff09;中实现了RS485收发&#xff0c;但总觉得效率不高&#xff0c;函数封装也不完善。考虑到RS485总线应用都是主从式结构&#xff0c;比如工业领域常用的Modbus协议&#xff0c;都是以帧为单位进行收发&#…

【python】内存管理和数据类型问题

一、内存管理 Python有一个自动内存管理机制&#xff0c;但它并不总是按照期望的方式工作。例如&#xff0c;如果创建了一个大的列表或字典&#xff0c;并且没有删除它&#xff0c;那么这个对象就会一直占用内存&#xff0c;直到Python的垃圾回收器决定清理它。为了避免这种情…

【Verilog】运算符

系列文章 数值&#xff08;整数&#xff0c;实数&#xff0c;字符串&#xff09;与数据类型&#xff08;wire、reg、mem、parameter&#xff09; 系列文章算术运算符关系运算符相等关系运算符逻辑运算符按位运算符归约运算符移位运算符条件运算符连接和复制运算符 算术运算符 …

全志T113开发板Qt远程调试

1引言 通常情况下工程师在调试Qt程序时&#xff0c;需要频繁制作镜像烧录到核心板来测试Qt程序是否完善&#xff0c;这样的操作既费时又费力。这时我们可以通过QtCreator设备功能&#xff0c;定义设备后&#xff0c;在x86_64虚拟机上交叉编译qt程序&#xff0c;将程序远程部署到…

【机器学习前置知识】狄利克雷分布

在阅读本文前&#xff0c;建议先食用以下几篇文章以能更好地理解狄利克雷分布&#xff1a; 二项分布 Beta分布 多项分布 共轭分布 狄利克雷分布 狄利克雷分布(Dirichlet distribution)是Beta分布的扩展&#xff0c;把Beta分布从二元扩展到多元形式就是狄利克雷分布&#…

你Go代码写的像“鸭子”吗???

概 述 Go 语言也提供了接口类型&#xff0c;使得我们可以面向接口编程&#xff0c;将实现和接口分离。在我看来&#xff0c;软件的抽象之美也应该以此来表达&#xff0c;和 Java 语言不同的是 Go 并不是那么 “强制”&#xff0c;它使用了一种 鸭子类型 的方式让动态类型成为可…

three.js : tweenjs创建threejs动画

效果&#xff1a; 代码 <template><div><el-container><el-main><div class"box-card-left"><div id"threejs" style"border: 1px solid red"></div> <div class"box-right"><…

超凡脱俗的 sudo

文章目录 超凡脱俗的 sudo语法 没有sudo权限的用户指定用户执行命令列出目前sudo的权限使用sudo快速统计家目录的使用情况更多信息 超凡脱俗的 sudo Linux sudo命令以系统管理者的身份执行指令&#xff0c;也就是说&#xff0c;经由 sudo 所执行的指令就好像是 root 亲自执行。…

认识SpringBoot中的条件注解

✅作者简介&#xff1a;大家好&#xff0c;我是Leo&#xff0c;热爱Java后端开发者&#xff0c;一个想要与大家共同进步的男人&#x1f609;&#x1f609; &#x1f34e;个人主页&#xff1a;Leo的博客 &#x1f49e;当前专栏&#xff1a; 循序渐进学SpringBoot ✨特色专栏&…

虚幻引擎:开创视觉与创意的新纪元

先看看据说虚幻5做出来的东西吧&#xff1a; 虚幻引擎5&#xff01;&#xff01;&#xff01;4K画质PS5实机演示&#xff01; 好了&#xff0c;用文字认识一下吧&#xff1a; 虚幻引擎5.3对UE5的核心工具集作了进一步优化&#xff0c;涉及渲染、世界构建、程序化内容生成&…

【解刊】Elsevier旗下,1区CCF-B,超快审稿:2个月22天录用!

计算机类 • 升区期刊 本次带来Elsevier旗下高分快刊&#xff0c;入选CCF-B类推荐&#xff0c;如有相关领域作者意向投稿&#xff0c;可重点关注&#xff01;更多领域期刊&#xff0c;可移步公众号【Unionpub学术】了解详情~ 01 期刊简介 Computer Networks ✅出版社&#x…

14:00面试,14:07就出来了,问的问题有点变态。。。

前言 刚从小厂出来&#xff0c;没想到在另一家公司我又寄了。 在这家公司上班&#xff0c;每天都要加班&#xff0c;但看在钱给的比较多的份上&#xff0c;也就不太计较了。但万万没想到一纸通知&#xff0c;所有人不准加班了&#xff0c;不仅加班费没有了&#xff0c;薪资还…

回顾基础--HTML篇

HTML语法规范 <html></html> 开始标签与结束标签 <br /> 单标签 包含关系 <head><title></title> </head>并列关系 <head></head> <body></body> 1、 标题标签 标题标签 【双标签】【不同标题字体大小…

计算机网络-各层协议

大家在搞嵌入式开发的时候基本都了解过七层网络协议、五层网络协议、四层网络协议&#xff0c;那么今天让我们更加的深入了解一下&#xff1a; 历史发展介绍 OSI七层模型由ISO国际标准化组织提出的通信标准。TCP/IP四层模型是OSI七层模型的简化版&#xff0c;OSI在它被官方完…

论文阅读1---OpenCalib论文阅读之factory calibration模块

前言 该论文的标定间比较高端&#xff0c;一旦四轮定位后&#xff0c;可确定标定板与车辆姿态。以下为本人理解&#xff0c;仅供参考。 工厂标定&#xff0c;可理解为车辆相关的标定&#xff0c;不涉及传感器间标定 该标定工具不依赖opencv&#xff1b;产线长度一般2.5米 Fa…

浅谈WAF——守护网络安全的无形之盾

随着信息化时代的到来&#xff0c;网络已逐渐融入我们日常生活的方方面面。然而&#xff0c;与此同时&#xff0c;网络安全问题却也如影随形。为此&#xff0c;一种名为“Web应用防火墙”的工具应运而生&#xff0c;简称”WAF”。 WAF是什么&#xff1f; WAF&#xff08;Web …