Unity之AssetBundle5.X平台下载4.X方式打包

xiaoxiao2021-03-01  23

下载资源时,如果在本地下载,需要注意不同平台的下载路径。一般情况下,下载资源后,资源会被保存在本地,等下一次下载时从本地读取。Application.streamingAssetsPath:只读路径,在Unity工程中, 放在StreamingAssets文件夹中的任何资源都将被原样复制到目标设备上的一个特定文件夹中。在任何平台中都可以统一使用Application.streamingAssetsPath 属性来获得这一文件夹路径。安卓平台StreamingAssets文件下的资源加载必须通过www的方式异步加载。StreamingAssets主要用来存放一些二进制文件。Application.persistentDataPath:内容可读写,不过只能运行时才能写入或者读取。提前将数据存入这个路径是不可行的。所以,平常测试时,可以从StreamingAsset中读取二进制文件或者从AssetBundle读取文件来写入PersistentDataPath中。Application.dataPath:这个属性返回的是程序的数据文件所在文件夹的路径,例如在Editor中就是项目的Assets文件夹的路径,通过这个路径可以访问项目中任何文件夹中的资源,但是在移动端它是完全没用。以下代码中,服务器路径为ModelPath,属于本地路径,只用于测试代码。 using System.Collections; using System.IO; using UnityEngine; public class DownLoadASB : MonoBehaviour {     private string ModelPath; //服务器路径     private string LocalPath; //本地路径    void Awake()     { #if UNITY_EDITOR         ModelPath = "file:///" + Application.streamingAssetsPath + "/ MoShuShi.assetbundle";         LocalPath = Application.persistentDataPath + "/Model/ MoShuShi.assetbundle"; #endif #if UNITY_ANDROID         ModelPath = Application.streamingAssetsPath + "/ MoShuShi.assetbundle";         LocalPath = Application.persistentDataPath + "/Model/ MoShuShi.assetbundle"; #endif #if UNITY_IPHONE         ModelPath = "file:///"+Application.streamingAssetsPath + "/ MoShuShi.assetbundle";         LocalPath = Application.persistentDataPath + "/Model/ MoShuShi.assetbundle"; #endif     }     // Update is called once per frame     void Update()     {         if (Input.GetMouseButtonDown(0))         {             if (File.Exists(LocalPath))             {                 StartCoroutine(DownLoadModel("file:///"+LocalPath)); //本地下载             }             else             {                 StartCoroutine(DownLoadModel(ModelPath)); //服务器下载             }         }     }     IEnumerator DownLoadModel(string path)     {         WWW www = new WWW(path);         yield return www;         if (www.error == null)         {             byte[] bytes = www.bytes;             CreateDir(Application.persistentDataPath + "/Model");             if (!File.Exists(LocalPath)) //保存到本地             {                 File.WriteAllBytes(LocalPath, bytes);             }             AssetBundle bundle = www.assetBundle; --------------------------------加载资源方法一:异步加载----------------------------------             AssetBundleRequest request = bundle.LoadAssetAsync("MoShuShi.prefab"); //可以不加后缀             yield return request;             if (request.isDone)             {                 GameObject Model = request.asset as GameObject;                 if (Model != null)                 {                     Instantiate(Model, Vector3.zero, Quaternion.identity);                 }                 else                 {                     print("资源不存在");                 }             } -------------------------------加载资源方法二:同步加载-----------------------------------             GameObject go = bundle.LoadAsset("MoShuShi") as GameObject;             Instantiate(go, Vector3.zero, Quaternion.identity);             bundle.Unload(false);         }         else         {             print(www.error);         }     }     public void CreateDir(string path)     {         DirectoryInfo info = new DirectoryInfo(path);         if (!info.Exists)         {             info.Create();         }     } } 模拟服务器路径: 项目运行之前:          项目运行之后:   从服务器下载后,存入本地路径:
转载请注明原文地址: https://www.6miu.com/read-3450021.html

最新回复(0)