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
原创粉丝点击