当前位置: 首页 > news >正文

Python发送数据到Unity实现

Unity设置:

  • 打开Unity项目。
  • 创建一个空的GameObject,并附加一个新的脚本TCPReceiver
     
  • using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using UnityEngine;
    using System.Threading;public class MyListener : MonoBehaviour
    {Thread thread;public int connectionPort = 25001;TcpListener server;TcpClient client;bool running;void Start(){// Receive on a separate thread so Unity doesn't freeze waiting for dataThreadStart ts = new ThreadStart(GetData);thread = new Thread(ts);thread.Start();}void GetData(){// Create the serverserver = new TcpListener(IPAddress.Any, connectionPort);server.Start();// Create a client to get the data streamclient = server.AcceptTcpClient();// Start listeningrunning = true;while (running){Connection();}server.Stop();}void Connection(){// Read data from the network streamNetworkStream nwStream = client.GetStream();byte[] buffer = new byte[client.ReceiveBufferSize];int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);// Decode the bytes into a stringstring dataReceived = Encoding.UTF8.GetString(buffer, 0, bytesRead);// Make sure we're not getting an empty string//dataReceived.Trim();if (dataReceived != null && dataReceived != ""){// Convert the received string of data to the format we are usingposition = ParseData(dataReceived);nwStream.Write(buffer, 0, bytesRead);}}// Use-case specific function, need to re-write this to interpret whatever data is being sentpublic static Vector3 ParseData(string dataString){Debug.Log(dataString);// Remove the parenthesesif (dataString.StartsWith("(") && dataString.EndsWith(")")){dataString = dataString.Substring(1, dataString.Length - 2);}// Split the elements into an arraystring[] stringArray = dataString.Split(',');// Store as a Vector3Vector3 result = new Vector3(float.Parse(stringArray[0]),float.Parse(stringArray[1]),float.Parse(stringArray[2]));return result;}// Position is the data being received in this exampleVector3 position = Vector3.zero;void Update(){// Set this object's position in the scene according to the position receivedtransform.position = position;}
    }

    Python设置:

  • 如果尚未安装socket库,安装(pip install socket
  • 创建一个Python脚本send_data.py
     
    import sockethost, port = "127.0.0.1", 25001
    data = "1,2,3"# SOCK_STREAM means TCP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)try:# Connect to the server and send the datasock.connect((host, port))sock.sendall(data.encode("utf-8"))response = sock.recv(1024).decode("utf-8")print (response)finally:sock.close()
http://www.lryc.cn/news/267860.html

相关文章:

  • Unity 渲染顺序受哪些影响(相机depth、SortingLayer、Render Queue、透明)
  • 【论文笔记】Run, Don’t Walk: Chasing Higher FLOPS for Faster Neural Networks
  • python常用函数汇总
  • 阶段十-物业项目
  • 使用 Jekyll 构建你的网站 - 初入门
  • 【数据库】postgressql设置数据库执行超时时间
  • SQL语言之DDL
  • hive高级查询(2)
  • golang的jwt学习笔记
  • 第十五节TypeScript 接口
  • 【hadoop】解决浏览器不能访问Hadoop的50070、8088等端口?!
  • 14.bash shell中的for/while/until循环
  • RPC(6):RMI实现RPC
  • strlen和sizeof的初步理解
  • 纯CSS的华为充电动画,它来了
  • 在架构设计中,前后端分离有什么好处?
  • C语言中的结构体和联合体:异同及应用
  • 文件夹共享(普通共享和高级共享的区别)防火墙设置(包括了jdk安装和Tomcat)
  • ❀My排序算法学习之冒泡排序❀
  • 服务器数据恢复-raid6离线磁盘强制上线后分区打不开的数据恢复案例
  • Zookeeper在分布式命名服务中的实践
  • 说说 Spring Boot 实现接口幂等性有哪几种方案?
  • Dash中的callback的使用 多input 6
  • 平方矩阵()
  • git基本命令
  • GPU性能实时监测的实用工具
  • 概率论中的 50 个具有挑战性的问题 [第 6 部分]:Chuck-a-Luck
  • windows搭建MySQL主从补充说明
  • Python:GUI Tkinter
  • 制作一个可以离线安装的Visual Studio安装包