Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

Commit 2295dff

Browse files
committed
Redirect IronPython output to Unity console
1 parent 44da3be commit 2295dff

4 files changed

Lines changed: 66 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
88

9+
### Added
10+
* IronPython output is now redirected to the Unity console
11+
912
### Fixed
1013
* Fix problem where not all types in the `UnityEngine` namespace could be
1114
imported

Scripts/DebugLogWriter.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.IO;
3+
4+
/// <summary>
5+
/// Used to redirect IronPython output to the Unity debug console.
6+
/// </summary>
7+
public class UnityLogWriter : StreamWriter
8+
{
9+
private readonly Action<string> logger;
10+
private string buffer = "";
11+
12+
public UnityLogWriter(Action<string> logger, Stream s) : base(s)
13+
{
14+
if (logger == null)
15+
{
16+
throw new ArgumentNullException("logger");
17+
}
18+
this.logger = logger;
19+
}
20+
21+
public override void Write(string value)
22+
{
23+
base.Write(value);
24+
buffer += value;
25+
26+
var lines = buffer.Split('\n');
27+
for (var i = 0; i < lines.Length; ++i)
28+
{
29+
if (i == lines.Length - 1)
30+
{
31+
buffer = lines[i];
32+
}
33+
else
34+
{
35+
logger(lines[i]);
36+
}
37+
}
38+
}
39+
}

Scripts/DebugLogWriter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/UnityPython.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
using Microsoft.Scripting.Hosting;
33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Linq;
67
using System.Reflection;
8+
using UnityEngine;
79

810
/// <summary>
911
/// Convenience class for creating a Python engine integrated with Unity.
1012
///
1113
/// All scripts executed by the ScriptEngine created by this class will:
14+
/// * Redirect output to Unity's console
1215
/// * Be able to import any class in a `UnityEngine*` namespace
1316
/// * Be able to import any class in a `UnityEditor*` namespace, if the script
1417
/// is running in the UnityEditor
@@ -19,6 +22,16 @@ public static ScriptEngine CreateEngine(IDictionary<string, object> options = nu
1922
{
2023
var engine = Python.CreateEngine(options);
2124

25+
// Redirect IronPython IO
26+
var infoStream = new MemoryStream();
27+
var infoWriter = new UnityLogWriter(Debug.Log, infoStream);
28+
engine.Runtime.IO.SetOutput(infoStream, infoWriter);
29+
30+
var errorStream = new MemoryStream();
31+
var errorWriter = new UnityLogWriter(Debug.Log, errorStream);
32+
engine.Runtime.IO.SetErrorOutput(errorStream, errorWriter);
33+
34+
2235
// Load assemblies for the `UnityEngine*` namespaces
2336
foreach (var assembly in GetAssembliesInNamespace("UnityEngine"))
2437
{

0 commit comments

Comments
 (0)