AssetsBundleHelper.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections.Generic;
  2. using System.IO;
  3. namespace ET
  4. {
  5. public static class AssetsBundleHelper
  6. {
  7. public static Dictionary<string, UnityEngine.Object> LoadBundle(string assetBundleName)
  8. {
  9. assetBundleName = assetBundleName.ToLower();
  10. Dictionary<string, UnityEngine.Object> objects = new Dictionary<string, UnityEngine.Object>();
  11. if (!Define.IsAsync)
  12. {
  13. if (Define.IsEditor)
  14. {
  15. string[] realPath = null;
  16. realPath = Define.GetAssetPathsFromAssetBundle(assetBundleName);
  17. foreach (string s in realPath)
  18. {
  19. //string assetName = Path.GetFileNameWithoutExtension(s);
  20. UnityEngine.Object resource = Define.LoadAssetAtPath(s);
  21. objects.Add(resource.name, resource);
  22. }
  23. }
  24. return objects;
  25. }
  26. string p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName);
  27. UnityEngine.AssetBundle assetBundle = null;
  28. if (File.Exists(p))
  29. {
  30. assetBundle = UnityEngine.AssetBundle.LoadFromFile(p);
  31. }
  32. else
  33. {
  34. p = Path.Combine(PathHelper.AppResPath, assetBundleName);
  35. assetBundle = UnityEngine.AssetBundle.LoadFromFile(p);
  36. }
  37. if (assetBundle == null)
  38. {
  39. // 获取资源的时候会抛异常,这个地方不直接抛异常,因为有些地方需要Load之后判断是否Load成功
  40. UnityEngine.Debug.LogWarning($"assets bundle not found: {assetBundleName}");
  41. return objects;
  42. }
  43. UnityEngine.Object[] assets = assetBundle.LoadAllAssets();
  44. foreach (UnityEngine.Object asset in assets)
  45. {
  46. objects.Add(asset.name, asset);
  47. }
  48. assetBundle.Unload(false);
  49. return objects;
  50. }
  51. }
  52. }