Skip to content

Instantly share code, notes, and snippets.

@RichyHBM
Last active August 29, 2015 14:27
Show Gist options
  • Save RichyHBM/1a8ccf0c066bbb20f315 to your computer and use it in GitHub Desktop.
Save RichyHBM/1a8ccf0c066bbb20f315 to your computer and use it in GitHub Desktop.
Unity3d custom window to allow compilation and replacement of custom DLL code
#if UNITY_EDITOR
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Diagnostics;
using UnityEditor;
public class RebuildDll : EditorWindow
{
//List of .csproj files to build
List<String> Projects = new List<String> ();
//Location to place the built dll in the unity project
String Location = String.Format ("Assets{0}code", Path.DirectorySeparatorChar);
[MenuItem ("Assets/Tools/Build DLLs")]
public static void ShowWindow ()
{
EditorWindow.GetWindow (typeof(RebuildDll));
}
void OnGUI ()
{
EditorGUILayout.LabelField ("DLL Location");
Location = EditorGUILayout.TextField (Location);
EditorGUILayout.LabelField ("CS Project Files");
Projects = EditorGUILayout.TextArea (String.Join (Environment.NewLine, Projects.ToArray ()))
.Split (Environment.NewLine.ToCharArray ()).ToList ();
if (GUILayout.Button ("Build DLL's"))
{
foreach (String project in Projects)
{
string fileName = Path.GetFileNameWithoutExtension (project);
try {
Process buildProcess = new Process ();
//For non-mono runtimes this should probably use msbuild
buildProcess.StartInfo.FileName = "xbuild";
buildProcess.StartInfo.Arguments = "/p:Configuration=Release /t:Rebuild " + project;
buildProcess.StartInfo.UseShellExecute = false;
buildProcess.StartInfo.RedirectStandardOutput = true;
buildProcess.StartInfo.RedirectStandardError = true;
buildProcess.Start ();
string stdOutput = buildProcess.StandardOutput.ReadToEnd ();
string errorOutput = buildProcess.StandardError.ReadToEnd ();
buildProcess.WaitForExit ();
UnityEngine.Debug.Log (stdOutput);
if(!String.IsNullOrEmpty(errorOutput))
UnityEngine.Debug.LogError (errorOutput);
String builtDllLocation = String.Format ("{0}{1}bin{1}Release{1}{2}.dll", Path.GetDirectoryName (project), Path.DirectorySeparatorChar, fileName);
if (File.Exists (Location + Path.DirectorySeparatorChar + fileName + ".dll") && File.Exists (builtDllLocation))
{
File.Replace (builtDllLocation, Location + Path.DirectorySeparatorChar + fileName + ".dll", null);
UnityEngine.Debug.Log ("Replaced " + fileName);
}
} catch (Exception e) {
EditorUtility.DisplayDialog ("Error building DLL's", e.Message, "OK");
}
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment