构建播放器时,有时会想在某些方面修改已构建的播放器。例如,可能想添加一个自定义图标,复制一些文件到播放器旁边或构建一个安装程序 (Installer)。手动进行这些操作会让人觉得非常枯燥,如果知道如何编写 sh 或 perl 脚本,就能自动执行该任务。
创建完播放器之后,Unity 会自动在工程 (Project) 的资源 (Assets)/编辑器 (Editor) 文件夹下查找名为 PostprocessBuildPlayer(不带任何扩展名)的 sh 或 perl 脚本。找到文件后会在完成播放器构建时调用。
在该脚本中,可随意发布处理播放器。如构建一个播放器的外部安装程序。
可使用 perl、sh 或任何其他与命令行兼容的语言。
Unity 将一些有用的命令行参数传递给脚本,使您了解是播放器类型及其保存位置。
当前目录将设置为工程文件夹,资源 (Assets) 文件夹就包含在内。
#!/usr/bin/perl my $installPath = $ARGV[0]; # The type of player built: # "dashboard", "standaloneWin32", "standaloneOSXIntel", "standaloneOSXPPC", "standaloneOSXUniversal", "webplayer" my $target = $ARGV[1]; # What optimizations are applied. At the moment either "" or "strip" when Strip debug symbols is selected. my $optimization = $ARGV[2]; # The name of the company set in the project settings my $companyName = $ARGV[3]; # The name of the product set in the project settings my $productName = $ARGV[4]; # The default screen width of the player. my $width = $ARGV[5]; # The default screen height of the player my $height = $ARGV[6]; print ("\n*** Building at '$installPath' with target: $target \n");
注意,Python 等有些语言将脚本名称传递为命令行参数之一。如果使用其中一种语言,这些参数将沿着数组中的一个位置高效转移(因此安装路径为 ARGV[1] 等等)。
为了看到这个活动中的功能,请访问本网站的工程示例页面并下载 PostprocessBuildPlayer 示例包文件,将其导入后用于自己的工程当中。它使用创建播放器管道 (Build Player Pipeline) 功能提供 自定义的网络播放器构建后置处理,以展示可在自己的 PostprocessBuildPlayer 脚本中执行的自定义构建行为类型。
Windows 系统不支持 PostprocessBuildPlayer 脚本,但是可以使用编辑器脚本达到同样的效果。可使用 BuildPipeline.BuildPlayer 运行此版本,后面加上所需的任何后处理代码:-
using UnityEditor; using System.Diagnostics; public class ScriptBatch : MonoBehaviour { [MenuItem("MyTools/Windows Build With Postprocess")] public static void BuildGame () { // Get filename. string path = EditorUtility.SaveFolderPanel("Choose Location of Built Game", "", ""); // Build player. BuildPipeline.BuildPlayer(levels, path + "/BuiltGame.exe", BuildTarget.StandaloneWindows, BuildOptions.None); // Copy a file from the project folder to the build folder, alongside the built game. FileUtil.CopyFileOrDirectory("Assets/WebPlayerTemplates/Readme.txt", path + "Readme.txt"); // Run the game (Process class from System.Diagnostics). Process proc = new Process(); proc.StartInfo.FileName = path + "BuiltGame.exe"; proc.Start(); } }
Page last updated: 2013-06-30