AssetBundleExporter.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. dir = new System.IO.DirectoryInfo(assetBundlesPath + BuildTarget.WebPlayer.ToString());
  79. dir.Create();
  80. }
  81. static void CleanStreamingAssetsFolder()
  82. {
  83. string path = Directory.GetCurrentDirectory()+"/Assets/StreamingAssets";
  84. System.IO.DirectoryInfo streamingAssetsFolder = new DirectoryInfo(path);
  85. foreach (FileInfo file in streamingAssetsFolder.GetFiles())
  86. {
  87. file.Delete();
  88. }
  89. foreach (DirectoryInfo dir in streamingAssetsFolder.GetDirectories())
  90. {
  91. dir.Delete(true);
  92. }
  93. }
  94. static void CopyDefaultAssetBundleFor(string platform)
  95. {
  96. string streamingAssetsFolderPath = Directory.GetCurrentDirectory()+"/Assets/StreamingAssets/"+platform;
  97. Directory.CreateDirectory(streamingAssetsFolderPath);
  98. string assetBundlesPath = Directory.GetCurrentDirectory()+"/AssetBundles/"+platform;
  99. System.IO.DirectoryInfo assetBundlesFolder = new DirectoryInfo(assetBundlesPath);
  100. foreach (FileInfo file in assetBundlesFolder.GetFiles())
  101. {
  102. if (file.Name.StartsWith("default_"))
  103. {
  104. file.CopyTo(streamingAssetsFolderPath+"/"+file.Name);
  105. }
  106. }
  107. }
  108. }