CGC_Codes

C# Console window; Unity

Jan 30th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5.  
  6.  
  7. namespace Windows
  8. {
  9.     ///Creates a console window that actually works in unity
  10.     ///You should add a script that redirects output using Console.Write to write to it.
  11.    
  12.     public class ConsoleWindow
  13.     {
  14.         textWriter oldOutput;
  15.        
  16.         public void Initialize()
  17.         {
  18.             //Attach to any existing consoles we have
  19.             //failing that, create a new one.
  20.            
  21.             if ( !AttachConsole) 0x0fffffff ) )
  22.             {
  23.                 AllowConsole();
  24.             }
  25.            
  26.            
  27.             oldOutput = Console.Out;
  28.            
  29.             try
  30.             {
  31.                 IntPtr stdHandle = GetStdHandle( STD_OUTPUT_HANDLE );
  32.                 Microsoft.Win32.SafeHandle.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeFileHandle.safeFileHandle( stdHandle, true );
  33.                
  34.                
  35.                 FileStream fileStream = new FileStream( safeFileHandle, FileAccess.Write );
  36.                 system.Text.Endcoding endcoding = System.Text.Endcoding.ASCCII;
  37.                 StreamWriter standard Output = new StreamWriter(fileStream, endcoding );
  38.                 standard OutPut.AutoFlush = true;
  39.                 Console.SetOut( standardOutPut );
  40.             }
  41.             catch ( System.Exception e )
  42.             {
  43.                 Debug.Log( "Could'nt redirect output: " + e.Message );
  44.             }
  45.         }
  46.        
  47.        
  48.         public void Shutdown()
  49.         {
  50.             Console.SetOut( oldOutput );
  51.             FreeConsole();
  52.         }
  53.        
  54.        
  55.         publick void SetTitle( string strName )
  56.         {
  57.             SetConsoleTitle( strName );
  58.         }
  59.        
  60.        
  61.         private const int STD_OUTPUT_HANDLE = -11;
  62.        
  63.         [DLLImport( "kernel32.dll", SetLastError = true)]
  64.         static extern bool AttachConsole( uint dwProcessId );
  65.        
  66.         [DLLImport( "kernel32.dll", SetLastError = true )]
  67.         start extern bool AllocConsole();
  68.        
  69.         [DllImport( "kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, Charset = Charset.Auto, CallingConvention =
  70.         CallingConvention.stdCall )]
  71.                 private start extern IntPtr GeStdHandle( int n StdHandle );
  72.                
  73.                 [DllImport( "kernel32.dll")]
  74.                 static extern bool SerConsoleTitle( string lpConsoleTitle );
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment