Skip to content

Commit

Permalink
Add an MPKBuilder script to unity package. (#6)
Browse files Browse the repository at this point in the history
Any Unity project can use this to build mpks for Lumin. Scenes listed
and enabled in the editor build settings are added to the mpk, either
one scene per mpk or all scenes in one mpk, depending on the cmd line
args passed in to Unity.
To invoke this script, pass the fully qualified class & func name -
'-executeMethod UnityEditor.XR.MagicLeap.MPKBuilder.BuildMPK'.
  • Loading branch information
Nihav-Jain authored and GitHub Enterprise committed Mar 5, 2021
1 parent 4cc7a67 commit fd94af2
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,4 @@ FodyWeavers.xsd

# Don't ignore Unity .meta files
!Runtime/**/*.meta
!Editor/**/*.meta
19 changes: 19 additions & 0 deletions Editor/LuminUnity-Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "LuminUnity-Editor",
"rootNamespace": "",
"references": [
"GUID:ec0144320e2ab8c42b9ce084651ff2a1",
"GUID:28d8685fae23c21419ef42894cb9b075"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Editor/LuminUnity-Editor.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

188 changes: 188 additions & 0 deletions Editor/MPKBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;

namespace UnityEditor.XR.MagicLeap
{
public class MPKBuilder
{
private static readonly string Arg_Development = "--development";
private static readonly string Arg_Shipping = "--shipping";
private static readonly string Arg_OutDir = "--outdir";
private static readonly string Arg_OneMpkPerScene = "--one-mpk-per-scene";
private static readonly string Arg_CheckProLicense = "--check-pro-license";

private class BuildSettingsCache
{
private string appIdentifier;
private string productName;
private UnityEditor.BuildTargetGroup seletedBuildTargetGroup;
private bool signPackage;
private bool isPackageDebuggable;

public BuildSettingsCache()
{
appIdentifier = PlayerSettings.applicationIdentifier;
productName = PlayerSettings.productName;
seletedBuildTargetGroup = EditorUserBuildSettings.selectedBuildTargetGroup;
signPackage = PlayerSettings.Lumin.signPackage;
isPackageDebuggable = UnityEditor.Lumin.UserBuildSettings.isPackageDebuggable;
}

public void RestoreSettings()
{
PlayerSettings.applicationIdentifier = appIdentifier;
PlayerSettings.productName = productName;
EditorUserBuildSettings.selectedBuildTargetGroup = seletedBuildTargetGroup;
PlayerSettings.Lumin.signPackage = signPackage;
UnityEditor.Lumin.UserBuildSettings.isPackageDebuggable = isPackageDebuggable;
}

~BuildSettingsCache()
{
RestoreSettings();
}
}

public static void BuildMPK()
{
MPKBuilder mpkBuilder = new MPKBuilder();
mpkBuilder.Build();
}

private void Build()
{
BuildSettingsCache buildSettingsCache = new BuildSettingsCache();

try
{
if (IsArgSet(Arg_CheckProLicense) && !UnityEngine.Application.HasProLicense())
{
throw new System.Exception($"{Arg_CheckProLicense} was passed but Unity Pro License not found!");
}

EditorUserBuildSettings.selectedBuildTargetGroup = BuildTargetGroup.Lumin;
PlayerSettings.Lumin.signPackage = true;
UnityEditor.Lumin.UserBuildSettings.isPackageDebuggable = !IsArgSet(Arg_Shipping);

if (IsArgSet(Arg_OneMpkPerScene))
{
BuildSceneByScene();
}
else
{
BuildAllScenes();
}
}
catch (System.Exception)
{
throw;
}
finally
{
buildSettingsCache.RestoreSettings();
}
}

private void BuildAllScenes()
{
List<string> activeScenes = new List<string>();
EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes;
foreach (EditorBuildSettingsScene scene in scenes)
{
if (!scene.enabled)
{
continue;
}

activeScenes.Add(scene.path);
}

BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.target = BuildTarget.Lumin;
buildPlayerOptions.targetGroup = BuildTargetGroup.Lumin;
buildPlayerOptions.options = GetBuildOptions();
buildPlayerOptions.scenes = activeScenes.ToArray();
buildPlayerOptions.locationPathName = System.IO.Path.Combine(GetBuildFolder().FullName, $"{PlayerSettings.applicationIdentifier}.mpk");

UnityEditor.Build.Reporting.BuildReport report = UnityEditor.BuildPipeline.BuildPlayer(buildPlayerOptions);
if (report.summary.result == UnityEditor.Build.Reporting.BuildResult.Failed)
{
throw new System.Exception($"Building {PlayerSettings.applicationIdentifier} failed.");
}
}

private void BuildSceneByScene()
{
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.target = BuildTarget.Lumin;
buildPlayerOptions.targetGroup = BuildTargetGroup.Lumin;
buildPlayerOptions.options = GetBuildOptions();

System.IO.FileInfo buildFolder = GetBuildFolder();

string cachedAppIdentifier = PlayerSettings.applicationIdentifier;

EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes;
foreach (EditorBuildSettingsScene scene in scenes)
{
if (!scene.enabled)
{
continue;
}

string sceneName = System.IO.Path.GetFileNameWithoutExtension(scene.path);
PlayerSettings.applicationIdentifier = $"{cachedAppIdentifier}.{sceneName.ToLower()}";
PlayerSettings.productName = sceneName;
buildPlayerOptions.locationPathName = System.IO.Path.Combine(buildFolder.FullName, $"{PlayerSettings.applicationIdentifier}.mpk");
buildPlayerOptions.scenes = new string[] { scene.path };

UnityEditor.Build.Reporting.BuildReport report = UnityEditor.BuildPipeline.BuildPlayer(buildPlayerOptions);
if (report.summary.result == UnityEditor.Build.Reporting.BuildResult.Failed)
{
throw new System.Exception($"Building {sceneName} failed.");
}
}
}

private UnityEditor.BuildOptions GetBuildOptions()
{
UnityEditor.BuildOptions buildOptions = BuildOptions.None;
if (IsArgSet(Arg_Development))
{
buildOptions |= BuildOptions.Development;
}

return buildOptions;
}

private System.IO.FileInfo GetBuildFolder()
{
string outDir = "Build";
TryGetArgValue(Arg_OutDir, ref outDir);

System.IO.FileInfo buildFolder = new System.IO.FileInfo(System.IO.Path.Combine(outDir, BuildTarget.Lumin.ToString()));
buildFolder.Directory.Create();

return buildFolder;
}

private bool IsArgSet(string arg)
{
return (System.Array.IndexOf(System.Environment.GetCommandLineArgs(), arg) != -1);
}

private bool TryGetArgValue(string arg, ref string value)
{
int argIndex = System.Array.IndexOf(System.Environment.GetCommandLineArgs(), arg);
if (argIndex != -1 && argIndex < System.Environment.GetCommandLineArgs().Length - 1)
{
value = System.Environment.GetCommandLineArgs()[argIndex + 1];
return true;
}

return false;
}
}
}
11 changes: 11 additions & 0 deletions Editor/MPKBuilder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd94af2

Please sign in to comment.