在 Unity 中从一个程序向另一个程序发送消息 [英] Send message from one program to another in Unity

查看:98
本文介绍了在 Unity 中从一个程序向另一个程序发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在装有 Windows 操作系统的同一台计算机上运行了两个不同的可执行文件.它们都是在 Unity 中构建的.我想在不使用网络的情况下将消息从一个发送到另一个.

I have two different executable files running on same computer which has Windows OS. Both of them are build in Unity. I want to send message from one to other without using network.

Unity 中如何将消息从一个 exe 程序发送到另一个 exe 程序?

How do you send message from an exe program to another exe program in Unity?

这是否可以通过 集成 Mono/.net 功能 或其他方式实现?

Is this possible with integrated Mono/.net functionality or something else?

推荐答案

您可以使用命名管道,它使用共享内存与同一台机器上的另一个应用程序通信.

You can use Named Pipes which uses shared memory to communicate with another application on the-same machine.

转到 File --> Build Settings... 选择 PC、Mac &Linux Standalone 然后点击 Player Settings....现在,将 Api Compatibility Level 更改为 .NET 2.0.

Go to File --> Build Settings... select PC, Mac & Linux Standalone then click on Player Settings.... Now, change Api Compatibility Level to .NET 2.0.

关闭并重新打开 Visual Studio.现在,您可以导入 using System.IO.Pipes; 并能够使用 NamedPipeServerStreamNamedPipeClientStream.

Close and re-open Visual Studio. Now, you can import using System.IO.Pipes; and be able to use NamedPipeServerStream and NamedPipeClientStream.

下面是一个非常简化的服务器和客户端.您必须Thread 中执行此操作,并且还应该处理异常.

Below is a very simplified server and client. You must do that in a Thread and should also handle exception.

如果您不想使用 Thread,还有一个异步参数 (PipeOptions.Asynchronous) 使其成为非阻塞运算符.从那里开始变得复杂,你必须在 MS doc 上寻找一些例子.

If you don't want to use Thread, there is also asynchronous parameter (PipeOptions.Asynchronous) that makes it a non blocking operator. It gets complicated from there and you have to look for some examples for that on MS doc.

简单服务器:

//Create Server Instance
NamedPipeServerStream server = new NamedPipeServerStream("MyCOMApp", PipeDirection.InOut, 1);
//Wait for a client to connect
server.WaitForConnection();
//Created stream for reading and writing
StreamString serverStream = new StreamString(server);
//Send Message to Client
serverStream.WriteString("Hello From Server");
//Read from Client
string dataFromClient = serverStream.ReadString();
UnityEngine.Debug.Log("Received from Client: " + dataFromClient);
//Close Connection
server.Close();

简单客户端:

//Create Client Instance
NamedPipeClientStream client = new NamedPipeClientStream(".", "MyCOMApp",
               PipeDirection.InOut, PipeOptions.None,
               TokenImpersonationLevel.Impersonation);

//Connect to server
client.Connect();
//Created stream for reading and writing
StreamString clientStream = new StreamString(client);
//Read from Server
string dataFromServer = clientStream.ReadString();
UnityEngine.Debug.Log("Received from Server: " + dataFromServer);
//Send Message to Server
clientStream.WriteString("Bye from client");
//Close client
client.Close();

来自 StreamString 类">MS 文档:

The StreamString class from MS Doc:

public class StreamString
{
    private Stream ioStream;
    private UnicodeEncoding streamEncoding;

    public StreamString(Stream ioStream)
    {
        this.ioStream = ioStream;
        streamEncoding = new UnicodeEncoding();
    }

    public string ReadString()
    {
        int len = 0;

        len = ioStream.ReadByte() * 256;
        len += ioStream.ReadByte();
        byte[] inBuffer = new byte[len];
        ioStream.Read(inBuffer, 0, len);

        return streamEncoding.GetString(inBuffer);
    }

    public int WriteString(string outString)
    {
        byte[] outBuffer = streamEncoding.GetBytes(outString);
        int len = outBuffer.Length;
        if (len > UInt16.MaxValue)
        {
            len = (int)UInt16.MaxValue;
        }
        ioStream.WriteByte((byte)(len / 256));
        ioStream.WriteByte((byte)(len & 255));
        ioStream.Write(outBuffer, 0, len);
        ioStream.Flush();

        return outBuffer.Length + 2;
    }
}

这篇关于在 Unity 中从一个程序向另一个程序发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆