unity3D 命名管道 进程通信

14 篇文章 1 订阅
由于项目需求, 需要和另外一个程序之间通信。 在筛选了 进程间通信方案之后 选择 使用 命名管道的方法,操作简单容易实现:

具体实现如下 Unity3d 端: 在这里 unity 充当 客户端 负责发送消息

这里需要注意下,设置Unity3D:




using UnityEngine;
using System;
using System.IO;
using System.IO.Pipes;

 
using System.Security.Principal;

public class ProcessCommunication : MonoBehaviour {

    // Use this for initialization
    public string content="";
    public int count;

    void OnGUI()
    {
        content = GUILayout.TextArea(content,GUILayout.Width(200));
        if (GUILayout.Button("SendData"))
        {
            SendData( content+"    "+(++count) );
        }
    }
    private static void SendData(string str)
    {
        try
        {
            using (NamedPipeClientStream pipeClient =
          new NamedPipeClientStream("localhost", "clouddeskpipeTest", PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.None))
            {
                pipeClient.Connect();
                using (StreamWriter sw = new StreamWriter(pipeClient))
                {
                    sw.WriteLine(str);
                    sw.Flush();
                }
            }
        }
        catch (Exception ex)
        {
            Debug.Log(ex.Message);
        }

    }
}

服务端我们测试用的是 ConsoleApplication 具体代码如下:


using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplicationWindowsService_Server
{
    class Program
    {
        static void Main(string[] args)
        {
            WaitData();

        }


        private static void WaitData()
        {
            Console.WriteLine("pipeServer 初始化");
            while (true)
            {
                try
                {
                    NamedPipeServerStream pipeServer = new NamedPipeServerStream("clouddeskpipeTest", PipeDirection.InOut, 2);
                    pipeServer.WaitForConnection();
                    StreamReader sr = new StreamReader(pipeServer);
                    string con = sr.ReadLine();

                    Console.WriteLine("pipeServer 内容:" + con);

                    sr.Close();
                    Thread.Sleep(50);

                    Console.WriteLine("pipeServer 等待中:");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("pipeServer  异常:" + ex.Message);
                }

            }
        }
    }
}

测试过程中 发现 Unity3D在编辑状态下 会出现无响应状态, 发布之后没有这个问题 !


过几天我会写一个 unity3D 和 WindowsService   命名管道 进程通信的博客。 应为在WindowsService 中会有权限的问题

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

坎大哈

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

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

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

打赏作者

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

抵扣说明:

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

余额充值