123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using UnityEngine;
- using UnityEditor;
- using System.Collections.Generic;
- using System.IO;
- /// <summary>
- /// Exports the levels under ForAssetBundles asset folder.
- /// The build target used is dependent on the current project's platform.
- /// This is to avoid switching of platform causing pain for reimporting ALL assets (especially if using PVRTC on iOS)
- /// </summary>
- public class AssetBundleExporter
- {
- public static void Export()
- {
- CreateAssetBundleFolders();
- CleanStreamingAssetsFolder();
- string forAssetBundlesPath = Application.dataPath + "/ForAssetBundles";
- //Find all levels to export
- System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(forAssetBundlesPath);
- foreach (System.IO.DirectoryInfo g in dir.GetDirectories())
- {
- BuildTarget buildTarget;
- #if UNITY_IPHONE
- buildTarget = BuildTarget.iOS;
- #elif UNITY_ANDROID
- buildTarget = BuildTarget.Android;
- #elif UNITY_WEBPLAYER
- buildTarget = BuildTarget.WebPlayer;
- #endif
- Export(g.Name, g.FullName, buildTarget);
- }
- CopyDefaultAssetBundleFor(AssetBundleUtils.GetBuildTargetPrefix());
- }
- static void Export(string bundleNamePrefix, string srcPath, BuildTarget buildTarget)
- {
- System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(srcPath);
- //find all sub-parts to export
- foreach (System.IO.DirectoryInfo subPart in dir.GetDirectories())
- {
- string bundleName = bundleNamePrefix + "_" + subPart.Name;
- AVDebug.Log("Building bundle " + bundleName);
- List<Object> assets = new List<Object>();
- foreach (System.IO.FileInfo file in subPart.GetFiles())
- {
- //skip meta files
- if (file.Name.EndsWith("meta"))
- {
- continue;
- }
- string relativeSrcPath = "Assets"+file.FullName.Substring(Application.dataPath.Length);
- AVDebug.Log("Adding "+relativeSrcPath);
- Object[] asset = AssetDatabase.LoadAllAssetsAtPath(relativeSrcPath);
- assets.AddRange(asset);
- }
- Debug.Log("assets "+assets.Count);
- string destPath = GetDestPathFor(buildTarget) + "/"+bundleName+".unity3d";
- BuildPipeline.BuildAssetBundle(assets[0], assets.ToArray(), destPath, BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies, buildTarget);
- }
- }
- static string GetDestPathFor(BuildTarget buildTarget)
- {
- string result = AssetBundleUtils.GetLocalAssetBundlesDestPath() + buildTarget.ToString();
- AVDebug.Log("Destination is " + result);
- return result;
- }
- static void CreateAssetBundleFolders()
- {
- string assetBundlesPath = AssetBundleUtils.GetLocalAssetBundlesDestPath();
- System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(assetBundlesPath);
- if (dir.Exists)
- {
- return;
- }
- dir.Create();
- dir = new System.IO.DirectoryInfo(assetBundlesPath + BuildTarget.iOS.ToString());
- dir.Create();
- dir = new System.IO.DirectoryInfo(assetBundlesPath + BuildTarget.Android.ToString());
- dir.Create();
- }
- static void CleanStreamingAssetsFolder()
- {
- string path = Directory.GetCurrentDirectory()+"/Assets/StreamingAssets";
- System.IO.DirectoryInfo streamingAssetsFolder = new DirectoryInfo(path);
- foreach (FileInfo file in streamingAssetsFolder.GetFiles())
- {
- file.Delete();
- }
- foreach (DirectoryInfo dir in streamingAssetsFolder.GetDirectories())
- {
- dir.Delete(true);
- }
- }
- static void CopyDefaultAssetBundleFor(string platform)
- {
- string streamingAssetsFolderPath = Directory.GetCurrentDirectory()+"/Assets/StreamingAssets/"+platform;
- Directory.CreateDirectory(streamingAssetsFolderPath);
- string assetBundlesPath = Directory.GetCurrentDirectory()+"/AssetBundles/"+platform;
- System.IO.DirectoryInfo assetBundlesFolder = new DirectoryInfo(assetBundlesPath);
- foreach (FileInfo file in assetBundlesFolder.GetFiles())
- {
- if (file.Name.StartsWith("default_"))
- {
- file.CopyTo(streamingAssetsFolderPath+"/"+file.Name);
- }
- }
- }
- }
|