Repository navigation
Expand file tree
/
Copy pathUnityPython.cs
More file actions
112 lines (95 loc) · 3.5 KB
/
Copy pathUnityPython.cs
File metadata and controls
112 lines (95 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
namespace Exodrifter.UnityPython
{
/// <summary>
/// Convenience class for creating a Python engine integrated with Unity.
///
/// All scripts executed by the ScriptEngine created by this class will:
/// * Redirect output to Unity's console
/// * Be able to import any class in a `UnityEngine*` namespace
/// * Be able to import any class in a `UnityEditor*` namespace, if the script
/// is running in the UnityEditor
/// </summary>
public static class UnityPython
{
public static ScriptEngine CreateEngine(UnityPythonCreationSetting setting)
{
var runtime = Python.CreateRuntime(setting.Options);
// Redirect IronPython IO
if (setting.RedirectStandardOutputStream)
{
var infoStream = new UniPyLogStream(Debug.Log);
runtime.IO.SetOutput(infoStream, Encoding.UTF8);
}
if (setting.RedirectStandardErrorStream)
{
var errorStream = new UniPyLogStream(Debug.LogError);
runtime.IO.SetErrorOutput(errorStream, Encoding.UTF8);
}
var engine = Python.GetEngine(runtime);
// Load assemblies for the `UnityEngine*` namespaces
if (setting.IncludeUnityEngine)
{
foreach (var assembly in GetAssembliesInNamespace("UnityEngine"))
{
engine.Runtime.LoadAssembly(assembly);
}
}
// Load assemblies for the `UnityEditor*` namespaces
if (setting.IncludeUnityEditorIfPossible)
{
#if UNITY_EDITOR
foreach (var assembly in GetAssembliesInNamespace("UnityEditor"))
{
engine.Runtime.LoadAssembly(assembly);
}
#endif
}
else
{
#if !UNITY_EDITOR
Debug.Log("Can not load namespace of UnityEditor! Please set UnityPythonCreationSetting.IncludeUnityEditorIfPossible to false.");
#endif
}
// Load Specific given namespace
if (setting.CustomIncluding != null)
{
foreach (var ns in setting.CustomIncluding)
{
foreach (var assembly in GetAssembliesInNamespace(ns))
{
engine.Runtime.LoadAssembly(assembly);
}
}
}
return engine;
}
public static ScriptEngine CreateEngine() => CreateEngine(UnityPythonCreationSetting.CreateNew());
public static ScriptEngine CreateEngine(params string[] customInclude)
=> CreateEngine(UnityPythonCreationSetting.CreateNew(customIncluding: customInclude));
public static ScriptEngine CreateEngine(IDictionary<string, object> options = null, params string[] customInclude)
=> CreateEngine(UnityPythonCreationSetting.CreateNew(options: options, customIncluding: customInclude));
/// <summary>
/// Get a list of all loaded assemblies in the current AppDomain for a
/// namespace beginning with the specified string.
/// </summary>
/// <param name="prefix">The beginning of the namespace.</param>
/// <returns>All matching assemblies.</returns>
private static IEnumerable<Assembly> GetAssembliesInNamespace(string prefix)
{
return AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(t => t.GetTypes())
.Where(t => t.Namespace != null && t.Namespace.StartsWith(prefix))
.Select(t => t.Assembly)
.Distinct();
}
}
}