本人呢,也是小白一枚,只是最近在研究python与unity的通信。这个其实就是建立python与unity的联系,利用python的深度学习方面的能力,并把得到的数据传输给unity,利用unity来做一个可视化的操作,而且还可以返回数据到python,应用场景还是很多的。
unity端:
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
using System.Threading;
public class CSharpForGIT : MonoBehaviour
{
Thread mThread;
public string connectionIP = "127.0.0.1";
public int connectionPort = 25001;
IPAddress localAdd;
TcpListener listener;
TcpClient client;
Vector3 receivedPos = Vector3.zero;
bool running;
private void Update()
{
transform.position = receivedPos; //assigning receivedPos in SendAndReceiveData()
}
private void Start()
{
ThreadStart ts = new ThreadStart(GetInfo);
mThread = new Thread(ts);
mThread.Start();
}
void GetInfo()
{
localAdd = IPAddress.Parse(connectionIP);
listener = new TcpListener(IPAddress.Any, connectionPort);
listener.Start();
client = listener.AcceptTcpClient();
running = true;
while (running)
{
SendAndReceiveData();
}
listener.Stop();
}
void SendAndReceiveData()
{
NetworkStream nwStream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];
//---receiving Data from the Host----
int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize); //Getting data in Bytes from Python
string dataReceived = Encoding.UTF8.GetString(buffer, 0, bytesRead); //Converting byte data to string
if (dataReceived != null)
{
//---Using received data---
receivedPos = StringToVector3(dataReceived); //<-- assigning receivedPos value from Python
print("received pos data, and moved the Cube!");
//---Sending Data to Host----
byte[] myWriteBuffer = Encoding.ASCII.GetBytes("Hey I got your message Python! Do You see this massage?"); //Converting string to byte data
nwStream.Write(myWriteBuffer, 0, myWriteBuffer.Length); //Sending the data in Bytes to Python
}
}
public static Vector3 StringToVector3(string sVector)
{
// Remove the parentheses
if (sVector.StartsWith("(") && sVector.EndsWith(")"))
{
sVector = sVector.Substring(1, sVector.Length - 2);
}
// split the items
string[] sArray = sVector.Split(',');
// store as a Vector3
Vector3 result = new Vector3(
float.Parse(sArray[0]),
float.Parse(sArray[1]),
float.Parse(sArray[2]));
return result;
}
}
python端:
import socket import time host, port = "127.0.0.1", 25001 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, port)) startPos = [0, 0, 0] #Vector3 x = 0, y = 0, z = 0 while True: time.sleep(0.5) #sleep 0.5 sec startPos[0] +=1 #increase x by one posString = ','.join(map(str, startPos)) #Converting Vector3 to a string, example "0,0,0" print(posString) sock.sendall(posString.encode("UTF-8")) #Converting string to Byte, and sending it to C# receivedData = sock.recv(1024).decode("UTF-8") #receiveing data in Byte fron C#, and converting it to String print(receivedData)
先到unity创建一个cube,在asset里面创建一个C#脚本,然后把unity端的代码复制过去,(c#脚本的名字要和代码中的public class后面的CSharpForGIT保持一致)结束后可以直接拖动脚本挂载到cube上
然后运行unity,点击上方的开始按钮,在运行python端,可以发现完成通讯,cube在一点一移动。
经过上述的操作后,不得不赞叹python的灵活易用。