Unity直接调用Python脚本

216 篇文章 9 订阅
65 篇文章 2 订阅

Windows  and Ubuntu

Windows

Unity的工程直接调用 python脚本,然后输出相应的结果。

1 Unity 工程和脚本

新建一个工程,并添加脚本LoadPython.cs

using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Diagnostics;  //需要添加这个名词空间,调用DataReceivedEventArg 



public class LoadPython : MonoBehaviour
{
	string sArguments = @"UnityLoad.py";//这里是python的文件名字

    // Use this for initialization
    void Start()
    {

        RunPythonScript(sArguments, "-u");
    }

    // Update is called once per frame
    void Update()
    {
		RunPythonScript(sArguments, "-u");
    }

    public static void RunPythonScript(string sArgName, string args = "")
    {
        Process p = new Process();
		//python脚本的路径
		string path = @"F:\pythonBuffer\" + sArgName;
        string sArguments = path;


		//(注意:用的话需要换成自己的)没有配环境变量的话,可以像我这样写python.exe的绝对路径
		//(用的话需要换成自己的)。如果配了,直接写"python.exe"即可
	    p.StartInfo.FileName = @"python.exe";
		//p.StartInfo.FileName = @"C:\Program Files\Python35\python.exe";

		// sArguments为python脚本的路径   python值的传递路线strArr[]->teps->sigstr->sArguments 
		//在python中用sys.argv[ ]使用该参数
		p.StartInfo.UseShellExecute = false;
        p.StartInfo.Arguments = sArguments; 
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        p.Start();
        p.BeginOutputReadLine();
		p.OutputDataReceived += new DataReceivedEventHandler (Out_RecvData);
        Console.ReadLine();
        p.WaitForExit();
    }

	static void Out_RecvData(object sender, DataReceivedEventArgs e)
	{
		if (!string.IsNullOrEmpty(e.Data)) {
			UnityEngine.Debug.Log (e.Data);

		}
	}

}

2 新建python脚本 UnityLoad.py

# -*- coding: utf-8 -*-

PoemName = "杜甫《江畔独步寻花·其六》"
PoemTest = """
黄四娘家花满蹊,千朵万朵压枝低。
留连戏蝶时时舞,自在娇莺恰恰啼。
"""

print(PoemName)
print(PoemTest)

MottoEn = "A warm smile is the universal language of kindness."
MottoCh = "温暖的微笑是表示善意的通用语言。"

print(MottoEn)
print(MottoCh)

3  运行unity调用

 

Ubuntu

 

1 Unity 工程和脚本

新建一个工程,并添加脚本RunPython.cs

using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Diagnostics;  //需要添加这个名词空间,调用DataReceivedEventArg 
 
 
 
public class RunPython : MonoBehaviour
{
	string sArguments = @"UnityPython.py";//这里是python的文件名字
 
    // Use this for initialization
    void Start()
    {
 
        RunPythonScript(sArguments, "-u");
    }
 
    // Update is called once per frame
    void Update()
    {
		RunPythonScript(sArguments, "-u");
    }
 
    public static void RunPythonScript(string sArgName, string args = "")
    {
        Process p = new Process();
		//python脚本的路径
		//string path = @"F:\BUFFER\PycharmBuffer\" + sArgName; 
		string path = @"/home/michael/BUFFER/PycharmBuffer/" + sArgName;
        string sArguments = path;

        //文件目录的查询参考 https://blog.csdn.net/moonlightpeng/article/details/116195087?spm=1001.2014.3001.5501
 
     print("hello python");
		//(注意:用的话需要换成自己的)没有配环境变量的话,可以像我这样写python.exe的绝对路径
		//(用的话需要换成自己的)。如果配了,直接写"python.exe"即可
	    p.StartInfo.FileName = @"/home/michael/.conda/envs/PVNet/bin/python";
                 //注意Ubuntu系统pyhton后缀没有.exe
		//p.StartInfo.FileName = @"C:\Program Files\Python35\python.exe";
 
		// sArguments为python脚本的路径   python值的传递路线strArr[]->teps->sigstr->sArguments 
		//在python中用sys.argv[ ]使用该参数
		p.StartInfo.UseShellExecute = false;
        p.StartInfo.Arguments = sArguments; 
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        p.Start();
        p.BeginOutputReadLine();
		p.OutputDataReceived += new DataReceivedEventHandler (Out_RecvData);
        Console.ReadLine();
        p.WaitForExit();
    }
 
	static void Out_RecvData(object sender, DataReceivedEventArgs e)
	{
		if (!string.IsNullOrEmpty(e.Data)) {
			UnityEngine.Debug.Log (e.Data);
 
		}
	}
 
}

2 新建python脚本 UnityPython.py

# -*- coding: utf-8 -*-

PoemName = "杜甫《客至》"
PoemTest = """
舍南舍北皆春水,但见群鸥日日来。
花径不曾缘客扫,蓬门今始为君开。
盘飧市远无兼味,樽酒家贫只旧醅。
肯于邻翁相对饮,隔离呼取尽馀杯。
"""

print(PoemName)
print(PoemTest)

MottoEn = "If you can't fly then run, if you can't run then walk, if you can't walk then crawl" \
          "but whatever you do you have to keep moving forward."
MottoCh = "如果不能飞,那就跑;如果跑不动,那就走;实在走不动了,那就爬。无论做什么,你都要勇往直前。"

print(MottoEn)
print(MottoCh)

3  运行unity调用

 

注意事项:加载的python脚本名要写对,如果写错了竟然不会报错,仔细一点。

传参数通过unity到python参考

 

c#调用python的四种方法

 

 

 

  • 17
    点赞
  • 165
    收藏
    觉得还不错? 一键收藏
  • 30
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值