在Unity中将消息从一个程序发送到另一个程序 [英] Send message from one program to another in 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.
如何将消息从exe程序发送到Unity中的另一个exe程序?
How do you send message from an exe program to another exe program in Unity?
使用integrated Mono/.net functionality
或其他方式可能吗?
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.
转到文件-> 构建设置... 选择 PC,Mac& Linux独立版,然后单击播放器设置... .现在,将 Api兼容级别更改为 .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;
并可以使用NamedPipeServerStream
和NamedPipeClientStream
.
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();
来自
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屋!