免責事項:この記事はブロガーオリジナル記事です。ブロガーの同意なしに複製してはなりません。https://blog.csdn.net/akof1314/article/details/36048747
キャプチャの方法は、「から取られるアクションでIronPythonの」、次のようなコードの本:
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
使用して
システムを。
使用して System.Collections.Genericを。 使用して System.IOを。 使用 するSystem.Reflectionを。 使用して System.Textのを。 使用して のSystem.Windows.Formsを。 使用して IronPython.Hostingを。 使用して IronPython.Runtimeを。 使用して IronPython.Runtime.Exceptionsを。 使用して Microsoft.Scriptingを。 使用して Microsoft.Scripting.Runtimeを。 使用して Microsoft.Scripting.Hostingを。 使用して プラグインを。 名前空間 EmbeddingPlugin { 内部 クラス PythonStream:MemoryStreamを { テキストボックス_output。 公共 PythonStream(テキストボックス、テキストボックス) { _output =テキストボックス。 } パブリック オーバーライド ボイド 書き込み( バイト []バッファ、 int型 、オフセット int型の 数) { _output.AppendText(Encoding.UTF8.GetString(バッファ、オフセット、カウント))。 } } 内部 クラス エンジン { たScriptEngineの_engine。 ScriptRuntimeの_runtime。 テキストボックス_box。 公共の 一覧<PluginBase>プラグイン { {取得 戻り PluginStore.Pluginsを。} } パブリック エンジン(テキストボックス、テキストボックス) { _engine = Python.CreateEngine()。 _runtime = _engine.Runtime。 _box =テキストボックス。 SetStreams(); ストリング ROOTDIR = AddAssemblies()。 LoadPlugins(ROOTDIR)。 } 公共 ボイド SetStreams() { PythonStreamストリーム= 新しい PythonStream(_box)。 _runtime.IO.SetOutput(ストリーム、Encoding.UTF8)。 _runtime.IO.SetErrorOutput(ストリーム、Encoding.UTF8)。 } パブリック 文字列 AddAssemblies() { アセンブリmainAssembly = Assembly.GetExecutingAssembly()。 ストリング ROOTDIR = Directory.GetParent(mainAssembly.Location).FullName。 弦 pluginsPath = Path.Combine(ROOTDIR、 "Plugins.dll" )。 アセンブリpluginsAssembly = Assembly.LoadFile(pluginsPath)。 _runtime.LoadAssembly(mainAssembly)。 _runtime.LoadAssembly(pluginsAssembly)。 _runtime.LoadAssembly( typeof演算 (文字列).Assembly)。 _runtime.LoadAssembly( typeof演算 (URI).Assembly)。 返す ROOTDIRを。 } 公共 ボイド LoadPlugins( 文字列 ROOTDIR) { ストリング pluginsDir = Path.Combine(ROOTDIR、 "プラグイン" )。 foreachの ( 文字列 パス に Directory.GetFiles(pluginsDir)) { 場合 (path.ToLower()EndsWith( "の.py" )) { CreatePlugin(パス)。 } } } 公共 ボイド CreatePlugin( 文字列 パス) { 試みます { ScriptSourceスクリプト= _engine.CreateScriptSourceFromFile(パス)。 CompiledCodeコード= script.Compile()。 ScriptScopeスコープ= _engine.CreateScope()。 script.Execute(スコープ) } キャッチ (SyntaxErrorException電子) { 文字列 MSG = "構文エラー\で" {0} \ "" 。 ShowError(MSG、Path.GetFileName(パス)、E)。 } キャッチ (SystemExitException電子) { 文字列 MSG = "\でSystemExitを" {0} \ "" 。 ShowError(MSG、Path.GetFileName(パス)、E)。 } キャッチ (例外e) { 文字列 MSG = "エラーローディングプラグイン\" {0} \ "" 。 ShowError(MSG、Path.GetFileName(パス)、E)。 } } 公共 ボイド ShowError( 列 タイトル、 列 名、例外e) { 文字列 キャプション= String.Formatの(タイトル名); ExceptionOperations EO = _engine.GetService <ExceptionOperations>(); 文字列 エラー= eo.FormatException(E); MessageBox.Show(エラー、キャプション、MessageBoxButtons.OK、MessageBoxIcon.Error)。 } 公共 ボイド ExecutePluginAtIndex( int型の インデックス) { PluginBaseプラグインプラグイン= [インデックス]。 試す { plugin.Execute(_box)。 } キャッチ (例外e) { 文字列 MSG = "エラー実行プラグイン\" {0} \ "" 。 ShowError(MSG、plugin.Name、E)。 } } } } |
結果は以下のとおりです。
でUnityに移植しました。ただ、キーコードが可能になります。