python跟csharp_unity-工具-csharp与python交互
title: unity-工具-csharp与python交互 categories: Unity3d tags: [unity, 编辑器, 扩展, python] date: 2020-03-23 15:35:51 comments: false mathjax: true toc: true
很多时候写工具都是使用 python 来写, unity 工具需要调用 python 脚本并获取到执行结果.
非弹窗式
可以从 Python 的 os 的 标准输出 (sys.stdout.write) 中返回给 cshap
python 的 print 其实就是调用 sys.stdout.write + 换行符
csharp 执行 命令行 的方法 // 可以获取到 py 脚本 print 的值
public static string ProcCmd(string command, string argument) {
ProcessStartInfo psi = new ProcessStartInfo(command);
psi.Arguments = argument;
psi.CreateNoWindow = true;
psi.ErrorDialog = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
psi.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;
Process p = Process.Start(psi);
StringBuilder sb1 = new StringBuilder();
StreamReader reader = p.StandardOutput;
while (!reader.EndOfStream) {
string line = reader.ReadLine();
if (!line.StartsWith("---")) { // 过滤掉自定义的日志, 方便 py 调试
sb1.Append(line);
}
if (!reader.EndOfStream) {
sb1.Append("\n");
}
}
reader.Close();
StringBuilder sb2 = new StringBuilder();
StreamReader errorReader = p.StandardError;
while (!errorReader.EndOfStream) {
sb2.Append(errorReader.ReadLine()).Append("\n");
}
errorReader.Close();
p.WaitForExit();
p.Close();
if (sb2.Length > 0) {
throw new Exception(string.Format("--- Error, python error, msg:{0}", sb2.ToString()));
}
return sb1.ToString();
}
python 工具脚本 #!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
if __name__ == "__main__":
print("hello world, arg1: {}".format(sys.argv[1])) # 这个就能输出给 csharp
测试 csharp 执行 python 结果 string pyPath = Path.Combine(PackConfig.PyToolDir, "tool.py");
string res = EditorUtils.ProcCmd("python3", string.Format("{0} {1}", pyPath, "getProjBranch"));
Debug.LogFormat("--- py res: {0}", res);
弹窗式
单独一个命令行窗口, 标准输出不会返回给 csharp
csharp 执行 命令行 的方法 // 弹窗执行 py
public static void ProcCmdOnWin(string command, string argument) {
ProcessStartInfo psi = new ProcessStartInfo(command);
psi.Arguments = argument;
psi.CreateNoWindow = true;
psi.ErrorDialog = true;
psi.UseShellExecute = true;
psi.RedirectStandardOutput = false;
psi.RedirectStandardError = false;
psi.RedirectStandardInput = false;
Process p = Process.Start(psi);
p.WaitForExit();
p.Close();
}
数据结构的传递
一般使用 json 作为数据结构的格式传递给 Python, 但是命令不能正常传递 json 字符串 给 Python 获取, 所以得用曲线救国的方式 base64 encode 一下给 py, 然后在 py 在 decode 解出正常的 json
csharp public static string Base64Encode(string plainText) {
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static string Base64Decode(string base64EncodedData) {
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
python def base64Encode(plainText: str) -> str:
return base64.encodebytes(plainText.encode()).decode()
def base64Decode(encodedData: str) -> str:
return base64.decodebytes(encodedData.encode()).decode()
python跟csharp_unity-工具-csharp与python交互相关推荐
- Python数学计算工具5、Python求最最小公倍数
最小公倍数百度解析: 两个或多个整数公有的倍数叫做它们的公倍数,其中除0以外最小的一个公倍数就叫做这几个整数的最小公倍数.整数a,b的最小公倍数记为[a,b],同样的,a,b,c的最小公倍数记为[a, ...
- python的开发工具有哪些,Python开发软件毕业设计
python中用到哪些软件 一.Python代码编辑器1.sublime Textsublime Text是一款非常流行的代码编辑器,支持Python代码编辑,同时兼容所有平台,并且丰富的插件扩展了语 ...
- python 内存分析工具_[转] python运行时内存分析工具meliae
meliae是一个python进程内存占用监控.分析工具,它的安装需要依赖pyrex包. 一.安装: 安装python内存分析工具 sudo pip install cython sudo pip i ...
- python的编程工具spider_7款Python开发神器,拿走不谢
在人工智能开启的时代,Python作为人工智能的首选语言,前景可以预见.因此学习Python的人越来越多.今天,快快小编准备给大家介绍7款Python开发神器,欢迎收藏转发! 1.Micro Pyth ...
- 基于python的性能测试工具_基于 Python 的性能测试工具 locust 与 LR 的简单对比[转发]...
背景 最近自己开发了一个小的接口,功能测完了,突然想测下性能,原来做性能测试,我一直用的是HP的LoadRunner,前一段时间正好看过locust,想想就用这个来测测性能吧. 由于对LR比较熟,正好 ...
- Python数学计算工具3、Python 斐波那契数列-前500项列表
百度解析: 斐波那契数列(Fibonacci sequence),又称黄金分割数列,因数学家莱昂纳多·斐波那契(Leonardo Fibonacci)以兔子繁殖为例子而引入,故又称为"兔子数 ...
- python pdf处理工具_用Python处理pdf文档
介绍 译者翻译了很多Python强大的包,其中,一以贯之的思想是:面向对象.我用下面的翻译来举一个例子,比如:从PyPDF2包中导入PdfFileReader包.PdfFileReader是此包的一个 ...
- 如何用python写小工具_用python写一个录音小工具
Python的paramiko,wxPython库的应用 Sound eXchange 命令行 需求 最近在给一个做语音识别的项目做QA工作.众所周知,此类人工智能方面的项目都需要一些数据收集的工作. ...
- python代码扫描工具_用Python编写一个高效的端口扫描器的方法
PyPortScanner python多线程端口扫描器. 输出示例: Github 背景 有时候,在进行网络相关的研究的时候,我们需要执行一些有目的的参数测量.而端口扫描就是其中比较普遍也比较重要的 ...
最新文章
- ITK:获取类型的基本信息
- Redis分布式锁问题
- Python绘制柱状图显示中国式过马路方式
- paip.提升用户体验=----c++ qt 字体切换功能缺少的总结..
- AD09 PCB制作开异性窗口
- Go 语言圣经-习题汇总(Go 程序设计语言/The Go Programming Language)
- Wincc常用C脚本
- opencv基于MultiTracker的多目标跟踪
- 好课堂Scratch编程08 趣学篇(四)密林深处的危险气息
- 虚拟机ruc_sched Self-detected stall on cpu{4}(t=60001)
- 计算机十六进制ABCD,16进制计算(十六进制计算器在线)
- python 执行linux rm命令_Linux rm命令:删除文件
- maya2018模型传递点序
- jmeter分布式操作之远程启动功能
- NATO(北大西洋公约组织)采用ADOBE FLEX作为作战支持系统
- js屏蔽键盘esc键
- Skywalking概述
- 泽塔云荣膺“中国高科技高成长50强”,成唯一上榜超融合企业
- WIFI基础入门--802.11--成帧细节(管理帧)--5
- 【CodeForces 1260E --- Tournament】