ResourceManager(八)—— 在安卓机子上实现加载AssetBundle
来源:互联网 发布:毕业生户口迁移 知乎 编辑:程序博客网 时间:2024/05/22 01:40
上一篇的demo只能在windows上运行,这一篇看看在安卓上怎么搞。1.首先肯定还是要先点击工具栏Build/BuildAndroid生成assetbundle。2.把Assets/assetbundles里面的东西全拖到Assets/StreamingAssets文件夹下面。3.可以打包运行了。
接下来看看在安卓机上的运行流程:1.从StreamingAssets文件夹下先复制AssetInfo.bytes,Resource.bytes,Version.bytes三个文件到PersistentAssets目录下。2.读取AssetInfo.bytes中保存的路径信息,然后把对应的StreamingAssets下的资源复制PersistentAssets目录下。3.点击button。跳转到TestScene。
就这样吧,手机截图不好上传,就不截图了。
这一篇相对于上一篇只有UpdateSceneScript.cs改变了的:
using System.Collections;using System.Collections.Generic;using UnityEngine;using System.IO;using System.Xml;public class UpdateSceneScript : MonoBehaviour { //这个路径是可以用的,测试过了 string StreamingAssetPath; string PersistentPath; //存放所有资源的路径 List<string> mAllAssetPath = new List<string> (); void Start () { StreamingAssetPath = "jar:file://" + Application.dataPath + "!/assets/"; PersistentPath = Application.persistentDataPath + "/"; //靠AssetInfo.bytes文件是否存在来判断是否已经复制过了 if (File.Exists(PersistentPath + "AssetInfo.bytes")) { Debug.Log ("files exist already, do not neead to copy files from StreamAssetPath to PersistentDataPath!!!"); } //开始复制 else { //1.复制AssetInfo.bytes StartCoroutine (CopyAssetInfoFile ()); //1.复制Resource.bytes StartCoroutine (CopyResourceFile ()); //1.复制Version.bytes StartCoroutine (CopyVersionFile ()); } } //复制AssetInfo.bytes文件 private IEnumerator CopyAssetInfoFile() { Debug.Log ("Copying AssetInfo.bytes File !!!"); WWW www = new WWW (StreamingAssetPath + "AssetInfo.bytes"); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log ("Copying AssetInfo.bytes should not error!!!"); www.Dispose (); } else { //把AssetInfo.bytes写到persitentPath下面去 File.WriteAllBytes (PersistentPath + "AssetInfo.bytes", www.bytes); www.Dispose (); //因为AssetInfo.bytes对于复制其他资源文件是必须的,所以必须先复制了才行 //2.开始读取AssetInfo.bytes内的信息 GetAssetPaths (); } } //复制Resource.bytes文件 private IEnumerator CopyResourceFile() { Debug.Log ("Copying Resource.bytes file !!!"); WWW www = new WWW (StreamingAssetPath + "Resource.bytes"); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log ("Copying Resource.bytes should not error!!!"); www.Dispose (); } else { //把AssetInfo.bytes写到persitentPath下面去 File.WriteAllBytes (PersistentPath + "Resource.bytes", www.bytes); www.Dispose (); } } //复制Version.bytes文件 private IEnumerator CopyVersionFile() { Debug.Log ("Copying Version.bytes file !!!"); WWW www = new WWW (StreamingAssetPath + "Version.bytes"); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log ("Copying Version.bytes should not error!!!"); www.Dispose (); } else { //把AssetInfo.bytes写到persitentPath下面去 File.WriteAllBytes (PersistentPath + "Version.bytes", www.bytes); www.Dispose (); } } //获取AssetInfo.bytes中路径信息 private void GetAssetPaths() { if (File.Exists(PersistentPath + "AssetInfo.bytes")) { FileStream fs = File.Open (PersistentPath + "AssetInfo.bytes", FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader assetInfoSW = new StreamReader (fs, System.Text.Encoding.Default); XmlDocument doc = new XmlDocument (); doc.LoadXml (assetInfoSW.ReadToEnd ()); XmlElement root = doc.DocumentElement; IEnumerator iter = root.GetEnumerator (); while (iter.MoveNext()) { XmlElement child_root = iter.Current as XmlElement; string name = child_root.GetAttribute ("name"); mAllAssetPath.Add (name); } fs.Close (); assetInfoSW.Close (); Debug.Log ("Get all assets path completed!!!"); } Debug.Log ("Start Copy ! ! !"); //3.开始复制 StartCopy (); Debug.Log ("ALl files copied completed"); } void OnGUI() { //6.全部加载完了就开始加载TestScene if (GUI.Button(new Rect(10, 10, 100, 100), "button")) { //完成初始化 ResourcesManager.Instance.Init (); //加载场景 ResourcesManager.Instance.loadLevel ("Scenes/TestScene", null); } } //开始资源文件的复制 private void StartCopy() { //文件路径 string file = mAllAssetPath [0]; mAllAssetPath.RemoveAt (0); //4.www加载 StartCoroutine (WwwDownload (file, delegate(WWW www) { //5.www加载好了就开始复制 CopyToPersistent(file, www); //销毁 www.Dispose(); //递归copy StartCopy(); })); } private delegate void HandleCopyCompleted(WWW www); //WWW加载StreamingAssets下的资源 private IEnumerator WwwDownload(string filePath, HandleCopyCompleted handle) { //源路径 string filePathSrc = StreamingAssetPath + filePath + ".bytes"; WWW www = new WWW (filePathSrc); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log ("should not error!!!"); www.Dispose (); } else { if (handle != null) { handle (www); } //www.Dispose (); } } //复制操作 private void CopyToPersistent(string filePath, WWW www) { //目标路径 string filePathDest = PersistentPath + filePath + ".bytes"; string filePathDestFolder = ResourceCommon.getPath (PersistentPath + filePath); if (!Directory.Exists(filePathDestFolder)) { Directory.CreateDirectory (filePathDestFolder); Debug.Log ("Create Directory!!!"); Debug.Log ("filePathDestFolder: + " + filePathDestFolder); } File.WriteAllBytes (filePathDest, www.bytes); }}
阅读全文
0 0
- ResourceManager(八)—— 在安卓机子上实现加载AssetBundle
- ResourceManager(七)—— 打包AssetBundle然后从AssetBundle加载资源
- 在Android机子上进行AssetBundle 读取
- AssetBundle 在Android机子上进行读取
- Unity3D - AssetBundle 在Android机子上进行读取
- Unity3D - AssetBundle 在Android机子上进行读取
- Unity3D - AssetBundle 在Android机子上进行读取
- [Unity3d]安卓无法加载assetbundle的问题
- [Unity3d]安卓无法加载assetbundle的问题
- 在没有.net环境的机子上运行C#程序——方法一
- AssetBundle在安卓中的解析的教程
- vc 加载绝对路径图片的程序怎么能在别人的机子上运行
- Unity3d的AssetBundle打包——AssetBundle Browser(简单实现资源复用)
- AssetBundle-----资源加载(读取assetbundle)
- 安卓自定义ListView实现上拉加载、下拉刷新
- 安卓:ListView上拉加载,下拉刷新简单实现
- 安卓实现下拉刷新上拉加载
- ResourceManager(一)—— ArchiveManager
- O
- 为什么struct x1{struct x1 stX};无法通过编译
- 10种简单的Java性能优化
- Node.js——bower简明入门教程
- 34.笔记go语言——内建函数
- ResourceManager(八)—— 在安卓机子上实现加载AssetBundle
- vs的使用技巧
- web开发中实现会话跟踪
- verilog简单驱动sram
- 给你的大杀器装备核弹——Windows下安装使用GPU版XGBoot详细参考指南
- 35.笔记go语言——array、slices和map
- URAL 1083|Factorials!!!|暴力
- 用C语言写的一个简单的将数字金钱转换为文字
- java使用tar算法压缩解压缩文件、数据流、byte[]字节数组