当前位置:   article > 正文

AssetBundle(2)AssetBundle的加载和使用_loadfrommemoryasync android

loadfrommemoryasync android

四种加载方式

1,AssetBundle.LoadFromMemoryAsync           第一种从内存异步加载 IEnumerator
2,AssetBundle.LoadFromFileAsync                  第二种从本地异步加载 IEnumerator
3,WWW.LoadFromCacheOrDownload             第三种使用www来加载
4,UnityWebRequest                                          第四种使用UnityWebRequest

一、AssetBundle.LoadFromMemoryAsync


  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. public class LoadAssetBundles : MonoBehaviour {
  7. IEnumerator Start ()
  8. {
  9. string rootPath = Application.streamingAssetsPath;
  10. string path = rootPath+ "/AssetBundles/cubewall.unity3d";
  11. //AssetBundle ab1 = AssetBundle.LoadFromFile("AssetBundles/scenes/share.unity3d");
  12. //第一种从内存异步加载 IEnumerator
  13. AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
  14. yield return request;
  15. AssetBundle ab = request.assetBundle;
  16. AssetBundle assetBundleManifest = AssetBundle.LoadFromFile(rootPath+"/AssetBundles/AssetBundles");
  17. AssetBundleManifest manifest = assetBundleManifest.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
  18. //foreach (string name in manifest.GetAllAssetBundles())
  19. //{
  20. // Debug.Log(name);
  21. //}
  22. string[] dependencies = manifest.GetAllDependencies("cubewall.unity3d");
  23. foreach (string str in dependencies)
  24. {
  25. print(str);
  26. AssetBundle.LoadFromFile(rootPath+"/AssetBundles/" + str);
  27. }
  28. //实例化资源
  29. GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");
  30. Instantiate(wallPrefab);
  31. //AssetBundle.UnloadAllAssetBundles(true);
  32. }
  33. }

二、AssetBundle.LoadFromFileAsync

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. public class LoadAssetBundles : MonoBehaviour {
  7. IEnumerator Start ()
  8. {
  9. string rootPath = Application.streamingAssetsPath;
  10. string path = rootPath+ "/AssetBundles/cubewall.unity3d";
  11. AssetBundle ab1 = AssetBundle.LoadFromFile("AssetBundles/scenes/share.unity3d");
  12. //第二种从本地异步加载 IEnumerator
  13. AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
  14. yield return request;
  15. AssetBundle ab = request.assetBundle;
  16. //AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/scenes/cubewall.unity3d");
  17. AssetBundle assetBundleManifest = AssetBundle.LoadFromFile(rootPath+"/AssetBundles/AssetBundles");
  18. AssetBundleManifest manifest = assetBundleManifest.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
  19. //foreach (string name in manifest.GetAllAssetBundles())
  20. //{
  21. // Debug.Log(name);
  22. //}
  23. string[] dependencies = manifest.GetAllDependencies("cubewall.unity3d");
  24. foreach (string str in dependencies)
  25. {
  26. print(str);
  27. AssetBundle.LoadFromFile(rootPath+"/AssetBundles/" + str);
  28. }
  29. //实例化资源
  30. GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");
  31. Instantiate(wallPrefab);
  32. //AssetBundle.UnloadAllAssetBundles(true);
  33. }
  34. }

三、WWW.LoadFromCacheOrDownload

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. public class LoadAssetBundles : MonoBehaviour {
  7. IEnumerator Start ()
  8. {
  9. string rootPath = Application.streamingAssetsPath;
  10. string path = rootPath+ "/AssetBundles/cubewall.unity3d";
  11. AssetBundle ab1 = AssetBundle.LoadFromFile("AssetBundles/scenes/share.unity3d");
  12. //第三种使用www来加载
  13. while (!Caching.ready)
  14. {
  15. yield return null;
  16. }
  17. WWW www = WWW.LoadFromCacheOrDownload(@"file://E:\UnityProjects\Unity5.4\AssetBundleProjects\Assets\StreamingAssets\AssetBundles\AssetBundles\cubewall.unity3d", 1);
  18. //WWW www = WWW.LoadFromCacheOrDownload(@"http://192.168.1.102/AssetBundles\cubewall.unity3d", 1);
  19. yield return www;
  20. if (!string.IsNullOrEmpty(www.error))
  21. {
  22. Debug.Log(www.error);
  23. yield break;
  24. }
  25. AssetBundle ab = www.assetBundle;
  26. AssetBundle assetBundleManifest = AssetBundle.LoadFromFile(rootPath+"/AssetBundles/AssetBundles");
  27. AssetBundleManifest manifest = assetBundleManifest.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
  28. //foreach (string name in manifest.GetAllAssetBundles())
  29. //{
  30. // Debug.Log(name);
  31. //}
  32. string[] dependencies = manifest.GetAllDependencies("cubewall.unity3d");
  33. foreach (string str in dependencies)
  34. {
  35. print(str);
  36. AssetBundle.LoadFromFile(rootPath+"/AssetBundles/" + str);
  37. }
  38. //实例化资源
  39. GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");
  40. Instantiate(wallPrefab);
  41. //AssetBundle.UnloadAllAssetBundles(true);
  42. }
  43. }

四、UnityWebRequest

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. public class LoadAssetBundles : MonoBehaviour {
  7. IEnumerator Start ()
  8. {
  9. string rootPath = Application.streamingAssetsPath;
  10. string path = rootPath+ "/AssetBundles/cubewall.unity3d";
  11. AssetBundle ab1 = AssetBundle.LoadFromFile("AssetBundles/scenes/share.unity3d");
  12. //第四种使用UnityWebRequest
  13. string url = @"file://E:\UnityProjects\Unity5.4\AssetBundleProjects\Assets\StreamingAssets\AssetBundles\cubewall.unity3d";
  14. //string url = @"http://192.168.1.102/AssetBundles\cubewall.unity3d";
  15. UnityWebRequest request = UnityWebRequest.GetAssetBundle(url);
  16. yield return request.Send();
  17. //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
  18. AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  19. AssetBundle assetBundleManifest = AssetBundle.LoadFromFile(rootPath+"/AssetBundles/AssetBundles");
  20. AssetBundleManifest manifest = assetBundleManifest.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
  21. //foreach (string name in manifest.GetAllAssetBundles())
  22. //{
  23. // Debug.Log(name);
  24. //}
  25. string[] dependencies = manifest.GetAllDependencies("cubewall.unity3d");
  26. foreach (string str in dependencies)
  27. {
  28. print(str);
  29. AssetBundle.LoadFromFile(rootPath+"/AssetBundles/" + str);
  30. }
  31. //实例化资源
  32. GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");
  33. Instantiate(wallPrefab);
  34. //AssetBundle.UnloadAllAssetBundles(true);
  35. }
  36. }

加载Manifests

  1. AssetBundle assetBundle = AssetBundle.LoadFromFile(manifestFilePath);
  2. AssetBundleManifest manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
  3. string[] dependencies = manifest.GetAllDependencies("assetBundle"); //Pass the name of the bundle you want the dependencies for.
  4. foreach (string name in manifest.GetAllAssetBundles())
  5. {
  6. Debug.Log(name);
  7. }
  8. foreach (string dependency in dependencies)
  9. {
  10. AssetBundle.LoadFromFile(Path.Combine(assetBundlePath, dependency));
  11. }

AssetBundle的卸载

卸载有两个方面
1,减少内存使用
2,有可能导致丢失

什么时候去卸载资源

AssetBundle.Unload(true)卸载所有资源,即使有资源被使用着
1,在关卡切换、场景切换
2,资源没被用的时候 调用
AssetBundle.Unload(false)卸载所有没用被使用的资源

个别资源怎么卸载

1,通过 Resources.UnloadUnusedAssets.
2,场景切换的时候

关于文件校验

CRC MD5 SHA1

相同点:
CRC、MD5、SHA1都是通过对数据进行计算,来生成一个校验值,该校验值用来校验数据的完整性。
不同点:
1. 算法不同。CRC采用多项式除法,MD5和SHA1使用的是替换、轮转等方法;
2. 校验值的长度不同。CRC校验位的长度跟其多项式有关系,一般为16位或32位;MD5是16个字节(128位);SHA1是20个字节(160位);
3. 校验值的称呼不同。CRC一般叫做CRC值;MD5和SHA1一般叫做哈希值(Hash)或散列值;
4. 安全性不同。这里的安全性是指检错的能力,即数据的错误能通过校验位检测出来。CRC的安全性跟多项式有很大关系,相对于MD5和SHA1要弱很多;MD5的安全性很高,不过大概在04年的时候被山东大学的王小云破解了;SHA1的安全性最高。
5. 效率不同,CRC的计算效率很高;MD5和SHA1比较慢。
6. 用途不同。CRC一般用作通信数据的校验;MD5和SHA1用于安全(Security)领域,比如文件校验、数字签名等。

结尾

1,依赖包重复问题
a,把需要共享的资源打包到一起
b,分割包,这些包不是在同一时间使用的
c,把共享部分打包成一个单独的包
2,图集重复问题
3,Android贴图问题
4,iOS文件处理重复fixed in Unity 5.3.2p2.

Unity Asset Bundle Browser tool

工具链接:http://pan.baidu.com/s/1slPrVaP 密码:zz80

直接放在Editor目录下即可,在unity编辑器导航栏Windows->AssetBundle Browser




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/82274
推荐阅读
相关标签
  

闽ICP备14008679号