Question by
Dipcar · Sep 11, 2018 at 01:09 PM ·
multiplayerscene-loadingthreadinglistenertcpclient
Is there a way to somehow load a scene when client receive a message in non-main thread ?
Hi, I need help with my unity project. I need to load a new scene, when client receive a message from client. This unity client listener has own thread for listening, so this loading can not be done inside main thread. I know that unity is not thread safe.
So, what am i asking is if is it possible to somehow load a new scene when TcpClient receive a message inside a non-main thread.
Client listener method
private void ListenForData()
{
try
{
socketConnection = new TcpClient("localhost", 2507);
Byte[] bytes = new Byte[1024];
while (true)
{
using (NetworkStream stream = socketConnection.GetStream())
{
int length;
while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
{
var incommingData = new byte[length];
Array.Copy(bytes, 0, incommingData, 0, length);
string serverMessage = Encoding.ASCII.GetString(incommingData);
Debug.Log("Server message received as: " + serverMessage);
if (serverMessage.Equals("MoveToLobby"))
MenuManager.goToScene("lobby");
}
}
}
}
catch (SocketException socketException)
{
Debug.Log("Socket exception: " + socketException);
}
}
MenuManager#goToScene method
public static void goToScene(string scene) {
switch (scene) {
case "login": SceneManager.LoadScene("LoginScene", LoadSceneMode.Single); break;
case "lobby": SceneManager.LoadScene("LobbyScene", LoadSceneMode.Single); break;
}
}
Comment
Your answer
Follow this Question
Related Questions
Can I run a lobby and multiple instances of my main game scene for multiplayer? 1 Answer
Problems with the TCP server 0 Answers
Targetting VR and flatscreen 0 Answers
Many listeners in multiplayer 0 Answers
Scene based multiplayer game 0 Answers