翻译 - Unity3D 中的 IronPython
高洛峰
高洛峰 2017-04-17 11:57:30
0
1
483

我想用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

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
洪涛

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!")
Edit #1

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.

Edit #2

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.

Edit #3

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
Edit #4

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");
Edit #5

The version I am using now is:

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!