首页 > 代码库 > unity从streamingassets拷贝到persistentassets,再从persistentassets用www加载进入场景

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

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 =http://www.mamicode.com/
#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加载进入场景