Search Unity

LoadSceneAsyncNameIndexInternal, Is there a way to somehow load a scene inside non-main thread ?

Discussion in 'Editor & General Support' started by Dipcar, Sep 10, 2018.

  1. Dipcar

    Dipcar

    Joined:
    Sep 10, 2018
    Posts:
    1
    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 listenig, 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 TpcClient receive a message inside a non-main thread.

    Client listener method
    Code (CSharp):
    1. private void ListenForData()
    2.     {
    3.         try
    4.         {
    5.             socketConnection = new TcpClient("localhost", 2507);
    6.             Byte[] bytes = new Byte[1024];
    7.             while (true)
    8.             {      
    9.                 using (NetworkStream stream = socketConnection.GetStream())
    10.                 {
    11.                     int length;              
    12.                     while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
    13.                     {
    14.                         var incommingData = new byte[length];
    15.                         Array.Copy(bytes, 0, incommingData, 0, length);                    
    16.                         string serverMessage = Encoding.ASCII.GetString(incommingData);
    17.                         Debug.Log("Server message received as: " + serverMessage);
    18.                         if (serverMessage.Equals("MoveToLobby"))
    19.                             MenuManager.goToScene("lobby");
    20.                     }
    21.                 }
    22.             }
    23.         }
    24.         catch (SocketException socketException)
    25.         {
    26.             Debug.Log("Socket exception: " + socketException);
    27.         }
    28.     }
    I know that this will not work...

    MenuManager#goToScene method
    Code (CSharp):
    1. public static void goToScene(string scene) {
    2.         switch (scene) {
    3.             case "login": SceneManager.LoadScene("LoginScene", LoadSceneMode.Single); break;
    4.             case "lobby": SceneManager.LoadScene("LobbyScene", LoadSceneMode.Single); break;
    5.         }
    6.     }


    Thanks in advance