Skip to content

Instantly share code, notes, and snippets.

@marinho
Created February 17, 2022 06:16
Show Gist options
  • Save marinho/1e96ef986a4e372c2e4757b0f33ec950 to your computer and use it in GitHub Desktop.
Save marinho/1e96ef986a4e372c2e4757b0f33ec950 to your computer and use it in GitHub Desktop.
A class and script to build a game in Unity project to multiple platforms at once, either by one click Unity main menu, or via command line call. In fact, this with these utilities, I'm able to build more often and find building issues earlier than if I did it manually.
#!/bin/sh
DIR=`pwd`
BUILDDIR="$DIR/Build"
BIN="/Applications/Unity/Hub/Editor/2020.3.15f2/Unity.app/Contents/MacOS/Unity"
$BIN -quit -batchmode -projectPath .. -executeMethod TwelveBuildForMultiplePlatforms.BuildStory
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
public class UnityBuildForMultiplePlatforms : MonoBehaviour
{
static List<SupportedPlatform> platforms = new List<SupportedPlatform> {
new SupportedPlatform(BuildTarget.StandaloneOSX, "MacOS/Game.app"),
new SupportedPlatform(BuildTarget.StandaloneWindows64, "Win64/Game.exe")
// new SupportedPlatform(BuildTarget.StandaloneLinux64, "Linux64/Game")
};
[MenuItem("Tools/My Game/Build/Build Race")]
public static void BuildRace () {
string firstScene = "Assets/Game/Levels/0 Mainland/Abilities Trial/Abilities Trial.unity";
BuildForAllPlatforms(firstScene);
}
[MenuItem("Tools/My Game/Build/Build Story")]
public static void BuildStory () {
string firstScene = "Assets/Game/Levels/0 Mainland/Mainland.unity";
BuildForAllPlatforms(firstScene);
}
static void BuildForAllPlatforms(string firstScene) {
List<string> allScenes = CollectScenes();
string[] scenes = MoveSceneToTop(allScenes, firstScene).ToArray();
foreach (var platform in platforms) {
Debug.Log($"Building {platform.executablePath}");
BuildForPlatform(scenes, platform.target, platform.executablePath);
}
}
static void BuildForPlatform(string[] scenes, BuildTarget target, string platformPath, BuildOptions options = BuildOptions.None) {
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.scenes = scenes;
buildPlayerOptions.locationPathName = $"Build/{platformPath}";
buildPlayerOptions.target = target;
buildPlayerOptions.options = options;
BuildPipeline.BuildPlayer(buildPlayerOptions);
}
static List<string> CollectScenes() {
var projectScenes = new List<EditorBuildSettingsScene>(EditorBuildSettings.scenes);
var scenePaths = projectScenes.Select(s => s.path).ToList();
return scenePaths;
}
static List<string> MoveSceneToTop(List<string> scenes, string firstScenePath) {
var scenesWithoutFirst = scenes.FindAll(i => i != firstScenePath);
scenesWithoutFirst.Insert(0, firstScenePath);
return scenesWithoutFirst;
}
}
class SupportedPlatform {
public BuildTarget target;
public string executablePath;
public SupportedPlatform(BuildTarget _target, string _executablePath) {
target = _target;
executablePath = _executablePath;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment