最近在做一个任务的时候遇到这样一个问题,我用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会无响应
}
}