Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade shader exporter to new assetbundle pipeline #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 82 additions & 25 deletions Assets/Editor/ExportAssetBundle.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,84 @@
using UnityEditor;
using UnityEditor;
using UnityEngine;
using System.Linq;
using System.IO;
using System.Collections;

public class CreateAssetBundles
{
[MenuItem ("Assets/Build Shader Bundle")]
static void BuildAllAssetBundles ()
{
// Bring up save panel
string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");

var opts = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.CollectDependencies;
BuildTarget[] platforms = { BuildTarget.StandaloneWindows, BuildTarget.StandaloneOSXUniversal, BuildTarget.StandaloneLinux };
string[] platformExts = { "-windows", "-macosx", "-linux" };

for (var i = 0; i < platforms.Length; ++i)
{
// Build the resource file from the active selection.
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path.Replace(".unity3d", platformExts[i] + ".unity3d"), opts, platforms[i]);
Selection.objects = selection;
}
}
}

public class ExportSelected {

private const string ext = "unity3d";

static readonly string[] destinations = { "Windows", "Linux", "MacOSX" };

[MenuItem("Export/Export Selected Assets As Bundle")]
private static void BuildBundle()
{
string destination = EditorUtility.SaveFilePanel("Export Bundle", string.Empty, "New Asset Bundle", ext);
string fileName;
Split(ref destination, out fileName);

// As of this version of unity, there is no need for 32 bit exporting.
BuildTarget[] targets = { BuildTarget.StandaloneWindows64, BuildTarget.StandaloneLinux64, BuildTarget.StandaloneOSXIntel64 };
string[] extensions = { fileName + "-windows", fileName + "-linux", fileName + "-macosx" };

// Get selected assets
var assets = Selection.GetFiltered<Object>(SelectionMode.DeepAssets);

// Build the plan.
AssetBundleBuild[] builds = { BuildPlan(assets) };

// Build all bundles
for (int i = 0; i < 3; i++)
{
// Assign the proper bundle name
builds[0].assetBundleName = extensions[i];

// Create finalized filepath and create folders if necessary (because the operation fails otherwise)
string dest = Path.Combine(destination, destinations[i]);
if (!Directory.Exists(dest))
{
Directory.CreateDirectory(dest);
}

// Build bundle for this platform.
BuildPipeline.BuildAssetBundles(dest, builds, BuildAssetBundleOptions.None, targets[i]);
}

AssetDatabase.Refresh();
}

static readonly char[] separator = { '/' };
/// <summary>
/// Preprocesses a full file path for later use by the plan builder.
/// </summary>
/// <param name="path"></param>
/// <param name="filename"></param>
static void Split(ref string path, out string filename)
{
string[] parts = path.Split(separator);
filename = parts[parts.Length - 1];
path = path.Replace(filename, string.Empty);
filename = filename.Replace(".unity3d", string.Empty);
}

/// <summary>
/// Generates an AssetBundleBuild plan from the given array of assets.
/// </summary>
/// <param name="assets">The assets to be included in this bundle</param>
/// <returns></returns>
public static AssetBundleBuild BuildPlan(Object[] assets)
{
AssetBundleBuild build = new AssetBundleBuild()
{
addressableNames = new string[assets.Length],
assetNames = new string[assets.Length],
assetBundleVariant = ext
};

for (int i = 0; i < assets.Length; i++)
{
build.addressableNames[i] = assets[i].name;
build.assetNames[i] = AssetDatabase.GetAssetPath(assets[i]);
}

return build;
}
}