Skip to content

Instantly share code, notes, and snippets.

@liortal53
Last active December 25, 2020 03:41
Show Gist options
  • Save liortal53/aefd0322438737750e2e to your computer and use it in GitHub Desktop.
Save liortal53/aefd0322438737750e2e to your computer and use it in GitHub Desktop.
Redirect Console output to Unity's console
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
public class ConsoleWriter : TextWriter
{
public override void Write(char value)
{
}
public override void Write(string value)
{
Debug.Log(value);
}
public override Encoding Encoding
{
get { return Encoding.ASCII; }
}
}
public class ErrorWriter : TextWriter
{
public override void Write(char value)
{
}
public override void Write(string value)
{
Debug.LogError(value);
}
public override Encoding Encoding
{
get { return Encoding.ASCII; }
}
}
[InitializeOnLoad]
public class ConsoleRouter
{
static ConsoleRouter()
{
System.Console.SetOut(new ConsoleWriter());
System.Console.SetError(new ErrorWriter());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment