开发教程
1.unity端
//开启程序
private void StartCmdArg()
{
process = new Process();
process.StartInfo.FileName = @"C:\Users\ytrw\Desktop\ConsoleApp1\bin\Debug/ConsoleApp1.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.Arguments = "mt" + " " + "cvt";
process.StartInfo.CreateNoWindow = true;
//process.StartInfo.StandardOutputEncoding = Encoding.Default;
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
//byte[] by = Encoding.UTF8.GetBytes(e.Data);
//string s = Encoding.UTF8.GetString(by);
Debug.Log(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
}
//这一句是发送消息
process.StandardInput.WriteLine(1);
2.C#(控制台)
static void Main(string[] args)
{
foreach (string arg in args)
{
Console.WriteLine(arg);
}
for (; ; )
{
Console.WriteLine("Please enter the first number");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter calculation symbol :(+ - * / %)");
String b = Console.ReadLine();
Console.WriteLine("Please enter the second number");
int c = int.Parse(Console.ReadLine());
int d = 0;
if (b == "+")
{
d = a + c;
}
else if (b == "-")
{
d = a - c;
}
else if (b == "*")
{
d = a * c;
}
else if (b == "/")
{
d = a / c;
}
else if (b == "%")
{
d = a % c;
}
Console.WriteLine("The results" + d);
}
}
}
}
总结
文章介绍了Unity和C#程序通信,如有疑问请留言,也可下载 demo。