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

unity从streamingassets拷贝到persistentassets,再从persistentassets用www加载进入场景

时间:2015-01-18 17:13:18      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:unity 资源加载 下载 更新   android   

void OnClick()
    {
        //StartCoroutine(LoadLevel());
        StartCoroutine(LoadFrompersister());
    }


    IEnumerator LoadLevel()
    {
        string url = "file://" + Application.dataPath + "/2.Android.unity3d";
        Debug.Log(url);
        WWW www = WWW.LoadFromCacheOrDownload(url, 3);
        yield return www;
        if (www.error != null) yield return null;
        AssetBundle b = www.assetBundle;


        Application.LoadLevel("2");
    }


    IEnumerator LoadFrompersister()
    {
        try
        {
            FileStream fs = File.Create(Application.persistentDataPath + "/1.txt");
            fs.Close();
            fs = File.Create(Application.persistentDataPath + "/1.txt");
            fs.Close();


            lab.text = Application.persistentDataPath;


        }
        catch (System.Exception ex)
        {
            lab.text = ex.Message;
        }
        
        string des = Application.persistentDataPath + "/2.Android.unity";
        string src =
#if UNITY_EDITOR
        "file:///"+Application.streamingAssetsPath + "/2.Android.unity3d";
        des = des.Replace(‘/‘, ‘\\‘);
#elif UNITY_ANDROID
        "jar:file://" + Application.dataPath + "!/assets/2.Android.unity3d";
#endif
        Debug.Log("des:"+des);
        Debug.Log("src:"+src);


        //lab.text = Directory.GetFiles(Application.streamingAssetsPath)[0];
        //string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "1.txt");
        using (WWW w = new WWW(src))
        {
            yield return w;
            if (string.IsNullOrEmpty(w.error))
            {
                while (w.isDone == false) yield return null;


                if (File.Exists(des))
                    File.Delete(des);
                FileStream fs1 = File.Create(des);


                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs1, w.bytes);
                fs1.Close();
            }
            else
            {
                Debug.LogError(w.error);
            }
            
        }


        string downpath = "file:///" + Application.persistentDataPath + "/2.Android.unity";
        Debug.Log("down path:" + downpath);
        using (WWW www = WWW.LoadFromCacheOrDownload(downpath, 7))
        {
            yield return www;
            AssetBundle b = www.assetBundle;


            Application.LoadLevel("2");
            //lab.text = downpath;
        }

unity从streamingassets拷贝到persistentassets,再从persistentassets用www加载进入场景

标签:unity 资源加载 下载 更新   android   

原文地址:http://blog.csdn.net/v2x222/article/details/42836317

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