Skip to content

Instantly share code, notes, and snippets.

@randalfien
Last active February 16, 2021 13:30
Show Gist options
  • Save randalfien/741893ce9d6d4d70a2de8cb7f441565f to your computer and use it in GitHub Desktop.
Save randalfien/741893ce9d6d4d70a2de8cb7f441565f to your computer and use it in GitHub Desktop.
Unity Setup Automatic
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
using Debug = UnityEngine.Debug;
public static class BuildMenu
{
[MenuItem("Build/Win/STEAM")]
private static void BuildSteam()
{
Build(BuildOptions.None, true);
}
private static void Build(BuildOptions options)
{
var path = "D:/MyGame/Steam/Win"; // hardcoded path
DirectoryInfo directory = new DirectoryInfo(path);
directory.Empty();
// path = EditorUtility.SaveFolderPanel("Choose Location", "", ""); // Or let user choose
string[] levels = EditorBuildSettings.scenes.Select(a => a.path).ToArray();
var exePath = path + "/MyGame.exe";
var br = BuildPipeline.BuildPlayer(levels, exePath, BuildTarget.StandaloneWindows, options);
Debug.Log( "Built " + br.summary.result );
// Find ShellHelper here https://github.com/wlgys8/UnityShellHelper
ShellHelper.ShellRequest req = ShellHelper.ProcessCommand("run_build.bat", "D:/MyGame/Steam/sdk/tools/ContentBuilder/");
// run_build.bat looks like this:
// builder\steamcmd.exe +login username passwd +run_app_build_http ..\scripts\app_build_ID.vdf
// pause
// in your app build script, you can set auto-deploy to a branch with the "setlive" parameter
req.onLog += (logType, log) =>
{
Debug.Log( "steam>" + log );
};
req.onDone += () =>
{
Debug.Log( "steam done" );
};
}
[PostProcessBuild(1)]
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject) {
// This adds a textfile to your build with info about the last commit
Debug.Log( pathToBuiltProject );
var infoPath = Path.GetDirectoryName(pathToBuiltProject) + "/info.txt";
// Time
StringBuilder info = new StringBuilder();
info.AppendLine("-- Time --");
info.AppendLine(DateTime.Now.ToString("MM.dd. yyyy HH:mm"));
info.AppendLine("-");
// Last Commit
ShellHelper.ShellRequest req = ShellHelper.ProcessCommand("git log -1", Application.dataPath);
int numLines = 0;
req.onLog += (logType, log) =>
{
if (numLines == 0)
{
info.AppendLine("-- Last Commit --");
}
info.AppendLine(log);
numLines++;
};
// Git User
ShellHelper.ShellRequest req2 = ShellHelper.ProcessCommand("git config user.name", Application.dataPath);
req2.onLog += (logType, log) =>
{
info.AppendLine("-- User --");
info.AppendLine(log);
info.AppendLine("-");
};
int numDone = 0;
req.onDone += () =>
{
numDone++;
if (numDone == 2) File.WriteAllText(infoPath, info.ToString(), Encoding.UTF8);
};
req2.onDone += () =>
{
numDone++;
if (numDone == 2) File.WriteAllText(infoPath, info.ToString(), Encoding.UTF8);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment