Unity热更新学习(一) —— AssetBundle 打包和加载

理论不多说,网上,官方文档都有。  这里有一篇介绍很全面的文章:https://www.cnblogs.com/ybgame/p/3973177.html

示例和注意点记录一下,用到时以便查阅。

 

一、打包代码(Editor)

我所知道的,有两种打包方法:1、所有打包参数在代码里设置,完全代码控制;2、在编辑器中设置打包参数,代码简便

第一种:

在代码中指定bundle包名,指定包含在该bundle里的所有资源路径

 1 [MenuItem("AssetsBundle/Build Bundle -- 代码中设置参数")]
 2     static void BuildAB()
 3     {
 4         string path = Application.streamingAssetsPath + "/AssetsBundles/";
 5 
 6         if (Directory.Exists(path))
 7             Directory.Delete(path, true);
 8         Directory.CreateDirectory(path);
 9 
10         AssetBundleBuild[] buildMap = new AssetBundleBuild[2];
11 
12         //指定bundle包名
13         buildMap[0].assetBundleName = "prefab_bundle";
14 
15         string[] assets = new string[2];
16         assets[0] = "Assets/AssetsBundleObj/Cube.prefab"; // 必须是完整的文件名(包括后缀)
17         assets[1] = "Assets/AssetsBundleObj/Sphere.prefab";
18         //指定bundle包含的资源路径数组
19         buildMap[0].assetNames = assets;
20 
21         buildMap[1].assetBundleName = "scene_bundle";
22         string[] scenes = new string[1];
23         scenes[0] = "Assets/_Scenes/Scene.unity";
24         buildMap[1].assetNames = scenes;
25 
26         BuildPipeline.BuildAssetBundles(path, buildMap, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows);
27     }
代码中设置参数

第二种:

先在编辑器中设置参数,选中要打包的对象,设置好包名,后缀。

 

然后就直接打bundle包了,无需再在代码中处理包名之类的参数。

 1 [MenuItem("AssetsBundle/Build Bundle -- 编辑器中设置参数")]
 2     static void BuildAB2()
 3     {
 4         string path = Application.streamingAssetsPath + "/AssetsBundles/";
 5 
 6         if (Directory.Exists(path))
 7             Directory.Delete(path, true);
 8         Directory.CreateDirectory(path);
 9 
10         BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows);
11     }
编辑器中设置参数

 

二、加载代码(runtime)

通过www方法加载bundle,并把加载出来的资源实例化。

 1 void OnGUI()
 2     {
 3         if (GUILayout.Button("加载预制体Cube"))
 4         {
 5             StartCoroutine(LoadObj("prefab_bundle", "cube.prefab"));//有没有.prefab后缀都正常加载
 6         }
 7         if (GUILayout.Button("加载预制体Sphere"))
 8         {
 9             StartCoroutine(LoadObj("prefab_bundle", "sphere"));
10         }
11     }
12     /// <summary>
13     /// 加载预制体
14     /// </summary>
15     /// <param name="name"></param>
16     /// <returns></returns>
17     IEnumerator LoadObj(string bundle_name, string name)
18     {
19         string path = "file://" + Application.streamingAssetsPath + "/AssetsBundles/" + bundle_name;
20         //Debug.LogError(string.Format("obj  {0}", path));
21         WWW www = new WWW(path);
22         yield return www;
23         if (www.error == null)
24         {
25             AssetBundle bundle = www.assetBundle;
26             //这里LoadAsset第二个参数 有没有都能正常运行,这个类型到底什么用途还有待研究
27             GameObject go = Instantiate(bundle.LoadAsset(name, typeof(GameObject))) as GameObject;
28             //go.transform.parent = transform;
29             // 上一次加载完之后,下一次加载之前,必须卸载AssetBundle,不然再次加载报错:
30             //    The AssetBundle 'Memory' can't be loaded because another AssetBundle with the same files is already loaded
31             bundle.Unload(false);
32         }else
33         {
34             Debug.LogError(www.error);
35         }
36     }
加载预制体测试

 

 

转载于:https://www.cnblogs.com/yougoo/p/9769156.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AssetBundle 引用计数是指在使用 AssetBundle 打包资源的时候,记录每个 AssetBundle 被使用的次数,以便在使用完后及时释放资源,从而避免内存泄漏。在使用 AssetBundle 加载资源时,会对加载的 AssetBundle 进行引用计数的增加,使用完后再进行引用计数的减少,当引用计数为 0 时,就可以释放该 AssetBundle 的资源。 AssetBundle打包加载一般分为以下几个步骤: 1. 打包资源文件:使用 Unity Editor 自带的 AssetBundle 打包工具,将需要打包的资源文件进行打包,生成 AssetBundle 文件。 2. 加载 AssetBundle 文件:在游戏运行时,使用 Unity 提供的 AssetBundle.LoadFromFile 或 AssetBundle.LoadFromMemory 函数来加载 AssetBundle 文件。 3. 加载资源文件:使用加载的 AssetBundle,使用 AssetBundle.LoadAsset 或 AssetBundle.LoadAssetAsync 函数加载需要使用的资源文件。 4. 使用完成后,释放资源:使用 AssetBundle.Unload(false) 函数来释放 AssetBundle 中的资源,同时进行引用计数的减少。如果不再需要该 AssetBundle 中的任何资源,可以使用 AssetBundle.Unload(true) 函数来彻底释放该 AssetBundle,包括清除 AssetBundle 的缓存。 使用 AssetBundle 打包加载资源可以有效地减少应用程序的内存占用,提高应用程序的性能。同时,在使用 AssetBundle 的时候,需要注意避免重复加载同一个 AssetBundle,以及及时释放不再使用的 AssetBundle 资源,避免内存泄漏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值