Skip to content

Instantly share code, notes, and snippets.

@wmiller
Created February 3, 2014 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wmiller/8792555 to your computer and use it in GitHub Desktop.
Save wmiller/8792555 to your computer and use it in GitHub Desktop.
XML Database BuildAssetBundles
[MenuItem("Assets/Build AssetBundles")]
private static void BuildAssetBundles()
{
ClearAssetBundles();
// You would probably want to parameterize this part
Database.Instance.ReadFiles(AssetInfoXMLPath);
// Validate asset infos
ValidateAssetBundleAssetsInternal();
// Get all asset infos and asset bundle infos from the database
AssetInfo[] assetInfos = Database.Instance.GetEntries<AssetInfo>();
AssetBundleInfo[] assetBundleInfos = Database.Instance.GetEntries<AssetBundleInfo>();
// We need to build a list of all objects and object names (paths) for each
// asset bundle. Store these lists in two dictionaries, keyed to the
// asset bundle info ID that each asset info references.
Dictionary<ID, List<Object>> bundleObjects = new Dictionary<ID, List<Object>>();
Dictionary<ID, List<string>> bundleNames = new Dictionary<ID, List<string>>();
foreach (AssetInfo info in assetInfos)
{
List<Object> objectList;
if (!bundleObjects.TryGetValue(info.AssetBundleInfoRef.Entry.DatabaseID, out objectList))
{
objectList = new List<Object>();
bundleObjects.Add(info.AssetBundleInfoRef.Entry.DatabaseID, objectList);
}
List<string> nameList;
if (!bundleNames.TryGetValue(info.AssetBundleInfoRef.Entry.DatabaseID, out nameList))
{
nameList = new List<string>();
bundleNames.Add(info.AssetBundleInfoRef.Entry.DatabaseID, nameList);
}
UnityEngine.Object asset = Resources.LoadAssetAtPath<UnityEngine.Object>(info.Path);
if (asset == null)
{
throw new System.Exception("Invalid asset: " + info.Path);
}
objectList.Add(asset);
nameList.Add(info.Path);
}
// Build bundles
foreach (AssetBundleInfo info in assetBundleInfos)
{
// Get the list of objects for this bundle
List<Object> objectList;
if (!bundleObjects.TryGetValue(info.DatabaseID, out objectList))
{
Debug.LogWarning("No objects for bundle: " + info.Name);
continue;
}
// Get the list of names for this bundle
List<string> nameList;
if (!bundleNames.TryGetValue(info.DatabaseID, out nameList))
{
throw new System.Exception("No names generated for objects");
}
// Build the bundle using the two lists
string path = AssetBundlePath + "/" + info.Name + ".unity3d";
BuildAssetBundleOptions options = (BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets |
BuildAssetBundleOptions.DeterministicAssetBundle);
BuildTarget target = BuildTarget.WebPlayer;
if (!BuildPipeline.BuildAssetBundleExplicitAssetNames(objectList.ToArray(),
nameList.ToArray(),
path,
options,
target))
{
throw new System.Exception("Unable to build asset bundle: " + info.Name);
}
AssetDatabase.SaveAssets();
Debug.Log("Built bundle: " + info.Name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment