Unity AssetBundles 使用指南
2015-12-17 13:10
447 查看
0x00:简介
AssetBundles 是Unity使用的一种资源格式,AssetBundles资源可以在不同项目交叉单独使用,Unity中主要用AssetBundles使资源和可执行文件分离。0x01:生成AssetBundles
AssetBundle可以调用Unity接口:BuildPipeLine.BuildAssetBundle(Object mainAsset, Object[] assets, string pathName, BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget targetPlatform = BuildTarget.WebPlayer)
0x02:加载AssetBundles
1、AssetBundles存储路径:
Streaming Assets
通过路径访名可以访问,游戏中如果有视频文件,必须放在这个路径。根目录通过Application.streamingAssetsPath 来访问不同平台对应的路径
path = "file://" + Application.dataPath + "/StreamingAssets"; //pc path = "file://" + Application.dataPath + "/Raw"; // ios path = "jar:file://" + Application.dataPath + "!/assets/"; // android
PersistentDataPath
此路径可以保存数据,每次安装应用不会覆盖此目录加载AssetBundle通过
path = "file://" + Application.persistentDataPath;
Temporary CachePath
此路径保存零时文件,可能会被OS清除加载AssetBundle通过
path = "file://" + Application.temporaryCachePath;
2、AssetBundles加载方式
IEnumerator LoadAssetBundle(string fileUrl, LoadCallback callBack) { WWW www = new WWW(fileUrl); yield return www; if(www.error != null) { callBack(www.assetBundle); } www.Dispose(); www = null; }
0x03:自动化生成AssetBundle
对于生成个别资源手动写代码很容易,但对于大中型项目,AssetBundle需要随时生成任何一个目录 ,这时需要完整的自动化生成流程。1、switch 平台
2、加载生成AssetBundle 目录配置文件
3、生成AssetBundle
如果AssetBundle只用于打包补丁,指定Resources目录的文件生成相应的AssetBundle即可,当游戏启动的时候,资源会下载在可写目录。这时可以通过下面的方式来判断是否加载新的资源:
public bool IsNewAssetExist(string filePath) { string fileFullPath = Path.Combine(Application.dataPath, filePath); if (File.Exists(fileFullPath)) { return true; } return false; }
更多应给是将这些可变的资源放置到Assets目录的其他文件夹,然后生成AssetBundle到StreamingAssets文件下,这样的好处一个是Resources文件下文件少,容易整理,因为Unity会把Resources低下所有文件打包到App里,所以这样的资源管理方式也有助于Unity底下的资源整理。同时将这部分资源打包分离到外部,不打入app里,而是通过下载到本地可写目录,结果就是可以让安装包很小,对于Google Play 这绝对是最好的消息。如果资源一开始打包到StreamingAssets里,通过如下方式加载:
private string StreamingPath { get { if (Application.platform == RuntimePlatform.Android) { return "jar:file://" + Application.dataPath + "!/assets/"; } else if (Application.platform == RuntimePlatform.IPhonePlayer) { return "file://" + Application.dataPath + "/Raw/"; } else { return "file://" + Application.dataPath + "/StreamingAssets/"; } } } public string GetAssetPath(string filePath) { string fileFullPath = Path.Combine(Application.dataPath, filePath); if (File.Exists(fileFullPath)) { return "file://" + fileFullPath; } return StreamingPath + filePath; }
0x04:AssetBundle 的优缺点
优点
1、减小安装包大小2、异步加载
3、更新资源
缺点
1、逻辑复杂度2、增加下载功能
3、资源加载逻辑
4、资源卸载逻辑
5、加载时序问题解决
6、添加生成AssetBundle逻辑
注意
1、下载的AssetBundle需要设置Iphone.SetNoBackupFlag()2、不同平台下的AssetBundle不能通用
3、如果资源不放在Resources,那Unity Editor模式下如何加载资源,可以使用:
public static Object LoadAssetAtPath(string assetPath, Type type);
0x05:总结
目前市场上很多游戏都需要支持打补丁包,对于资源补丁包,Unity提供的方式就是AssetBundle,这篇文章里对于很多涉及到细节并不是面面俱到,比如资源都是通过携程加载,如果正在加载中,再重复加载同样的资源会如何?如何加载一个资源列表?这些东西只有在具体项目中,才会有深入的体会。同时在做Unity项目中,最好做到对Unity的帮助文档非常熟悉,并且动手将Unity提供的接口进行了实验,这样在做Unity项目的时候才能做到好的抉择,无论是性能效率方面,还是资源管理方面,就可以做到游刃有余。
相关文章推荐
- Unity AssetBundles 使用指南
- Unity Quaternion(四元数) 使用认识
- unity5地形lightmap阴影bug
- unity中通过SVN进行版本控制
- Unity3D Physics.Raycast 射线检测
- Unity UGUI RectTransform图解
- unity OnDrawGizmosSelected只在scene起作用
- unity3d-背景层设置
- 使用Unity3D的50个技巧:Unity3D最佳实践
- Unity 点乘(Dot)、叉乘(Cross)判断移动方向、朝向等向量问题
- unity5 animator过渡速度变化
- Unity3D -- 保存数据到excel
- 懵懵懂懂开始学习Unity
- Unity常见问题集(待续)
- Unity3D -- 从excel文件中读取数据
- unity3d结合轮廓显示,实现完整的框选目标(附Demo代码)
- Unity Text 插入超链接
- Unity3d 工具打包完 自动上传到FTP服务器
- Unity 点击模型上任意位置,模型旋转,点击位置朝向摄像机
- unity3d 嵌入iOS的 In App Purchase 应用程序内购买