调用python:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//注意 一定要引用下面这个命名空间
using System.Diagnostics;
public class Call_python : MonoBehaviour
{
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
string[] arr = new string[2];
arr[0] = "10";
arr[1] = "20";
RunPythonScript(arr);
}
}
private static void RunPythonScript(string[] argvs)
{
Process p = new Process();
string path = @"D:\******\unity.py";
foreach (string temp in argvs)
{
path += " " + temp;
}
#C:\Python39\python.exe python的解释器地址
p.StartInfo.FileName = @"C:\Python39\python.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = path;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.BeginOutputReadLine();
p.OutputDataReceived += new DataReceivedEventHandler(Get_data);
p.WaitForExit();
}
private static void Get_data(object sender, DataReceivedEventArgs eventArgs)
{
if (!string.IsNullOrEmpty(eventArgs.Data))
{
print(eventArgs.Data.ToString());
}
}
}
调用C++ dll库
将vs x64环境下生成的库都放到Plugins文件夹下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class Call_Cplus : MonoBehaviour
{
//[DllImport("库的名字")]
[DllImport("Add")]
//定义静态方法使用
private static extern int Add_2(int num1,int num2);
void Start()
{
int count = Add_2(1, 2);
print("Count = " + count.ToString());
}
void Update()
{
}
}