Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Build mutliple Unity applications for multiple platforms with one click
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class BuildAutomation {
public struct BuildTargetAndGroup {
public BuildTargetGroup group;
public BuildTarget target;
public BuildTargetAndGroup (BuildTargetGroup group, BuildTarget target) {
this.group = group;
this.target = target;
}
}
[MenuItem("My Project/Build/Build all")]
public static void BuildAll () {
string path = EditorUtility.SaveFolderPanel("Choose Location of Built Applications", "Builds", "");
BuildApplication(path);
BuildOtherApplication(path);
}
public static void BuildApplication (string path) {
string appName = "application";
Texture2D icon = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/application.png", typeof(Texture2D));
PlayerSettings.productName = "Application";
PlayerSettings.applicationIdentifier = "com.myproject.application";
PlayerSettings.defaultScreenWidth = 1024;
PlayerSettings.defaultScreenHeight = 768;
PlayerSettings.defaultIsFullScreen = true;
PlayerSettings.displayResolutionDialog = ResolutionDialogSetting.HiddenByDefault;
//...
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.scenes = new[] { "Assets/application.unity" };
buildPlayerOptions.locationPathName = path;
buildPlayerOptions.options = BuildOptions.None;
BuildForTargetes(buildPlayerOptions, appName, icon,
new BuildTargetAndGroup(BuildTargetGroup.Standalone, BuildTarget.StandaloneOSX),
new BuildTargetAndGroup(BuildTargetGroup.Standalone, BuildTarget.StandaloneWindows64),
new BuildTargetAndGroup(BuildTargetGroup.Standalone, BuildTarget.StandaloneLinux64) );
}
public static void BuildOtherApplication (string path) {
string appName = "otherApplication";
Texture2D icon = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/otherApplication.png", typeof(Texture2D));
PlayerSettings.productName = "Other Application";
PlayerSettings.applicationIdentifier = "com.myproject.otherapplication";
PlayerSettings.macFullscreenMode = MacFullscreenMode.FullscreenWindowWithDockAndMenuBar;
PlayerSettings.defaultIsNativeResolution = false;
PlayerSettings.defaultIsFullScreen = false;
PlayerSettings.defaultScreenWidth = 400;
PlayerSettings.defaultScreenHeight = 600;
PlayerSettings.displayResolutionDialog = ResolutionDialogSetting.HiddenByDefault;
//...
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.scenes = new[] { "Assets/otherApplication.unity" };
buildPlayerOptions.locationPathName = path;
buildPlayerOptions.options = BuildOptions.None;
BuildForTargetes(buildPlayerOptions, appName, icon,
new BuildTargetAndGroup(BuildTargetGroup.Standalone, BuildTarget.StandaloneOSX),
new BuildTargetAndGroup(BuildTargetGroup.Standalone, BuildTarget.StandaloneWindows64),
new BuildTargetAndGroup(BuildTargetGroup.Standalone, BuildTarget.StandaloneLinux64));
}
public static void BuildForTargetes(BuildPlayerOptions options, string appName, Texture2D icon, params BuildTargetAndGroup[] targets) {
string locationPathName = options.locationPathName;
foreach(BuildTargetAndGroup target in targets) {
// https://forum.unity.com/threads/cant-change-resolution-for-standalone-build.323931/
PlayerSettings.SetApplicationIdentifier(target.group, PlayerSettings.applicationIdentifier);
DeletePreference();
options.targetGroup = target.group;
options.target = target.target;
options.locationPathName = locationPathName + "/" + target.target.ToString() + "/" + appName;
SetIconForTargetGroup(options.targetGroup, icon);
//Debug.Log("building " + options.locationPathName);
BuildPipeline.BuildPlayer(options);
}
}
public static void SetIconForTargetGroup(BuildTargetGroup platform, Texture2D icon) {
int[] iconSizes = PlayerSettings.GetIconSizesForTargetGroup(platform);
Texture2D[] icons = new Texture2D[iconSizes.Length];
for(int i=0; i<icons.Length; i++) {
if(icon.width == iconSizes[i]) {
icons[i] = icon;
}
else {
Texture2D scaledIcon = new Texture2D(icon.width, icon.height, icon.format, false);
Graphics.CopyTexture(icon, scaledIcon);
scaledIcon.Resize(iconSizes[i], iconSizes[i], icon.format, false);
scaledIcon.Apply();
icons[i] = scaledIcon;
}
}
PlayerSettings.SetIconsForTargetGroup(platform, icons);
}
public static void DeletePreference() {
if(Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXEditor){
FileUtil.DeleteFileOrDirectory("~/Library/Preferences/"+PlayerSettings.applicationIdentifier+".plist");
}
else{
Debug.LogWarning("Not sure how to delete Prefferences for this platform!");
// https://forum.unity.com/threads/cant-change-resolution-for-standalone-build.323931/
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment