码迷,mamicode.com
首页 > 编程语言 > 详细

unity学习 5.x依赖打包和解包

时间:2017-04-23 22:22:59      阅读:508      评论:0      收藏:0      [点我收藏+]

标签:data   查看   转换   rom   4.6   name   depends   文件大小   封装   

unity5已经封装好了接口,所以依赖打包并没有那么神秘和复杂了。


打包:
1.定义好资源的assetBundleName
2.BuildPipeline.BuildAssetBundles,指定资源目录和压缩类型
 
生成:
1.Assetbundle文件,加载时的首要文件,包含所有资源的依赖信息
2.每个文件对应一个.manifest,不用管他,但是可以打开查看他引用了哪些资源。
 
加载:
1.获取AssetBundle文件
2.LoadAsset("AssetBundleManifest")转换为AssetBundleManifest
3.通过manifest.GetAllDependencies("测试文件"),获取它依赖的ab,得到的是AB数组,并下载它
4.最后下载名为(测试文件)的资源即可。
 
测试代码:
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class LoadAssetbundle : MonoBehaviour {  
  5.       
  6.     void Start()  
  7.     {  
  8.         // 1.加载Manifest文件  
  9.         AssetBundle manifestBundle=AssetBundle.CreateFromFile(Application.dataPath +"/ab/Assetbundle");  
  10.         if(manifestBundle != null)  
  11.         {  
  12.             AssetBundleManifest manifest = (AssetBundleManifest)manifestBundle.LoadAsset("AssetBundleManifest");  
  13.             // 2.获取依赖文件列表  
  14.             string[] cubedepends = manifest.GetAllDependencies("assets/res/1.prefab");  
  15.             AssetBundle[] dependsAssetbundle = new AssetBundle[cubedepends.Length];  
  16.             for(int index = 0; index < cubedepends.Length; index++)  
  17.             {  
  18.                 // 3.加载所有的依赖资源  
  19.                 dependsAssetbundle[index]=AssetBundle.CreateFromFile(  
  20.                     Application.dataPath +"/../Assetbundle/"+cubedepends[index]);  
  21.             }  
  22.   
  23.             // 4.加载资源  
  24.             AssetBundle cubeBundle=AssetBundle.CreateFromFile(  
  25.                 Application.dataPath +"/ab/assets/res/1.prefab" );  
  26.             GameObject cube=cubeBundle.LoadAsset("1") as GameObject;  
  27.             if(cube!=null)  
  28.             {  
  29.                 Instantiate(cube);  
  30.             }  
  31.         }  
  32.     }  
 
坑tips:
如果材质球的名称和它引用的贴图名称一样,材质球内存占有量就会像包含了贴图的内存一样。
 
换版本tips:
4.6项目移植到5.0.2,ab包无法加载,重新打包即可。
不改打包代码,多数文件大小增大2k
改为最新打包代码,不做依赖,多数文件大小增大2-4%左右
 
tips:
如果做依赖,会生成很多零碎的文件,开发期不建议使用,以免增加不必要的工作量。和美术定义好资源规范才是重点。(个人意见,不喜随你便)

unity学习 5.x依赖打包和解包

标签:data   查看   转换   rom   4.6   name   depends   文件大小   封装   

原文地址:http://www.cnblogs.com/gasolhome/p/6754120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!