初探AssetBundle
来源:互联网 发布:剑三捏脸数据百度云 编辑:程序博客网 时间:2024/06/03 21:59
首先来说说神马是AssetBundle,AssetBundle其实就是一个存在于硬盘中的文件或者说是一个压缩包。
其次来说说AssetBundle的工作流程,其实AssetBundle的工作流程看下面两张图就大概能理解了。
首先,(苦逼的程序员)将资源打包成AssetBundle包,然后将AssetBundle包上传到服务器,游戏玩家在进入游戏发现有更新时,从服务器上下载Asset Bundle包完成更新。
接下来就进入正题了,怎样将资源打包成AssetBundle包呢?
首先创建一个unity3d项目,然后创建一个物体(如一个Cube),将这个Cube制作成一个Prefab,点击这个Prefab,在unity的右下角就会出现这样的一个东东:
前面是AssetBundle的名字,后面是后缀,后缀以任何形式都可以。然后创建一个C#脚本,脚本必须放在Editor文件夹中(在Assets目录下创建一个就好了),将这段代码copy进去
[MenuItem("Assets/Build AssetBundles")] static void BuildAllAssetBundles() { string dir = "AssetBundles"; if(Directory.Exists(dir)==false) { Directory.CreateDirectory(dir); } BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows); }
注意,类里面只放这段代码,而且类不需要继承MonoBehaviour,命名空间只需引入UnityEditor,保存后回到unity中,点击左上角的Assets就会出现刚刚代码添加的Build AssetBundle。点击Build AssetBundle unity就会自动打包添加了AssetBundle标签的资源。打包完成后,找到AssetBundles目录,点开就会发现里面多了些东西,这些东西当然就是刚刚打包进来的东西咯
接下来说说怎样加载本地的Asset Bundle包和包里的资源:
一:从内存中进行加载:LoadFromMemory(同步),LoadFromMemoryAsync(异步)
(1) LoadFromMemory:
string sharepath = "AssetBundles/share.unity3d"; string wallpath = "AssetBundles/cubewall.unity3d"; AssetBundle share = AssetBundle.LoadFromMemory(File.ReadAllBytes(sharepath)); AssetBundle wall = AssetBundle.LoadFromMemory(File.ReadAllBytes(wallpath)); GameObject wallPrefab = wall.LoadAsset<GameObject>("CubeWall"); Instantiate(wallPrefab);
(2) LoadFromMemoryAsync: (返回值为IEnumerator)
string sharepath = "AssetBundles/share.unity3d"; string wallpath = "AssetBundles/cubewall.unity3d"; AssetBundleCreateRequest share = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(sharepath)); AssetBundleCreateRequest wall = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(wallpath)); yield return wall; AssetBundle shareAB = share.assetBundle; AssetBundle wallAB = wall.assetBundle; GameObject wallPrefab = wallAB.LoadAsset<GameObject>("CubeWall"); Instantiate(wallPrefab);
二:从本地进行读取:LoadFromFile(同步), LoadFromFileAsync(异步)
(1)LoadFromFile:
string sharepath = "AssetBundles/share.unity3d"; string wallpath = "AssetBundles/cubewall.unity3d"; AssetBundle share = AssetBundle.LoadFromFile(sharepath); AssetBundle wall = AssetBundle.LoadFromFile(wallpath); GameObject wallPrefab = wall.LoadAsset<GameObject>("CubeWall"); Instantiate(wallPrefab);(二)LoadFromFileAsync:(返回值为IEnumerator)
string sharepath = "AssetBundles/share.unity3d"; string wallpath = "AssetBundles/cubewall.unity3d"; AssetBundleCreateRequest share = AssetBundle.LoadFromFileAsync(sharepath); AssetBundleCreateRequest wall = AssetBundle.LoadFromFileAsync(wallpath); yield return wall; AssetBundle shareAB = share.assetBundle; AssetBundle wallAB = wall.assetBundle; GameObject wallPrefab = wallAB.LoadAsset<GameObject>("CubeWall"); Instantiate(wallPrefab);
三: 以WWW的方式进行读取:WWW.LoadFromCacheorDownload(返回值为IEnumerator)
string uri = @"file:///F:\unity3D\assertBundleLearning\AssetBundles\share.unity3d"; string uri2 = @"file:///F:\unity3D\assertBundleLearning\AssetBundles\cubewall.unity3d"; while (Caching.ready == false) { yield return null; } WWW www = WWW.LoadFromCacheOrDownload(uri, 1); yield return www; if (string.IsNullOrEmpty(www.error) == false) { Debug.Log(www.error); yield break; } AssetBundle share = www.assetBundle; www = WWW.LoadFromCacheOrDownload(uri2, 1); yield return www; if (string.IsNullOrEmpty(www.error) == false) { Debug.Log(www.error); yield break; } AssetBundle wall = www.assetBundle; GameObject wallPrefab = wall.LoadAsset<GameObject>("CubeWall"); Instantiate(wallPrefab);
四: 使用UnityWebRequest来进行读取(返回值为IEnumerator)
string uri = @"file:///F:\unity3D\assertBundleLearning\AssetBundles\cubewall.unity3d"; UnityWebRequest wall = UnityWebRequest.GetAssetBundle(uri); yield return wall.Send(); AssetBundle wallAB = DownloadHandlerAssetBundle.GetContent(wall); // 或 AssetBundle wallAB = wall.downloadHandler as DownloadHandlerAssetBundle). assetBundle; GameObject wallPrefab = wallAB.LoadAsset<GameObject>("CubeWall"); Instantiate(wallPrefab); AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles"); AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); string[] strs = manifest.GetDirectDependencies("CubeWall.unity3d"); foreach (string name in strs) { AssetBundle.LoadFromFile("AssetBundles/" + name); }
好了,AssetBundle的加载暂时就降到这里,剩下的没讲到的下次在继续将了,拜拜。
阅读全文
0 0
- 初探AssetBundle
- AssetBundle
- AssetBundle
- AssetBundle
- AssetBundle
- AssetBundle
- AssetBundle
- assetbundle
- AssetBundle
- AssetBundle
- AssetBundle
- AssetBundle
- AssetBundle
- AssetBundle测试
- 加载AssetBundle
- 制作AssetBundle
- AssetBundle杂谈
- 关于AssetBundle
- 文摘:跑了1135 家制造企业,我终于明白它们是怎么把自己玩死的了
- Leetcode:Median of Two Sorted Arrays
- hdu2577(基础dp)
- JavaScript DOM操作
- Java程序---学生管理系统的登录和注册
- 初探AssetBundle
- dword ptr指令详细解析[转载]
- 2017秋招总结
- Python的iterable与iterator
- listview分页加载
- BUG总结(2017.11.11)
- 关于安装Win8.1与ArchLinux双系统
- 《JavaScript高级程序设计》--Function类型笔记
- 1043. 输出PATest(20)