简体   繁体   English

将Python输出提供给Unity3D

[英]Feed Python output to Unity3D

I have some code that is running in the python console reading out text lines. 我有一些代码正在python控制台中运行,以读出文本行。

Is it possible to feed the output of the python console to my Unity3D game script so that it can trigger some actions in my game? 是否可以将python控制台的输出提供给我的Unity3D游戏脚本,以便它可以触发游戏中的某些动作?

in other words: python console running in the background outputs commands that need to feed to my Unity game. 换句话说:在后台运行的python控制台输出需要提要给我的Unity游戏的命令。

An efficient solution would be to use TCP sockets in both the Python script and the Unity-side script. 一个有效的解决方案是在Python脚本和Unity端脚本中都使用TCP套接字。 Since the Python-side is serving the data, then designate it as the server socket and make the socket on the Unity-side the client. 由于Python 端正在提供数据,因此将其指定为服务器套接字,并使该套接字在Unity端成为客户端。 This solution will be much faster than writing and reading to a shared file. 该解决方案比写入和读取共享文件要快得多。

If you're on windows you can use Process, spin up python and read standard out and collect the output. 如果您在Windows上,则可以使用Process,启动python并读取标准输出并收集输出。 Out side of that, if the process isn't started in Unity, you best bet is probably to monitor a file in a particular directory for changes and read in those changes which would have been the output from your python console. 另外,如果该过程不是在Unity中启动的,那么最好的办法可能是监视特定目录中的文件是否有更改,并读入这些更改,这些更改本来是python控制台的输出。

Here is an example of UnityScript creating and executing a process. 是UnityScript创建和执行过程的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM