AssetBundleExporter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. /// <summary>
  6. /// Exports the levels under ForAssetBundles asset folder.
  7. /// The build target used is dependent on the current project's platform.
  8. /// This is to avoid switching of platform causing pain for reimporting ALL assets (especially if using PVRTC on iOS)
  9. /// </summary>
  10. public class AssetBundleExporter
  11. {
  12. public static void Export()
  13. {
  14. CreateAssetBundleFolders();
  15. CleanStreamingAssetsFolder();
  16. string forAssetBundlesPath = Application.dataPath + "/ForAssetBundles";
  17. //Find all levels to export
  18. System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(forAssetBundlesPath);
  19. foreach (System.IO.DirectoryInfo g in dir.GetDirectories())
  20. {
  21. BuildTarget buildTarget;
  22. #if UNITY_IPHONE
  23. buildTarget = BuildTarget.iOS;
  24. #elif UNITY_ANDROID
  25. buildTarget = BuildTarget.Android;
  26. #elif UNITY_WEBPLAYER
  27. buildTarget = BuildTarget.WebPlayer;
  28. #endif
  29. Export(g.Name, g.FullName, buildTarget);
  30. }
  31. CopyDefaultAssetBundleFor(AssetBundleUtils.GetBuildTargetPrefix());
  32. }
  33. static void Export(string bundleNamePrefix, string srcPath, BuildTarget buildTarget)
  34. {
  35. System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(srcPath);
  36. //find all sub-parts to export
  37. foreach (System.IO.DirectoryInfo subPart in dir.GetDirectories())
  38. {
  39. string bundleName = bundleNamePrefix + "_" + subPart.Name;
  40. AVDebug.Log("Building bundle " + bundleName);
  41. List<Object> assets = new List<Object>();
  42. foreach (System.IO.FileInfo file in subPart.GetFiles())
  43. {
  44. //skip meta files
  45. if (file.Name.EndsWith("meta"))
  46. {
  47. continue;
  48. }
  49. string relativeSrcPath = "Assets"+file.FullName.Substring(Application.dataPath.Length);
  50. AVDebug.Log("Adding "+relativeSrcPath);
  51. Object[] asset = AssetDatabase.LoadAllAssetsAtPath(relativeSrcPath);
  52. assets.AddRange(asset);
  53. }
  54. Debug.Log("assets "+assets.Count);
  55. string destPath = GetDestPathFor(buildTarget) + "/"+bundleName+".unity3d";
  56. BuildPipeline.BuildAssetBundle(assets[0], assets.ToArray(), destPath, BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies, buildTarget);
  57. }
  58. }
  59. static string GetDestPathFor(BuildTarget buildTarget)
  60. {
  61. string result = AssetBundleUtils.GetLocalAssetBundlesDestPath() + buildTarget.ToString();
  62. AVDebug.Log("Destination is " + result);
  63. return result;
  64. }
  65. static void CreateAssetBundleFolders()
  66. {
  67. string assetBundlesPath = AssetBundleUtils.GetLocalAssetBundlesDestPath();
  68. System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(assetBundlesPath);
  69. if (dir.Exists)
  70. {
  71. return;
  72. }
  73. dir.Create();
  74. dir = new System.IO.DirectoryInfo(assetBundlesPath + BuildTarget.iOS.ToString());
  75. dir.Create();
  76. dir = new System.IO.DirectoryInfo(assetBundlesPath + BuildTarget.Android.ToString());
  77. dir.Create();
  78. }
  79. static void CleanStreamingAssetsFolder()
  80. {
  81. string path = Directory.GetCurrentDirectory()+"/Assets/StreamingAssets";
  82. System.IO.DirectoryInfo streamingAssetsFolder = new DirectoryInfo(path);
  83. foreach (FileInfo file in streamingAssetsFolder.GetFiles())
  84. {
  85. file.Delete();
  86. }
  87. foreach (DirectoryInfo dir in streamingAssetsFolder.GetDirectories())
  88. {
  89. dir.Delete(true);
  90. }
  91. }
  92. static void CopyDefaultAssetBundleFor(string platform)
  93. {
  94. string streamingAssetsFolderPath = Directory.GetCurrentDirectory()+"/Assets/StreamingAssets/"+platform;
  95. Directory.CreateDirectory(streamingAssetsFolderPath);
  96. string assetBundlesPath = Directory.GetCurrentDirectory()+"/AssetBundles/"+platform;
  97. System.IO.DirectoryInfo assetBundlesFolder = new DirectoryInfo(assetBundlesPath);
  98. foreach (FileInfo file in assetBundlesFolder.GetFiles())
  99. {
  100. if (file.Name.StartsWith("default_"))
  101. {
  102. file.CopyTo(streamingAssetsFolderPath+"/"+file.Name);
  103. }
  104. }
  105. }
  106. }