Home > other >  Paid for post
Paid for post

Time:09-15

When doing a task recently encountered such a problem, I wrote a script in c # socket server program that was written, opened a new thread has been able to accept and deal with the client's data and displayed in the console, after processing the information into a set of euler Angle or a group of quaternion, but I want to convert this set of real-time data assigned to the scene of a Cube, the error,
Error message display get_transform can only call in the main thread, how should do, please check it from the website is to use the Loom to interact, but this class is to write in a script or a script or other,,,, new people, paid for help
 
using UnityEngine;
using System.Collections;

Using System.Net;
Using System.Net.Sockets;
Using System. The Text;
Using System. The Threading;
Using System;
///& lt; summary>
///scoket server listening on port script
///& lt;/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;
Yy oula2=(float);
Zz oula2=(float);
}
}


Public GameObject Cube;


Public Thread thStartServer;//define start socket threads
//public string o;


Void the Start ()
{
The Debug Log (" 111 ");
ThStartServer=new Thread (StartServer);
ThStartServer. Start ();//start the thread
}

Void the Update ()
{

}

Public void StartServer ()
{
Const int bufferSize=8792;//the cache size, 8192 bytes
IPAddress IP=IPAddress. Parse (" 192.168.1.101 ");

TcpListener tlistener=new TcpListener (IP, 8080);
Tlistener. Start ();
The Debug Log (" Socket server monitoring start... ");

TcpClient remoteClient=tlistener. AcceptTcpClient ();//receive the connected client, blocking methods
The Debug Log (" the client is connected. Local: "+ remoteClient. Client. LocalEndPoint +" & lt; - Client: "+ remoteClient. Client. RemoteEndPoint);
NetworkStream streamToClient=remoteClient. GetStream ();//to get from the client flow
Do
{
Try//directly off the client, the server will throw an exception
{
//receive the client sends data section
Byte [] buffer=new byte [bufferSize];//define a cache buffer array
Int byteRead=streamToClient. Read (buffer, 0, bufferSize);//the data into the cache (a friend said the read () is blocking methods, not found in the test program block)
If (byteRead==0)//connection is broken, or call on the TCPClient Close () method, or on the flow invoke the Dispose () method,
{
The Debug Log (" the client connection is broken... ");
break;
}


. String MSG=Encoding UTF8. Get string (buffer, 0, byteRead);//converting from a binary string corresponding to the client will have from a string is converted to binary method
If (MSG [0]==0 | | MSG. Equals (null) | | MSG. Equals (";" ) | | MSG. Length & lt; 50)
{
return;
}
The else
{
String [] s=MSG. Split ('; ');//will be subject to the data into an array of strings
Int stringlength=s.L ength;
for (int i=0; i {
If (s [I] Length & gt; 54)//get the complete four element
{
If (s [0]. [I] Equals (' N ') & amp; & S [I] [1]. The Equals (' 0 '))
{
//if (t [0]. [I] Equals (' N ') & amp; & T [I] [1]. The Equals (' 0 '))
//{




//string t=o as string;
String [] seven=s [I] the 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 * * y (x + y * w), (1-2 * * y + z x y z));//roll
Double Pitch=Math. Asin (2 * (x - y * * z w));//pitching Angle
Double Yaw=Math. Atan2 (2 * * w * z (x + y), (1-2 * * * z + z w w));//partial line Angle
//the Debug Log (" receiving data: "+ STR +". The data length: [" + byteRead + "byte]");
Oulajiao Oulajiao=new Oulajiao ();
Oulajiao. Zhuanhuan (Roll * 180/Math. PI, Pitch * 180/Math in PI, Yaw * 180/Math. PI);
//oula11=Roll * 180/Math. PI.
//oulajiao oula2=Pitch * 180/Math. PI.
//oulajiao oula3=Yaw * 180/Math. PI.
.//the Debug Log (" euler Angle is: "+" "+ oulajiao oula1 +" "+ oulajiao oula2 +" "+ oulajiao oula3);


Cube. The transform. The Rotate (new Vector3 (oulajiao. Oula1, oulajiao. Oula2, oulajiao oula3), Space. The Self).

The Debug Log (" quaternion is;" + x + "" + y +" "+" "+ w + z);

.//the Debug Log (" euler Angle is: "+ Roll * 180/Math. PI + Pitch * 180/Math. The PI + Yaw * 180/Math. PI);

//Thread t=new Thread (Speedline);
//t.I sBackground=true;
//t.S tart ();
//}
}
The else
{
continue;
}
}
}
}
//the Debug Log (" receiving data: "+ STR +". The data length: [" + byteRead + "byte]");
}
The catch (Exception ex)
{
The Debug Log (" client exception: "+ ex. Message);
break;
}
}
While (true);
}

Void OnApplicationQuit ()
{
ThStartServer. Abort ();//at the end of the program to kill the thread, finished must remember his brush, by testing system will not give you wipe, wipe the second launch unity will no response
}


}

CodePudding user response:

The wrong report mentioned you, only operate in the main thread,,,,,,,, this is the server sends data to the customer segment, is commonly used in the development of new key a Queue, used to store the server sends the data to create a new script to monitor the Queue, if there is data in lateUpdate processing, the reason is that unity does not support in the unity of the data points in the thread operation (position, rotation, scale), as long as it is unity does not support before the 2018 version, 2018 heard that support multithreading, but I never used

CodePudding user response:

Is to use Loom type to solve it, and you create a gameobject hanging Loom type on the gameobject and set the gameobject for
DontDestroyOnLoad (gameObject); Can,,
  • Related