Created
June 14, 2017 15:52
-
-
Save sorob/a446bb81d99079c467c30113214e7cb2 to your computer and use it in GitHub Desktop.
Asset Bundle creator
This file contains 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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using UnityEditor; | |
using UnityEngine; | |
public class Bundler : MonoBehaviour | |
{ | |
public static BuildTarget[] DefaultTargetPlatforms { | |
get { | |
return new BuildTarget[] { | |
BuildTarget.Android, | |
BuildTarget.StandaloneOSXUniversal, | |
BuildTarget.StandaloneWindows64, | |
BuildTarget.WebGL | |
}; | |
} | |
} | |
[MenuItem ("Tools/TestBundler")] | |
public static void TestBundler () | |
{ | |
GenerateAssetBundle ("Assets/OpenArchitecture_V1.unity", "main"); | |
} | |
public static void GenerateAssetBundle (string scenePath, string name) | |
{ | |
var asset = AssetImporter.GetAtPath (scenePath); | |
asset.assetBundleName = name; | |
var assetObject = AssetDatabase.LoadAssetAtPath<UnityEngine.Object> (scenePath); | |
for (int i = 0; i < DefaultTargetPlatforms.Length; i++) | |
{ | |
string outputPath = Path.Combine (Application.dataPath, "../" + DefaultTargetPlatforms [i].ToString ()); | |
if (!Directory.Exists (outputPath)) | |
Directory.CreateDirectory (outputPath); | |
//@TODO: use append hash... (Make sure pipeline works correctly with it.) | |
BuildPipeline.BuildAssetBundles (outputPath, BuildAssetBundleOptions.None, DefaultTargetPlatforms [i]); | |
} | |
} | |
public static void ExportObjectAsAssetBundle (string path, string name) | |
{ | |
var asset = AssetImporter.GetAtPath (path); | |
asset.assetBundleName = name; | |
var assetObject = AssetDatabase.LoadAssetAtPath<UnityEngine.Object> (path); | |
var dependencies = AssetDatabase.GetDependencies (name, true); | |
List<UnityEngine.Object> dependencyObjects = new List<UnityEngine.Object> (); | |
foreach (var item in dependencies) | |
{ | |
dependencyObjects.Add (AssetDatabase.LoadAssetAtPath<UnityEngine.Object> (item)); | |
} | |
for (int i = 0; i < DefaultTargetPlatforms.Length; i++) | |
{ | |
string outputPath = Path.Combine (Application.dataPath, DefaultTargetPlatforms [i].ToString ()); | |
if (!Directory.Exists (outputPath)) | |
Directory.CreateDirectory (outputPath); | |
BuildPipeline.BuildAssetBundle (assetObject, dependencyObjects.ToArray (), outputPath, BuildAssetBundleOptions.None, DefaultTargetPlatforms [i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment