在使用Mirror做项目的时候经常需要打包程序,为了方便快速打包可以使用这段代码。
新建一个BuildScript.cs,放在Editor的文件夹中,然后把下面的代码复制进去。在Unity编辑器的菜单栏中就会多出一个Build菜单,可以打包WindowServer、LinuxServer、WindowClient。
using System; using System.IO; using UnityEditor; using UnityEngine; public class BuildScript { [MenuItem("Build/Build All")] public static void BuildAll() { BuildWindowsServer(); BuildLinuxServer(); BuildWindowsClient(); } [MenuItem("Build/Build Server (Windows)")] public static void BuildWindowsServer() { BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions(); buildPlayerOptions.scenes = new[] { "Assets/Scenes/Main.unity" }; buildPlayerOptions.locationPathName = "Builds/Windows/Server/Server.exe"; buildPlayerOptions.target = BuildTarget.StandaloneWindows64; buildPlayerOptions.options = BuildOptions.CompressWithLz4HC | BuildOptions.EnableHeadlessMode; Console.WriteLine("Building Server (Windows)..."); BuildPipeline.BuildPlayer(buildPlayerOptions); Console.WriteLine("Built Server (Windows)."); } [MenuItem("Build/Build Server (Linux)")] public static void BuildLinuxServer() { BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions(); buildPlayerOptions.scenes = new[] { "Assets/Scenes/Main.unity" }; buildPlayerOptions.locationPathName = "Builds/Linux/Server/Server.x86_64"; buildPlayerOptions.target = BuildTarget.StandaloneLinux64; buildPlayerOptions.options = BuildOptions.CompressWithLz4HC | BuildOptions.EnableHeadlessMode; Console.WriteLine("Building Server (Linux)..."); BuildPipeline.BuildPlayer(buildPlayerOptions); Console.WriteLine("Built Server (Linux)."); } [MenuItem("Build/Build Client (Windows)")] public static void BuildWindowsClient() { BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions(); buildPlayerOptions.scenes = new[] { "Assets/Scenes/Main.unity" }; buildPlayerOptions.locationPathName = "Builds/Windows/Client/Client.exe"; buildPlayerOptions.target = BuildTarget.StandaloneWindows64; buildPlayerOptions.options = BuildOptions.CompressWithLz4HC; Console.WriteLine("Building Client (Windows)..."); BuildPipeline.BuildPlayer(buildPlayerOptions); Console.WriteLine("Built Client (Windows)."); } }