Created
May 18, 2015 15:01
-
-
Save garrynewman/50ac0f22fb4588cf739b to your computer and use it in GitHub Desktop.
Asset bundle building
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void Build( string strPath, BuildTarget target ) | |
{ | |
if ( !System.IO.Directory.Exists( strPath ) ) | |
System.IO.Directory.CreateDirectory( strPath ); | |
AssetBundleBuild[] existingBundles = AssetDatabase.GetAllAssetBundleNames().Select( x => new AssetBundleBuild() { assetBundleName = x, assetBundleVariant = "", assetNames = AssetDatabase.GetAssetPathsFromAssetBundle( x ) } ).ToArray(); | |
EditorUtility.DisplayProgressBar( "SplitContent", "", 0 ); | |
var newBundles = SplitContent( existingBundles ); | |
BuildPipeline.BuildAssetBundles( strPath, newBundles, BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.DeterministicAssetBundle, target ); | |
EditorUtility.ClearProgressBar(); | |
} | |
static AssetBundleBuild[] SplitContent( AssetBundleBuild[] existingBundles ) | |
{ | |
EditorUtility.DisplayProgressBar( "SplitContent", "Get Explicitly Included Content", 0 ); | |
var explicitlyIncluded = existingBundles.SelectMany( x => x.assetNames ).ToArray(); | |
EditorUtility.DisplayProgressBar( "SplitContent", "Find All Included Files", 0 ); | |
var allIncludes = explicitlyIncluded.SelectMany( x => | |
{ | |
if ( System.IO.Directory.Exists( x ) ) | |
{ | |
return AssetDatabase.FindAssets( "", new[] { x } ).Select( y => AssetDatabase.GUIDToAssetPath( y ) ); | |
} | |
else | |
{ | |
return new[] { x }; | |
} | |
} ).Distinct().ToArray(); | |
EditorUtility.DisplayProgressBar( "SplitContent", "GetDependencies", 0 ); | |
var allDependancies = AssetDatabase.GetDependencies( allIncludes ) // Get all dependencies | |
.Where( x => !allIncludes.Contains( x ) ) // that aren't included explicitly | |
.Distinct(); | |
var extraIncludes = new List<AssetBundleBuild>(); | |
EditorUtility.DisplayProgressBar( "SplitContent", "Textures", 0 ); | |
{ | |
var allContent = allDependancies.Where( x => { return x.EndsWith( ".tga", System.StringComparison.CurrentCultureIgnoreCase ) || | |
x.EndsWith( ".tif", System.StringComparison.CurrentCultureIgnoreCase ) || | |
x.EndsWith( ".png", System.StringComparison.CurrentCultureIgnoreCase ) || | |
x.EndsWith( ".jpg", System.StringComparison.CurrentCultureIgnoreCase ) || | |
x.EndsWith( ".psd", System.StringComparison.CurrentCultureIgnoreCase ) | |
; } ); | |
foreach ( var group in allContent.GroupBy( x => Mathf.Abs( x.GetHashCode() % 128 ) ) ) | |
{ | |
extraIncludes.Add( new AssetBundleBuild() { assetNames = group.ToArray(), assetBundleVariant = "", assetBundleName = "content/textures/" + group.Key.ToString( "000" ) + ".content" } ); | |
} | |
} | |
EditorUtility.DisplayProgressBar( "SplitContent", "Audio", 0 ); | |
{ | |
var allContent = allDependancies.Where( x => { return x.EndsWith( ".wav", System.StringComparison.CurrentCultureIgnoreCase ) || | |
x.EndsWith( ".mp3", System.StringComparison.CurrentCultureIgnoreCase ) || | |
x.EndsWith( ".ogg", System.StringComparison.CurrentCultureIgnoreCase ) | |
; } ); | |
foreach ( var group in allContent.GroupBy( x => Mathf.Abs( x.GetHashCode() % 128 ) ) ) | |
{ | |
extraIncludes.Add( new AssetBundleBuild() { assetNames = group.ToArray(), assetBundleVariant = "", assetBundleName = "content/audio/" + group.Key.ToString( "000" ) + ".content" } ); | |
} | |
} | |
extraIncludes.AddRange( existingBundles ); | |
return extraIncludes.ToArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment