请教Unity3D中的IronPython问题
我想用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
来自@Storm Kiernan 的回答:
从Unity的脚本:
PythonEngine engine = new PythonEngine();
engine.LoadAssembly(Assembly.GetAssembly(typeof(GameObject)));
engine.ExecuteFile("apple.py");
python脚本里的(我的pple.py和game.exe放在一个文件夹里的):
import UnityEngine
from UnityEngine import *
Debug.Log("Hello From IronPython!")
Edit #1
我要指出我出错的原因是运行时版本指定为4.0,而不是3.5或者更低。
Edit #2
如果你需要从IronPython 访问脚本,你也可以通过加载程序集:
engine.LoadAssembly(Assembly.GetAssembly(typeof(MyPlayerScriptOrSomething)));
然后用到脚本里:
import MyPlayerScriptOrSomething
注意你不需要为每个脚本都加载程序集,只需一次获得程序集。
Edit #3
IronPython DLL应该放在Assets下的 Plugins文件夹,这是我的设置。
> Assets
> > Plugins
> > > IronMath.dll
> > > IronPython.dll
> > > Microsoft.Scripting.dll
> > > Microsoft.Scripting.Core.dll
Edit #4
脚本可以放在任何程序访问的到的地方。例如,你想直接把apple.py"放在C:\,你可以通过以下方法来执行:
engine.ExecuteFile(@"c:\apple.py");
Edit #5
我现在用的版本是:
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd19896.html
相关文章 Recommend
- • python re模块中的 match()和group()疑问
- • 请教web.py服务器端接收到的上传文件名出现乱码
- • python语言中的pcap与dpkt在统计中的作用分别是什么
- • 请教python返回值变量对象的问题,有源码
- • 请教tornado框架内链接数组参数类型的问题
- • python语言里函数作用域unBoundError错误问题请教
- • 求大牛看下python源码中的__init__()作用是什么
- • Flask中的URL部分什么方法可以让它支持正则
- • MySQL小问题,fetchone怎么返回到dict中的方法?
- • 请教tornado站点cookie无效不能登录的问题
- • virtualenv中的包除了pip 、easy_install外,还有其它安
- • 能不能解释一下hadoop中的mapreduce
您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容
我要分享到:
必知PYTHON教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
必知PYTHON模块 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • Python程序员解决棘手问题的常用库
- • 求助关于restfull api接口几个问题
- • qiniu pythonsdk提示ImportError错误求解
- • 问一个关于Hadoop Python中读写文件统计分析
- • 求问str()同__str__原理上有什么不同,分别在
- • 大神帮忙看下20行的python代码,文件io和数
- • python 爬虫爬wiki 报错 [Errno 65] No route to
- • python续点上传问题None bad token...
- • python3环境下文本中超链接出错,要如何修
- • Python环境保存操作思路问题求助
图文精华 RECOMMEND
-
Python程序员解决棘手问题的常用库
-
求问str()同__str__原理上有什么不同
-
scrapy框架里面用link extractor怎么能
-
python {}.fromkeys创建字典append添加操
-
python3 类型Type str doesn't support th
-
python里面为什么系统的时区是东八
热点文章 HOT
- 学习Python有什么好的书籍推荐?
- Python匿名函数 Lambda表达式作用
- Python与Java、C、Ruby、PHP等编程语言有什么
- Python 正则中文网页字符串提取问题
- 如何为实时性应用存取经纬度?django my
- 想用python做个客户端,在二维码登录这个地
- 有让IDE可识别Python函数参数类型的方法吗
- Python字符串转换成列表正则疑问