unity python_Unity引擎内嵌python
Unity腳本
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
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);
}
}
}
python腳本
# -*- coding: utf-8 -*-
PoemName = "杜甫《江畔獨步尋花·其六》"
PoemTest = """
黃四娘家花滿蹊,千朵萬朵壓枝低。
留連戲蝶時時舞,自在嬌鶯恰恰啼。
"""
print(PoemName)
print(PoemTest)
MottoEn = "A warm smile is the universal language of kindness."
MottoCh = "溫暖的微笑是表示善意的通用語言。"
print(MottoEn)
print(MottoCh)
總結
以上是生活随笔為你收集整理的unity python_Unity引擎内嵌python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 522. 最长特殊序列
- 下一篇: python--从入门到实践--chap