unity调用python代码,捕获控制台输出到ui面板上


前言

unity调用python代码后,想把python生成的数据内容直接传到unity内的ui面板上,但不是通过socket通信传递数据。这里直接捕获python内print到控制台的内容。


1.python部分

python代码部分直接print输出想要传递的数据

2.unity部分

传递的数据通过文本的方式被unity接收,通过字符串操作获取想要的数据

public class gtPrediction : MonoBehaviour
{
    private ProcessStartInfo startInfo;
    private Process process;
    private static StringBuilder output = new StringBuilder();
	 void Start()
    {
        Kill_All_Python_Process();
        // 创建UDP通信的Client
        udpClient = new UdpClient();
        // 设置IP地址与端口号
        remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 31412);
        localEP = new IPEndPoint(IPAddress.Any, 0);
        //string pythonPath = Application.dataPath + "/TDSTF/TDSTF-main/main.py";
        string pythonPath = "D:/main.py";
        // 创建Process
        process = new Process();
        // 创建ProcessStartInfo对象
        startInfo = new ProcessStartInfo();
        process.StartInfo = startInfo;
        // 设定执行cmd
        //startInfo.FileName = @"E:/DTprediction/Library/PythonInstall/python.exe";
        startInfo.FileName = Application.dataPath + "/StreamingAssets/PythonInstall/python.exe";
        // 输入参数是上一步的command字符串
        startInfo.Arguments = pythonPath;
        // 因为嵌入Unity中后台使用,所以设置不显示窗口
        startInfo.CreateNoWindow = true;
        // 这里需要设定为false
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardInput = true;     //打开流输入
        // 设置重定向这个进程的标准输出流,用于直接被Unity C#捕获,从而实现 Python -> Unity 的通信
        startInfo.RedirectStandardOutput = true;
        // 设置重定向这个进程的标准报错流,用于在Unity的C#中进行Debug Python里的bug
        startInfo.RedirectStandardError = true;
        //process.StandardInput.AutoFlush = true;//每次调用 Write()之后,将其缓冲区刷新到基础流
        process.OutputDataReceived += new DataReceivedEventHandler(OnOutputDataReceived);
        process.ErrorDataReceived += new DataReceivedEventHandler(OnErrorDataReceived);

        //启动脚本Process,并且激活逐行读取输出与报错
        process.Start();
        process.BeginErrorReadLine();
        process.BeginOutputReadLine();
    }
    private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.Data))
        {
            output.Append(e.Data);
            UnityEngine.Debug.Log(output.ToString());
        }
    }
    private void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.Data))
        {
            // 调试语句
            UnityEngine.Debug.LogError("Received error output: " + e.Data);
        }
    }

}

总结

output.ToString()就是想要的数据了,直接显示在ui即可。

  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fortune56

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值