Unity端实现代码
using System;
using System.Net.Sockets;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using System.Net;
using System.IO;
using System.Globalization;
public class Server : MonoBehaviour {
//General Init
private List<ServerClient> clients;
private List<int> disconnectIndex;
public int Port;
public TcpListener server;
private bool serverStarted;
public GameObject sphere;
Camera MainCamera;
// Use this for initialization
void Start () {
clients = new List<ServerClient>();
disconnectIndex = new List<int>();
try {
server = new TcpListener(IPAddress.Any, Port);
server.Start();
Startlistening();
serverStarted = true;
Debug.Log("Server has started on port:" + Port.ToString());
}
catch (Exception e) {
Debug.Log("Socket Error " + e.Message);
}
InvokeRepeating("UpdateLoop", 0f, 0.003f);
}
public void UpdateLoop()
{
if(!serverStarted)
return;
if(clients.Count == 0)
return;
for(int c = 0; c < clients.Count; c++ ) {
//Check if clients are connected
if(!isConnected(clients[c].tcp)) {
clients[c].tcp.Close();
disconnectIndex.Add(c);
Debug.Log(clients[c].clientName + " has disconnected from the server");
continue;
}
// Check for data from client
else {
&n

该文章描述了一个使用Unity建立的TCP服务器,用于接收来自Matlab的命令并执行相应操作,如移动相机和显示/隐藏游戏对象。Matlab端通过TCP连接发送指令,Unity端接收并解析这些指令,包括读取图像数据。文章展示了Unity中的代码实现,包括连接管理、数据接收和处理,以及Matlab端的图像读取和通信函数。

908

被折叠的 条评论
为什么被折叠?



