Skip to content

Instantly share code, notes, and snippets.

@t-mat
Created August 2, 2016 16:48
Show Gist options
  • Save t-mat/cf0c3dc4dec91d033b44eba1f9a4bb0b to your computer and use it in GitHub Desktop.
Save t-mat/cf0c3dc4dec91d033b44eba1f9a4bb0b to your computer and use it in GitHub Desktop.
[UnityEditorExtension] [Windows] AllocConsole for batch-mode
#if UNITY_EDITOR
// unity.exe -projectPath <PATH/TO/YOUR/PROJECT> -batchmode -executeMethod EditorTest.TestFunc -quit
using UnityEditor;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
public class EditorTest
{
public static void TestFunc() {
AttachConsole();
Console.WriteLine("EditorTest.TestFunc : " + DateTime.Now.ToString());
}
static void AttachConsole() {
#if UNITY_EDITOR_WIN
AttachConsole(-1);
var stdout = Console.OpenStandardOutput();
var sw = new System.IO.StreamWriter(stdout, Encoding.Default);
sw.AutoFlush = true;
Console.SetOut(sw);
Console.SetError(sw);
#endif
}
#if UNITY_EDITOR_WIN
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool AttachConsole(int pid);
#endif
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment