Created
November 20, 2025 02:39
-
-
Save Starpelly/b5aa6a048d426d98d7228f8ddf86d51b to your computer and use it in GitHub Desktop.
Creates a console window that actually works in Unity, written by Garry Newman but I'm posting this here for archival reasons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System; | |
| using System.Collections; | |
| using System.Runtime.InteropServices; | |
| using System.IO; | |
| using System.Security; | |
| namespace Windows | |
| { | |
| /// <summary> | |
| /// Creates a console window that actually works in Unity | |
| /// You should add a script that redirects output using Console.Write to write to it. | |
| /// </summary> | |
| [SuppressUnmanagedCodeSecurity] | |
| public class ConsoleWindow | |
| { | |
| TextWriter oldOutput; | |
| public void Initialize() | |
| { | |
| // | |
| // Attach to any existing consoles we have | |
| // failing that, create a new one. | |
| // | |
| if ( !AttachConsole( 0x0ffffffff ) ) | |
| { | |
| AllocConsole(); | |
| } | |
| oldOutput = Console.Out; | |
| try | |
| { | |
| System.Text.Encoding encoding = System.Text.Encoding.UTF8; | |
| System.Console.OutputEncoding = encoding; | |
| IntPtr stdHandle = GetStdHandle( STD_OUTPUT_HANDLE ); | |
| Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle( stdHandle, true ); | |
| FileStream fileStream = new FileStream( safeFileHandle, FileAccess.Write ); | |
| StreamWriter standardOutput = new StreamWriter( fileStream, encoding ); | |
| standardOutput.AutoFlush = true; | |
| Console.SetOut( standardOutput ); | |
| } | |
| catch ( System.Exception e ) | |
| { | |
| Debug.Log( "Couldn't redirect output: " + e.Message ); | |
| } | |
| } | |
| public void Shutdown() | |
| { | |
| Console.SetOut( oldOutput ); | |
| FreeConsole(); | |
| } | |
| public void SetTitle( string strName ) | |
| { | |
| SetConsoleTitleA( strName ); | |
| } | |
| private const int STD_INPUT_HANDLE = -10; | |
| private const int STD_OUTPUT_HANDLE = -11; | |
| [DllImport( "kernel32.dll", SetLastError = true )] | |
| static extern bool AttachConsole( uint dwProcessId ); | |
| [DllImport( "kernel32.dll", SetLastError = true )] | |
| static extern bool AllocConsole(); | |
| [DllImport( "kernel32.dll", SetLastError = true )] | |
| static extern bool FreeConsole(); | |
| [DllImport( "kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall )] | |
| private static extern IntPtr GetStdHandle( int nStdHandle ); | |
| [DllImport( "kernel32.dll" )] | |
| static extern bool SetConsoleTitleA( string lpConsoleTitle ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment