• AssetBundle实现服务器下载并从本地读取


    废话不多说  直接上代码.

    从服务器下载的, 很简单

    private IEnumerator Start()
    {
    byte[] ab = null;
    int len = 0;
    WWW www =null;
    foreach (string item in Directory.GetFiles("TestAssetBundles"))
    {
    www = new WWW(@"http://localhost:15109/TestAssetBundles/"+item.Split('\')[1]);
    yield return www;
    if (www.isDone)
    {
    ab = www.bytes;
    len = ab.Length;
    }
    // www.Dispose();

    Stream str = null;
    FileInfo file = new FileInfo(Application.persistentDataPath + "/"+item.Split('\')[1]);

    if (file.Exists)
    {
    file.Delete();
    str = file.Create();
    }

    if (!file.Exists)
    {
    str = file.Create();
    }

    str.Write(ab, 0, len);

    Debug.Log(item.Split('\')[1]);
    str.Close();
    str.Dispose();
    www.Dispose();
    www = null;
    }

    www.assetBundle.Unload(true);

    从下载的路径读取

    string index;


    private void Start()
    {
    index = GameObject.FindWithTag("SceneTag").name.Split('_')[1];
    StartCoroutine(LoadScene());
    }

    public IEnumerator LoadScene()
    {
    UnityWebRequest request = UnityWebRequest.GetAssetBundle(@"file://" + Application.persistentDataPath + "/" + "TestAssetBundles"); // 获取主assetbundle 和打包时候生成的文件夹的名字相同

    yield return request.Send(); //开始获取

    AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request); //得到主assetbundle的对象

    AssetBundleManifest abmanifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); //根据主assetbundle 找到主manifest 必须叫这个名

    string[] dependencies = abmanifest.GetAllDependencies("uiroot" + index + ".unity3d"); //根据abm拿到参数的assetbundle的关联的assetbundle
    List<AssetBundle> test = new List<AssetBundle>();
    foreach (var item in dependencies) //遍历所有关联assetbundle的名字
    {

    UnityWebRequest requestchild = UnityWebRequest.GetAssetBundle(@"file://" + Application.persistentDataPath + "/" + item); //获取这些关联的assetbundle
    yield return requestchild.Send();

    AssetBundle tempAB = DownloadHandlerAssetBundle.GetContent(requestchild);//因为不需要实例关联assetbundle这些物体(这里是一些图集) 所以只需要将他们生成到项目中就可以了,不需要拿到ab实例?????????????????这是绝对不行了, 因为你没有办法在加载后卸载!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    print(tempAB);
    test.Add(tempAB);
    requestchild.Dispose();
    requestchild = null;

    }


    UnityWebRequest requestRoot = UnityWebRequest.GetAssetBundle(@"file://" + Application.persistentDataPath + "/" + "uiroot" + index + ".unity3d");
    yield return requestRoot.Send();
    AssetBundle abRoot = DownloadHandlerAssetBundle.GetContent(requestRoot);
    GameObject go = abRoot.LoadAsset<GameObject>("UI Root" + index);

    foreach (AssetBundle item in test)
    {
    item.Unload(false);
    }

    //request.Dispose();
    //requestRoot.Dispose();
    ab.Unload(false);
    abRoot.Unload(false);
    Instantiate(go);

    ok在上班就不解释太多了

    本博客所有内容均为原创,转载请注明出处.
  • 相关阅读:
    技术网址收藏
    解决IE兼容模式的方案
    C/S通信模型与B/S通信模型介绍
    asp.net 常用于客户端注册的机器信息
    Ado.Net,关于DataSet和DataTable
    WinForm程序开发
    发送邮件-成功
    css背景全屏-视差
    ASP.NET中的随机密码生成
    javascript 替换 window.onload 的 document.ready
  • 原文地址:https://www.cnblogs.com/what-lee/p/6680967.html
Copyright © 2020-2023  润新知