|
| 1 | +using System.Collections.Generic; |
| 2 | +using UnityEditor; |
| 3 | +using UnityEditor.Build; |
| 4 | +using UnityEngine; |
| 5 | +using System; |
| 6 | +using System.Collections; |
| 7 | +using UnityEngine.Rendering; |
| 8 | +using UnityEngine.SceneManagement; |
| 9 | + |
| 10 | +// This script is used to automate the build process for different platforms. |
| 11 | +// When included in the project, it can be triggered from the script the game for teh given configuration. |
| 12 | +// Note that it's possible to have those as a button in the editor (see https://github.cds.internal.unity3d.com/unity/Megacity-Metro/blob/c3b1b16ff1f04f96fbfbcc3267696679ad4e8396/Megacity-Metro/Assets/Scripts/Utils/Editor/BuilderScript.cs) |
| 13 | +// Ideally we would like to pass scripting backend and platform as parameters instead of having different script per each combintation but the nature of calling this nmethod via script (via -executeMethod) is that it needs to be static and the parameters are not passed in a way that we can use them. |
| 14 | +// TODO: add iOS support |
| 15 | +public class BuilderScripts : MonoBehaviour |
| 16 | +{ |
| 17 | + [MenuItem("Tools/Builder/Build Development Windows Il2cpp")] |
| 18 | + static void BuildWinIl2cpp() |
| 19 | + { |
| 20 | + // This part modifies Player Settings. We only use it (for our case) to modify scripting backend |
| 21 | + PlayerSettings.SetScriptingBackend(NamedBuildTarget.Standalone, ScriptingImplementation.IL2CPP); |
| 22 | + PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.StandaloneWindows64, false); // disable auto graphic to use our own custom list |
| 23 | + PlayerSettings.SetGraphicsAPIs(BuildTarget.StandaloneWindows64, new []{GraphicsDeviceType.Vulkan, GraphicsDeviceType.Direct3D11, GraphicsDeviceType.Direct3D12}); // We need to specify the graphics API for Android builds to ensure proper shader compilation. Vulkan is recommended for modern devices. |
| 24 | + |
| 25 | + // Below you can see additional settings that you can apply to the build: |
| 26 | + //PlayerSettings.SetGraphicsAPIs(BuildTarget.StandaloneWindows64, new []{GraphicsDeviceType.Direct3D12}); |
| 27 | + //PlayerSettings.SetArchitecture(NamedBuildTarget.Standalone,0); |
| 28 | + |
| 29 | + // The settings that we applied above need to be saved. |
| 30 | + AssetDatabase.SaveAssets(); |
| 31 | + |
| 32 | + // We want to build all scenes in build settings, so we collect them here. |
| 33 | + // If you want to build only specific scenes, then you could just use something like buildPlayerOptions.scenes = new[] { "Assets/Scenes/Menu.unity","Assets/Scenes/Main.unity" }; below. |
| 34 | + List<string> scenesToAdd = new List<string>(); |
| 35 | + foreach (var scene in EditorBuildSettings.scenes) |
| 36 | + { |
| 37 | + if (scene.enabled) |
| 38 | + { |
| 39 | + Debug.Log("Adding scene to build: " + scene.path); |
| 40 | + scenesToAdd.Add(scene.path); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // This is an equivalent of BuildPlayerOptions in the Unity Editor. |
| 45 | + // We want to choose development build, what platform are we targetting, where to save the build and which scenes to include. |
| 46 | + // Some of those options can be omitted when triggering this script from withing GUI since more implicit context is provided (targetGroup, subtarget) |
| 47 | + // subtarget = (int)StandaloneBuildSubtarget.Player |
| 48 | + // targetGroup = BuildTargetGroup.Standalone |
| 49 | + // extraScriptingDefines = new[] { "NETCODE_DEBUG", "UNITY_CLIENT" }, |
| 50 | + var buildPlayerOptions = new BuildPlayerOptions |
| 51 | + { |
| 52 | + locationPathName = "./build/Windows_Il2cpp/PlaytestBuild.exe", |
| 53 | + target = BuildTarget.StandaloneWindows64, |
| 54 | + options = BuildOptions.Development, |
| 55 | + scenes = scenesToAdd.ToArray() |
| 56 | + }; |
| 57 | + |
| 58 | + BuildPipeline.BuildPlayer(buildPlayerOptions); |
| 59 | + } |
| 60 | + |
| 61 | + [MenuItem("Tools/Builder/Build Development Windows Mono")] |
| 62 | + static void BuildWinMono() |
| 63 | + { |
| 64 | + PlayerSettings.SetScriptingBackend(NamedBuildTarget.Standalone, ScriptingImplementation.Mono2x); |
| 65 | + PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.StandaloneWindows64, false); // disable auto graphic to use our own custom list |
| 66 | + PlayerSettings.SetGraphicsAPIs(BuildTarget.StandaloneWindows64, new []{GraphicsDeviceType.Vulkan, GraphicsDeviceType.Direct3D11, GraphicsDeviceType.Direct3D12}); // We need to specify the graphics API for Android builds to ensure proper shader compilation. Vulkan is recommended for modern devices. |
| 67 | + |
| 68 | + AssetDatabase.SaveAssets(); |
| 69 | + |
| 70 | + List<string> scenesToAdd = new List<string>(); |
| 71 | + foreach (var scene in EditorBuildSettings.scenes) |
| 72 | + { |
| 73 | + if (scene.enabled) |
| 74 | + { |
| 75 | + Debug.Log("Adding scene to build: " + scene.path); |
| 76 | + scenesToAdd.Add(scene.path); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + var buildPlayerOptions = new BuildPlayerOptions |
| 81 | + { |
| 82 | + locationPathName = "./build/Windows_Mono/PlaytestBuild.exe", |
| 83 | + target = BuildTarget.StandaloneWindows64, |
| 84 | + options = BuildOptions.Development, |
| 85 | + scenes = scenesToAdd.ToArray() |
| 86 | + }; |
| 87 | + |
| 88 | + BuildPipeline.BuildPlayer(buildPlayerOptions); |
| 89 | + } |
| 90 | + |
| 91 | + [MenuItem("Tools/Builder/Build Development Mac Mono")] |
| 92 | + static void BuildMacMono() |
| 93 | + { |
| 94 | + PlayerSettings.SetScriptingBackend(NamedBuildTarget.Standalone, ScriptingImplementation.Mono2x); |
| 95 | + PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.StandaloneOSX, false); // disable auto graphic to use our own custom list |
| 96 | + PlayerSettings.SetGraphicsAPIs(BuildTarget.StandaloneOSX, new []{GraphicsDeviceType.Metal}); // enforcing Metal Graphics API. Without this there will be shader errors in the final build. |
| 97 | + |
| 98 | + AssetDatabase.SaveAssets(); |
| 99 | + |
| 100 | + List<string> scenesToAdd = new List<string>(); |
| 101 | + foreach (var scene in EditorBuildSettings.scenes) |
| 102 | + { |
| 103 | + if (scene.enabled) |
| 104 | + { |
| 105 | + Debug.Log("Adding scene to build: " + scene.path); |
| 106 | + scenesToAdd.Add(scene.path); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + var buildPlayerOptions = new BuildPlayerOptions |
| 111 | + { |
| 112 | + locationPathName = "./build/macOS_Mono/PlaytestBuild.app", |
| 113 | + target = BuildTarget.StandaloneOSX, |
| 114 | + options = BuildOptions.Development, |
| 115 | + scenes = scenesToAdd.ToArray() |
| 116 | + }; |
| 117 | + |
| 118 | + BuildPipeline.BuildPlayer(buildPlayerOptions); |
| 119 | + } |
| 120 | + |
| 121 | + [MenuItem("Tools/Builder/Build Development Mac Il2cpp")] |
| 122 | + static void BuildMacIl2cpp() |
| 123 | + { |
| 124 | + PlayerSettings.SetScriptingBackend(NamedBuildTarget.Standalone, ScriptingImplementation.IL2CPP); |
| 125 | + PlayerSettings.SetGraphicsAPIs(BuildTarget.StandaloneOSX, new []{GraphicsDeviceType.Metal}); // enforcing Metal Graphics API. Without this there will be shader errors in the final build. |
| 126 | + |
| 127 | + AssetDatabase.SaveAssets(); |
| 128 | + |
| 129 | + List<string> scenesToAdd = new List<string>(); |
| 130 | + foreach (var scene in EditorBuildSettings.scenes) |
| 131 | + { |
| 132 | + if (scene.enabled) |
| 133 | + { |
| 134 | + Debug.Log("Adding scene to build: " + scene.path); |
| 135 | + scenesToAdd.Add(scene.path); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + var buildPlayerOptions = new BuildPlayerOptions |
| 140 | + { |
| 141 | + locationPathName = "./build/macOS_Il2cpp/PlaytestBuild.app", |
| 142 | + target = BuildTarget.StandaloneOSX, |
| 143 | + options = BuildOptions.Development, |
| 144 | + scenes = scenesToAdd.ToArray() |
| 145 | + }; |
| 146 | + |
| 147 | + BuildPipeline.BuildPlayer(buildPlayerOptions); |
| 148 | + } |
| 149 | + |
| 150 | + [MenuItem("Tools/Builder/Build Development Android Il2cpp")] |
| 151 | + static void BuildAndroidIl2cpp() |
| 152 | + { |
| 153 | + PlayerSettings.SetScriptingBackend(NamedBuildTarget.Android, ScriptingImplementation.IL2CPP); |
| 154 | + PlayerSettings.SetApplicationIdentifier(NamedBuildTarget.Android, "com.UnityTestRunner.UnityTestRunner"); // This is needed only for mobiles since by default the application identifier quite often contains invalid characters like spaces so we wan't to make sure that this has a valid value. It's needed only for mobile since that's an app store requirement |
| 155 | + PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.Android, false); // disable auto graphic to use our own custom list |
| 156 | + PlayerSettings.SetGraphicsAPIs(BuildTarget.Android, new []{GraphicsDeviceType.Vulkan}); // We need to specify the graphics API for Android builds to ensure proper shader compilation. Vulkan is recommended for modern devices. |
| 157 | + PlayerSettings.SetArchitecture(BuildTargetGroup.Android,1); // An integer value associated with the architecture of the build target. 0 - None, 1 - ARM64, 2 - Universal. most modern Android devices use the ARM64 architecture |
| 158 | + |
| 159 | + AssetDatabase.SaveAssets(); |
| 160 | + |
| 161 | + List<string> scenesToAdd = new List<string>(); |
| 162 | + foreach (var scene in EditorBuildSettings.scenes) |
| 163 | + { |
| 164 | + if (scene.enabled) |
| 165 | + { |
| 166 | + Debug.Log("Adding scene to build: " + scene.path); |
| 167 | + scenesToAdd.Add(scene.path); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + var buildPlayerOptions = new BuildPlayerOptions |
| 172 | + { |
| 173 | + locationPathName = "./build/Android_Il2cpp_Vulkan/PlaytestBuild.apk", |
| 174 | + target = BuildTarget.Android, |
| 175 | + options = BuildOptions.Development, |
| 176 | + scenes = scenesToAdd.ToArray() |
| 177 | + }; |
| 178 | + |
| 179 | + BuildPipeline.BuildPlayer(buildPlayerOptions); |
| 180 | + } |
| 181 | +} |
0 commit comments