Skip to content

Instantly share code, notes, and snippets.

@darktable
Created July 24, 2012 22:11
Show Gist options
  • Save darktable/3173013 to your computer and use it in GitHub Desktop.
Save darktable/3173013 to your computer and use it in GitHub Desktop.
Unity3D: C# replacement for the python version of PostprocessBuildPlayer
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEditor.Callbacks;
using System.IO;
public class PostprocessBuildPlayer : ScriptableObject {
[PostProcessBuild]
static void OnPostprocessBuildPlayer(BuildTarget target, string buildPath) {
string editorPath = Path.GetFullPath(Path.GetDirectoryName(AssetDatabase.GetAssetPath(MonoScript.FromScriptableObject(ScriptableObject.CreateInstance<PostprocessBuildPlayer>()))));
if (!Directory.Exists(editorPath)) {
Debug.LogError("No directory at: " + editorPath);
return;
}
if (!Directory.Exists(buildPath)) {
Debug.LogError("No directory at: " + buildPath);
return;
}
var files = Directory.GetFiles(editorPath, "*.*", SearchOption.TopDirectoryOnly);
files = System.Array.FindAll<string>(files, (x) => {
var name = Path.GetFileName(x).ToLower();
return name.StartsWith("postprocessbuildplayer_") && !name.EndsWith("meta");
});
if (files.Length == 0) {
Debug.LogError("No postprocess scripts at: " + editorPath);
return;
}
var args = new string[] {
buildPath,
target.ToString()
};
foreach (var file in files) {
Debug.Log("executing: " + file);
using (var process = new System.Diagnostics.Process()) {
process.StartInfo.FileName = file;
process.StartInfo.Arguments = string.Join(" ", args);
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.Start();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0) {
Debug.LogError(process.ExitCode + " : " + error);
}
}
}
}
}
@yadongliu
Copy link

Thanks for sharing the script. I wonder if you are using the same Apache license on this as your other gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment