PostprocessBuildPlayer scripts do not fire on windows machines to date, and a pre process script for unity does not exist in the public domain, so here is an editor script which adds two custom buttons to your menu:- Custom Build, and Custom Build-Run. The purpose of this script is to allow you to handle any file copying or amendment to the build folder pre or post build automatically.
The script takes all the information required from the normal build window options into account, when executed manually it allows the user to choose a save path, it then fires a pre-process function before building and post process function after building and the build path is sent as a parameter, from within these functions you can call whatever you heart desires from c# file copying code to calling other applications and sending parameters alongside
Notes:-
There may be a few blanks to fill in with the code below in certain scenarios, I haven’t included all the build application extensions ( as I am unsure of all of them, comment with them if you know better!).
Not sure on compatibilty with Unity Indy?
Could do with a clean up function that deletes unused unity build source files, that sometimes linger in the build directory.
Here’s the code, I hope it helps:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
using UnityEngine; using System.Collections; using UnityEditor; public class BuildProcessor : MonoBehaviour { [MenuItem ("Build/Custom Build",false,0)] static void CustomBuildOnly() { CustomBuild(false); } [MenuItem ("Build/Custom Build - Run",false,1)] static void CustomBuildRun() { CustomBuild(true); } static void CustomBuild(bool runbuild) { BuildOptions customBuildOptions=EditorUserBuildSettings.architectureFlags; if (runbuild) { //set EditorUserBuildSettings.architectureFlags enum flags set buildrun bit on if ((customBuildOptions & BuildOptions.AutoRunPlayer) != BuildOptions.AutoRunPlayer) { customBuildOptions = customBuildOptions | BuildOptions.AutoRunPlayer; } } else { //set EditorUserBuildSettings.architectureFlags enum flags set showfolder bit on if ((customBuildOptions & BuildOptions.ShowBuiltPlayer) != BuildOptions.ShowBuiltPlayer) { customBuildOptions = customBuildOptions | BuildOptions.ShowBuiltPlayer; } } BuildTarget mycustombuildtarget=EditorUserBuildSettings.activeBuildTarget; string extn=""; switch (EditorUserBuildSettings.selectedBuildTargetGroup) { case BuildTargetGroup.Standalone: switch (EditorUserBuildSettings.selectedStandaloneTarget) { case BuildTarget.DashboardWidget: mycustombuildtarget=BuildTarget.DashboardWidget; extn="wdgt"; break; case BuildTarget.StandaloneWindows: mycustombuildtarget=BuildTarget.StandaloneWindows; extn="exe"; break; // case BuildTarget.StandaloneWindows64: // mycustombuildtarget=BuildTarget.StandaloneWindows64; // extn="exe"; // break; case BuildTarget.StandaloneOSXUniversal: mycustombuildtarget=BuildTarget.StandaloneOSXUniversal; extn="app"; break; case BuildTarget.StandaloneOSXPPC: mycustombuildtarget=BuildTarget.StandaloneOSXPPC; extn="app"; break; case BuildTarget. StandaloneOSXIntel: mycustombuildtarget=BuildTarget. StandaloneOSXIntel; extn="app"; break; } break; case BuildTargetGroup.WebPlayer: if (EditorUserBuildSettings.webPlayerStreamed) { mycustombuildtarget=BuildTarget.WebPlayerStreamed; extn="unity3d"; } else { mycustombuildtarget=BuildTarget.WebPlayer; extn="unity3d"; } break; case BuildTargetGroup.Wii: mycustombuildtarget=BuildTarget.Wii; //extn="???" break; case BuildTargetGroup.iPhone: mycustombuildtarget=BuildTarget.iPhone; extn="xcode"; break; case BuildTargetGroup.PS3: mycustombuildtarget=BuildTarget.PS3; //extn="???" break; case BuildTargetGroup.XBOX360: mycustombuildtarget=BuildTarget.XBOX360; //extn="???" break; case BuildTargetGroup.Android: mycustombuildtarget=BuildTarget.Android; extn="apk"; break; case BuildTargetGroup.Broadcom: mycustombuildtarget=BuildTarget.StandaloneBroadcom; //extn="???" break; case BuildTargetGroup.GLESEmu: mycustombuildtarget=BuildTarget.StandaloneGLESEmu; //extn="???" break; case BuildTargetGroup.NaCl: mycustombuildtarget=BuildTarget.NaCl; //extn="???" break; } string savepath = EditorUtility.SaveFilePanel("Build "+mycustombuildtarget, EditorUserBuildSettings.GetBuildLocation(mycustombuildtarget),"",extn); if(savepath.Length != 0) { string dir=System.IO.Path.GetDirectoryName(savepath); //get build directory string[] scenes=new string[EditorBuildSettings.scenes.Length]; for(int i=0;i<EditorBuildSettings.scenes.Length;++i) { scenes[i]=EditorBuildSettings.scenes[i].path.ToString(); }; PreProcess(dir); BuildPipeline.BuildPlayer(scenes,savepath,mycustombuildtarget,customBuildOptions); EditorUserBuildSettings.SetBuildLocation(mycustombuildtarget,dir); //store new location for this type of build PostProcess(dir); // * warning, if set to build and run in build settings, this may fire after application execution. } Debug.Log(mycustombuildtarget.ToString() ); } static void PreProcess(string BuildPathDir) { //Your pre-process here, copy Assets/Folders to Directory, edit html pages etc //or you could execute a shell, cgi-perl or bat command by doing something like below:- //Application.OpenURL("yourfile.bat "+BuildPathDir); //etc } static void PostProcess(string BuildPathDir) { //Your post-process here, copy Assets/Folders to Directory, edit html pages etc //*warning, if set to run after build, these commands may execute after the built application is run. //Application.OpenURL("yourfile.bat "+BuildPathDir); //etc } } |
Thanks for sharing this code! Very useful!
You just need to replace “&” for “&” and “<” for “<".
LOL! trolled!
Let’s try again putting spaces. Replace “& a m p ;” for “&” and “& l t ;” for “<"
Thanks man, very useful script!
I just wrote a custom build script for my Unity project and this example was very helpful, thanks.
There were some slight changes I made to the code near the end, and I figure you might want these changes as well:
string path = EditorUserBuildSettings.GetBuildLocation(EditorUserBuildSettings.activeBuildTarget);
string dir = System.IO.Path.GetDirectoryName(path);
string name = System.IO.Path.GetFileName(path);
path = EditorUtility.SaveFilePanel(“Choose Build Location”, dir, name, “”);
if (path.Length != 0) {
int length = EditorBuildSettings.scenes.Length;
List scenes = new List();
for (int i = 0; i < length; i++) {
if (EditorBuildSettings.scenes[i].enabled)
scenes.Add(EditorBuildSettings.scenes[i].path.ToString());
};
BuildPipeline.BuildPlayer(scenes.ToArray(), path, EditorUserBuildSettings.activeBuildTarget, BuildOptions.None);
EditorUserBuildSettings.SetBuildLocation(EditorUserBuildSettings.activeBuildTarget, path);
}
Two things in particular were tweaked/fixed from your original code: the Save dialog that comes up needed to have directory and name separated, and the array of scenes to build should have disabled scenes excluded.