Python to C# script communication
So I have a C# script that communicates with a python script. The python script can only run at 15 cycles per second. This causes a bottleneck for me because the function I'm using to communicate with the python script is being called in Update(or so I'm assuming). In turn the game runs at 15fps. I'm trying to figure out how to call this function separate from update.
I have tried using co routines and invoke repeating, but both of them yield the same results. I'm assuming that I need to do something with threading, but I'm not quite sure where to go with it. Below is my current solution that doesn't run quite fast enough. Thanks for any help or pointers in the right direction.
public IEnumerator ConnectAsClient ()
{
while (true) {
TcpClient client = new TcpClient ();
client.Connect (IPAddress.Parse ("192.168.3.93"), 5004);
NetworkStream stream = client.GetStream ();
string s = "hello from client";
string t = "second thing to send";
byte[] message = Encoding.ASCII.GetBytes (s);
String responseData = String.Empty;
byte[] data = new Byte[256];
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read (data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString (data, 0, bytes);
// print ("Received: {0}" + responseData);
recievedData = responseData;
stream.Write (message, 0, message.Length);
message = Encoding.ASCII.GetBytes (t);
stream.Write (message, 0, message.Length);
stream.Close ();
client.Close ();
yield return null;
}
//StartCoroutine (ConnectAsClient);
// print ("closed\n");
}
void Start ()
{
StartCoroutine ("ConnectAsClient");
}
Answer by off99555 · Nov 16, 2018 at 10:31 AM
I know this thread is quite old but for anyone looking for a solution to communicate python with unity, please take a look at my repository dedicated for it: https://github.com/off99555/Unity3D-Python-Communication
Answer by Siliconifier · Dec 29, 2020 at 08:40 PM
This might be useful to others in the future. Have a look at this repo.