Skip to content

Instantly share code, notes, and snippets.

@GEMISIS
Last active May 24, 2020 05:20
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 GEMISIS/ed4e115a71f62b838f6863a391add7c3 to your computer and use it in GitHub Desktop.
Save GEMISIS/ed4e115a71f62b838f6863a391add7c3 to your computer and use it in GitHub Desktop.
A simple script for creating and deploying Asset Bundles in Unity to Oculus builds. Currently just has 2 Asset Bundles: scenes and assets. You can update the menu items for Deploying to support alternatives as necessary.
using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
using UnityEditor;
using UnityEngine;
namespace RGBSchemes
{
public class BuildAssetBundles : MonoBehaviour
{
private static readonly string ANDROID_PATH = $"{Application.dataPath}/../AssetBundles/Android";
private static readonly string WINDOWS_PATH = $"{Application.dataPath}/../AssetBundles/Windows";
#region Build menu items.
[MenuItem("Asset Bundles/Build/All")]
public static void BuildBundles()
{
BuildAndroidBundles();
BuildWindowsBundles();
}
[MenuItem("Asset Bundles/Build/Android")]
public static void BuildAndroidBundles()
{
if (!Directory.Exists(ANDROID_PATH))
{
UnityEngine.Debug.Log($"{ANDROID_PATH} does not exist! Creating...");
Directory.CreateDirectory(ANDROID_PATH);
}
UnityEngine.Debug.Log("Building Asset Bundle for Android...");
BuildPipeline.BuildAssetBundles(ANDROID_PATH, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.Android);
UnityEngine.Debug.Log("Built!");
}
[MenuItem("Asset Bundles/Build/Windows")]
public static void BuildWindowsBundles()
{
if (!Directory.Exists(WINDOWS_PATH))
{
UnityEngine.Debug.Log($"{WINDOWS_PATH} does not exist! Creating...");
Directory.CreateDirectory(WINDOWS_PATH);
}
UnityEngine.Debug.Log("Building Asset Bundle for Windows...");
BuildPipeline.BuildAssetBundles(WINDOWS_PATH, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
UnityEngine.Debug.Log("Built!");
}
#endregion
#region Deploy menu items.
[MenuItem("Asset Bundles/Deploy/Android/Assets")]
public static void DeployAndroidBundles_Assets()
{
DeployAndroidBundles("assets");
}
[MenuItem("Asset Bundles/Deploy/Android/Scenes")]
public static void DeployAndroidBundles_Scenes()
{
DeployAndroidBundles("scenes");
}
[MenuItem("Asset Bundles/Deploy/Windows/Assets")]
public static void DeployWindowsBundles_Assets()
{
DeployWindowsBundles("assets");
}
[MenuItem("Asset Bundles/Deploy/Windows/Scenes")]
public static void DeployWindowsBundles_Scenes()
{
DeployWindowsBundles("scenes");
}
#endregion
#region Helper methods.
private static void DeployAndroidBundles(string bundleName)
{
string path = Path.Combine(ANDROID_PATH, bundleName);
if (!File.Exists(path))
{
UnityEngine.Debug.Log("No existing AssetBundles found for Android. Building first...");
BuildAndroidBundles();
}
if (File.Exists(path))
{
UnityEngine.Debug.Log("Deploying to Android device...");
AdbCommand($"push {path} /sdcard/Android/obb/{Application.identifier}/");
}
else
{
UnityEngine.Debug.LogWarning("Could not deploy Asset Bundles! There was an error building them for Android!");
}
}
private static void DeployWindowsBundles(string bundleName)
{
string path = Path.Combine(WINDOWS_PATH, bundleName);
if (!File.Exists(path))
{
UnityEngine.Debug.Log("No existing AssetBundles found for Windows. Building first...");
BuildWindowsBundles();
}
if (File.Exists(path))
{
string OculusPath = Environment.GetEnvironmentVariable("OculusBase");
if (OculusPath.Length > 0)
{
UnityEngine.Debug.Log($"Found Oculus installation at {OculusPath}");
string newPath = Path.Combine(OculusPath, "Software/Software/", $"{Application.companyName}-{Application.productName}/", "OC_ASSET_FILES");
if (Directory.Exists(newPath))
{
UnityEngine.Debug.Log("Deploying to Windows installation...");
File.Copy(path, Path.Combine(newPath, bundleName), true);
UnityEngine.Debug.Log("Succesfully Deployed!");
}
else
{
UnityEngine.Debug.Log($"Could not find an installation for {Application.productName} from {Application.companyName}! Please make sure you have the latest build installed through the Oculus app!");
}
}
else
{
UnityEngine.Debug.Log($"The Oculus app is not installed! Please install it and install {Application.productName} from {Application.companyName} before running this!");
}
}
else
{
UnityEngine.Debug.LogWarning("Could not deploy Asset Bundles! There was an error building them for Windows!");
}
}
private static void AdbCommand(string input)
{
Thread thread = new Thread(delegate ()
{
ProcessStartInfo processInfo = new ProcessStartInfo("adb", input)
{
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true
};
Process process = new Process
{
StartInfo = processInfo
};
process.Start();
string errors = process.StandardError.ReadToEnd();
string output = process.StandardOutput.ReadToEnd();
if (errors.Length > 0)
{
UnityEngine.Debug.Log($"Error occured: {errors}");
}
else
{
UnityEngine.Debug.Log($"Results: {output}");
}
process.WaitForExit();
process.Close();
});
thread.Start();
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment