unity程序和控制台程序通信(C#)

45 篇文章 11 订阅
43 篇文章 1 订阅

unity程序和控制台程序通信

开发教程

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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

unity_YTWJJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值