Skip to content

Instantly share code, notes, and snippets.

@ekhart
Created February 1, 2021 19:29
Show Gist options
  • Save ekhart/2892b878d745532c82aacfe2cfef254f to your computer and use it in GitHub Desktop.
Save ekhart/2892b878d745532c82aacfe2cfef254f to your computer and use it in GitHub Desktop.
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using UnityEditor;
using Debug = UnityEngine.Debug;
namespace Project.Editor
{
public static class Builder
{
private static string CommandLineBuildPath
{
get
{
var args = Environment.GetCommandLineArgs();
Debug.Log("Args = " + string.Join(", ", args));
// 0 "C:\Program Files\Unity2019.2.12f1\Editor\Unity.exe"
// 1 -batchmode
// 2 -projectPath
// 3 ./
// 4 -executeMethod
// 5 Builder.BuildAndroid
// 6 "Builds/Android/Test.apk"
// 7 -quit
// 8 -logFile
// 9 build.log
return args[6];
}
}
private static void BuildWindows() =>
BuildPlayer(BuildTarget.StandaloneWindows, BuildTargetGroup.Standalone);
private static void BuildWindows64() =>
BuildPlayer(BuildTarget.StandaloneWindows64, BuildTargetGroup.Standalone);
private static void BuildWindowsDevelopment() =>
BuildPlayer(BuildTarget.StandaloneWindows, BuildTargetGroup.Standalone, BuildOptions.Development);
private static void BuildWindows64Development() =>
BuildPlayer(BuildTarget.StandaloneWindows64, BuildTargetGroup.Standalone, BuildOptions.Development);
private static void BuildAndroid() =>
BuildPlayer(BuildTarget.Android);
private static void BuildiOS() =>
BuildPlayer(BuildTarget.iOS);
private static void BuildPlayer(BuildTarget target, BuildTargetGroup group = BuildTargetGroup.Unknown, BuildOptions buildOptions = BuildOptions.None)
{
var scenes = EditorBuildSettings.scenes
.Where(_ => _.enabled)
.Select(_ => _.path)
.ToArray();
Debug.Log($"BuildPlayerTarget locationPathName {CommandLineBuildPath} target {target} targetGroup {group} options {buildOptions} scenes: {string.Join(", ", scenes)}");
var options = new BuildPlayerOptions
{
locationPathName = CommandLineBuildPath,
target = target,
targetGroup = group,
options = buildOptions,
scenes = scenes
};
BuildPipeline.BuildPlayer(options);
}
[MenuItem("Tools/Project/" + nameof(SetVersionSVNRevision))]
private static void SetVersionSVNRevision()
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "SubWCRev",
Arguments = ".",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true
}
};
process.Start();
var lines = new List<string>();
while (!process.StandardOutput.EndOfStream)
{
lines.Add(process.StandardOutput.ReadLine());
}
if (lines.Count > 0)
{
var revision = int.Parse(lines[1].Split(' ')[4]);
var splitted = PlayerSettings.bundleVersion.Split('.');
var major = splitted[0];
var minor = splitted[1];
var patch = splitted[2];
// var (major, minor, patch, _) = PlayerSettings.bundleVersion.Split('.'); // public static void Deconstruct(this IEnumerable<T> enumerable)
PlayerSettings.bundleVersion = $"{major}.{minor}.{patch}.{revision}";
File.WriteAllText("version.log", PlayerSettings.bundleVersion);
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment