Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lparkermg
Last active July 31, 2022 05:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lparkermg/453cf24d1055316fa48016882c1f218a to your computer and use it in GitHub Desktop.
Save lparkermg/453cf24d1055316fa48016882c1f218a to your computer and use it in GitHub Desktop.
C# and Batch File for the unity buildscript tutorial over at https://mrlparker.com
@echo off
SET UNITYVERSION=2018.1.1f1
IF NOT [%1]==[] (set UNITYVERSION=%1)
SET PRODUCTNAME="Product Name"
IF NOT [%2]==[] (set PRODUCTNAME=%2)
SET COMPANYNAME="Company Name"
IF NOT [%3]==[] (set COMPANYNAME=%3)
SET TARGET=Windows
IF NOT [%4]==[] (set TARGET=%4)
SET VERSION=0.0.0.0
IF NOT [%5]==[] (set VERSION=%5)
SET BUILDLOCATION="./Build/%TARGET%/%VERSION%"
rmdir -S %BUILDLOCATION%
mkdir %BUILDLOCATION%
>buildManifest.txt (
echo ProductName=%PRODUCTNAME%
echo CompanyName=%COMPANYNAME%
echo Version=%VERSION%
echo BuildLocation=%BUILDLOCATION%
)
"E:\Programs\Unity\%UNITYVERSION%\Editor\Unity.exe" -quit -batchMode -executeMethod BuildHelper.%TARGET%
del /f buildManifest.txt
using System.Linq;
using System.IO;
using System.Collections.Generic;
using UnityEditor;
public static class BuildHelper
{
private static string _buildLocation;
public static void Windows()
{
SetupVariables();
BuildPipeline.BuildPlayer(GetScenes(),_buildLocation + ".exe",BuildTarget.StandaloneWindows64,BuildOptions.None);
}
public static void Linux()
{
SetupVariables();
BuildPipeline.BuildPlayer(GetScenes(),_buildLocation, BuildTarget.StandaloneLinuxUniversal, BuildOptions.None);
}
public static void MacOS()
{
SetupVariables();
BuildPipeline.BuildPlayer(GetScenes(), _buildLocation + ".app", BuildTarget.StandaloneOSX, BuildOptions.None);
}
private static string[] GetScenes()
{
return EditorBuildSettings.scenes.Where(s => s.enabled).Select(s => s.path).ToArray();
}
private static void SetupVariables()
{
if (!File.Exists("./buildManifest.txt"))
{
PlayerSettings.productName = "Product Name Here";
PlayerSettings.companyName = "Luke Parker";
PlayerSettings.forceSingleInstance = true;
PlayerSettings.bundleVersion = "0.0.0.0";
_buildLocation = "./Build/";
}
else
{
using (var fs = new FileStream("./buildManifest.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var sr = new StreamReader(fs))
{
var fileData = new Dictionary<string, string>();
while (!sr.EndOfStream)
{
var line = sr.ReadLine().Split('=');
fileData.Add(line[0], line[1].Replace("\"", ""));
}
PlayerSettings.productName = fileData["ProductName"];
PlayerSettings.companyName = fileData["CompanyName"];
PlayerSettings.forceSingleInstance = true;
PlayerSettings.bundleVersion = fileData["Version"];
_buildLocation = fileData["BuildLocation"] + "/" + fileData["Version"] + "/" + fileData["ProductName"].Replace(" ", "_");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment