Version: 2022.3


When building a player, you sometimes want to modify the built player in some way. For example you might want to add a custom icon, copy some documentation next to the player or build an Installer. You can do this via editor scripting using BuildPipeline.BuildPlayer to run the build and then follow it with whatever postprocessing code you need:

// C# 示例。
using UnityEditor;
using System.Diagnostics;

public class ScriptBatch 
    [MenuItem("MyTools/Windows Build With Postprocess")]
    public static void BuildGame ()
        // 获取文件名。
        string path = EditorUtility.SaveFolderPanel("Choose Location of Built Game", "", "");
        string[] levels = new string[] {"Assets/Scene1.unity", "Assets/Scene2.unity"};

        // 构建播放器。
        BuildPipeline.BuildPlayer(levels, path +"/BuiltGame.exe", BuildTarget.StandaloneWindows, BuildOptions.None);

        // 将文件从项目文件夹复制到构建文件夹,与构建的游戏放在一起。
        FileUtil.CopyFileOrDirectory("Assets/Templates/Readme.txt", path + "Readme.txt");

        // 运行游戏(System.Diagnostics 中的 Process 类)。
        Process proc = new Process();
        proc.StartInfo.FileName = path +"/BuiltGame.exe";

PostProcessBuild 属性

还可以使用 PostProcessBuildAttribute 的 postprocessOrder 参数来定义构建方法的执行顺序,并使用 Process 类从这些方法调用外部脚本,如上一部分所示。此参数用于将构建方法从低到高排序,可为其分配任何负值或正值。