我想用IronPython作为Unity的外部语言脚本。IronPython执行加载所需的DLL放在Assets\Plugins。然而,当我运行脚本的时候出现了如下错误:
PythonImportErrorException: No module named UnityEngine
IronPython.Modules.Builtin.__import__ (IronPython.Runtime.Calls.ICallerContext,string,object,object,object) <IL 0x0003b, 0x001cc>
(wrapper dynamic-method) object.__import__##5 (IronPython.Runtime.Calls.ICallerContext,object,object,object,object) <IL 0x0000e, 0x0004d>
IronPython.Runtime.Calls.FastCallableWithContextAny.Call (IronPython.Runtime.Calls.ICallerContext,object,object,object,object) <IL 0x00015, 0x00067>
IronPython.Runtime.Calls.BuiltinFunction.Call (IronPython.Runtime.Calls.ICallerContext,object,object,object,object) <IL 0x0000d, 0x00058>
IronPython.Runtime.Operations.Ops.CallWithContext (IronPython.Runtime.Calls.ICallerContext,object,object,object,object,object) <IL 0x00012, 0x000b0>
IronPython.Runtime.Importer.Import (IronPython.Runtime.PythonModule,string,IronPython.Runtime.List) <IL 0x0000d, 0x0006c>
IronPython.Runtime.Operations.Ops.Import (IronPython.Runtime.PythonModule,string) <IL 0x00007, 0x0003b>
(wrapper dynamic-method) object.<string>##1 (IronPython.Runtime.ModuleScope) <IL 0x0006b, 0x00210>
脚本和UnityEngine.dll是在同一个文件夹里的。这个是脚本。
import clr
clr.LoadAssemblyFromFile("UnityEngine.dll")
import UnityEngine
from UnityEngine import *
Debug.Log("Hello World from Python!")
原问题:IronPython in Unity3D
PHP中文网2017-04-17 11:59:30
Answer from @Storm Kiernan:
Script from Unity:
PythonEngine engine = new PythonEngine();
engine.LoadAssembly(Assembly.GetAssembly(typeof(GameObject)));
engine.ExecuteFile("apple.py");
In the python script (my pple.py and game.exe are placed in the same folder):
import UnityEngine
from UnityEngine import *
Debug.Log("Hello From IronPython!")
I should point out that the reason I got the error is that the runtime version was specified as 4.0, not 3.5 or lower.
If you need to access scripts from IronPython, you can also load the assembly via:
engine.LoadAssembly(Assembly.GetAssembly(typeof(MyPlayerScriptOrSomething)));
Then use it in the script:
import MyPlayerScriptOrSomething
Note that you don't need to load the assembly for every script, you only need to get the assembly once.
IronPython DLL should be placed in the Plugins folder under Assets, this is my setting.
> Assets
> > Plugins
> > > IronMath.dll
> > > IronPython.dll
> > > Microsoft.Scripting.dll
> > > Microsoft.Scripting.Core.dll
Scripts can be placed anywhere that any program can access. For example, if you want to put apple.py" directly in C:, you can do it through the following methods:
engine.ExecuteFile(@"c:\apple.py");
The version I am using now is: