有偿求助帖

xue small sir 2019-03-13 10:54:31
最近在做一个任务的时候遇到这样一个问题,我用c#脚本写了一个socket服务端程序,是新开了一个线程写的,已经能够接受并且处理客户端的数据并显示在控制台中。处理后的信息为一组欧拉角或者一组四元数,但是我想把这组实时变换的数据赋值给场景中的一个Cube时,出错,
报错信息显示get_transform只能在主线程中调用,请问应怎样办,从网上查到是使用Loom类进行交互,可是这个类是写在脚本里还是另起一个脚本或者。。。。新人求教,有偿求助
using UnityEngine;
using System.Collections;

using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System;
/// <summary>
/// scoket服务器监听端口脚本
/// </summary>
public class SocketServer : MonoBehaviour
{

public class Oulajiao
{
public float oula1;
public float oula2;
public float oula3;

public void zhuanhuan(double xx,double yy,double zz)
{
oula1 = (float)xx;
oula2 = (float)yy;
oula2 = (float)zz;
}
}


public GameObject Cube;


public Thread thStartServer;//定义启动socket的线程
//public string o;


void Start()
{
Debug.Log("111");
thStartServer = new Thread(StartServer);
thStartServer.Start();//启动该线程
}

void Update()
{

}

public void StartServer()
{
const int bufferSize = 8792;//缓存大小,8192字节
IPAddress ip = IPAddress.Parse("192.168.1.101");

TcpListener tlistener = new TcpListener(ip, 8080);
tlistener.Start();
Debug.Log("Socket服务器监听启动......");

TcpClient remoteClient = tlistener.AcceptTcpClient();//接收已连接的客户端,阻塞方法
Debug.Log("客户端已连接!local:" + remoteClient.Client.LocalEndPoint + "<---Client:" + remoteClient.Client.RemoteEndPoint);
NetworkStream streamToClient = remoteClient.GetStream();//获得来自客户端的流
do
{
try //直接关掉客户端,服务器端会抛出异常
{
//接收客户端发送的数据部分
byte[] buffer = new byte[bufferSize];//定义一个缓存buffer数组
int byteRead = streamToClient.Read(buffer, 0, bufferSize);//将数据搞入缓存中(有朋友说read()是阻塞方法,测试中未发现程序阻塞)
if (byteRead == 0)//连接断开,或者在TCPClient上调用了Close()方法,或者在流上调用了Dispose()方法。
{
Debug.Log("客户端连接断开......");
break;
}


string msg = Encoding.UTF8 .GetString(buffer, 0, byteRead);//从二进制转换为字符串对应的客户端会有从字符串转换为二进制的方法
if (msg[0] == 0 || msg.Equals(null) || msg.Equals(";") || msg.Length < 50)
{
return;
}
else
{
string[] s = msg.Split(';'); //将受到的数据化为字符串数组
int stringlength = s.Length;
for (int i = 0; i < stringlength; i++)//
{
if (s[i].Length > 54) //获取完整的四元素
{
if (s[i][0].Equals('N') && s[i][1].Equals('0'))
{
// if (t[i][0].Equals('N') && t[i][1].Equals('0'))
// {




// string t = o as string;
string[] seven = s[i].Split(' ');
double x = double.Parse(seven[1]);
double y = double.Parse(seven[2]);
double z = double.Parse(seven[3]);
double w = double.Parse(seven[4]);
double a = double.Parse(seven[5]);
double b = double.Parse(seven[6]);
double c = double.Parse(seven[7]);
double Roll = Math.Atan2(2 * (x * y + y * w), (1 - 2 * y * y + z * z));//横滚角
double Pitch = Math.Asin(2 * (x * z - y * w));//俯仰角
double Yaw = Math.Atan2(2 * (x * z + y * w), (1 - 2 * z * z + w * w));//偏行角
// Debug.Log("接收数据:" + str + ".数据长度:[" + byteRead + "byte]");
Oulajiao oulajiao = new Oulajiao();
oulajiao.zhuanhuan(Roll * 180 / Math.PI, Pitch * 180 / Math.PI, Yaw * 180 / Math.PI);
//oula11 = Roll * 180 / Math.PI;
// oulajiao.oula2 = Pitch * 180 / Math.PI;
//oulajiao. oula3 = Yaw * 180 / Math.PI;
// Debug.Log("欧拉角为:" + " " + oulajiao.oula1 + " " + oulajiao.oula2 + " " + oulajiao.oula3);


Cube.transform.Rotate(new Vector3(oulajiao.oula1, oulajiao.oula2, oulajiao.oula3), Space.Self);

Debug.Log("四元数为;"+ x+" " +y+" "+z+" "+ w);

// Debug.Log("欧拉角为:" + Roll * 180 / Math.PI + Pitch * 180 / Math.PI + Yaw * 180 / Math.PI);

//Thread t = new Thread(Speedline);
//t.IsBackground = true;
//t.Start();
// }
}
else
{
continue;
}
}
}
}
// Debug.Log("接收数据:" + str + ".数据长度:[" + byteRead + "byte]");
}
catch (Exception ex)
{
Debug.Log("客户端异常:" + ex.Message);
break;
}
}
while (true);
}

void OnApplicationQuit()
{
thStartServer.Abort();//在程序结束时杀掉线程,完事一定要记得自己擦,系统不会给你擦,经测试不擦第二次启动unity会无响应
}


}
...全文
85 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
nfsinitialD 2019-07-03
  • 打赏
  • 举报
回复
是用Loom类解决就行。。 你创建一个gameobject 把loom类挂到该gameobject上 并设置该gameobject为 DontDestroyOnLoad(gameObject);即可。。
  • 打赏
  • 举报
回复
错报告述你了,只能在主线程中操作。。。。。。这个就是服务器发送数据给客户段,在开发中常用的是新键一个Queue,用来存放服务器发送来的数据,新建一个脚本监听Queue,如果有数据在lateUpdate中处理。这个原因就是unity不支持在分线程中操作unity的数据(位置,旋转、缩放),只要是unity都不支持2018版以前的。2018听说支持多线程,但是我自己没用过

2,537

社区成员

发帖
与我相关
我的任务
社区描述
Unity3D相关内容讨论专区
游戏unity 技术论坛(原bbs)
社区管理员
  • Unity3D
  • 芝麻粒儿
  • 「已注销」
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

Unity3D社区公告:

  1. 社区致力于解决各种Unity3D相关的“疑难杂症”。
  2. 社区不允许发布与Unity3D或相关技术无关内容。
  3. 社区版主邀请各位一道为打造优秀社区不懈努力。

试试用AI创作助手写篇文章吧