我已经创建了在 C# 中嵌入 IronPython 代码的代码
public ScriptEngine GetEngine() {
if( _engine != null )
return _engine;
_engine = Python.CreateEngine();
var paths = _engine.GetSearchPaths();
paths.Add( _pythonPath );
_engine.SetSearchPaths( paths );
var runtime = _engine.Runtime;
runtime.LoadAssembly( typeof( CharacterCore ).Assembly );
return _engine;
}
public IAbility Movement() {
var engine = GetEngine();
var script = engine.CreateScriptSourceFromFile( Path.Combine( _pythonPath, "movement.py" ) );
var code = script.Compile();
var scope = engine.CreateScope();
code.Execute( scope );
return scope.GetVariable( "result" );
}
Run Code Online (Sandbox Code Playgroud)
在同一解决方案中的单独 Python 项目中使用适当的 Python 代码。代码本身有效,但如果我在 Python …