itisnajim / SocketIOUnity

A Wrapper for socket.io-client-csharp to work with Unity.
MIT License
379 stars 65 forks source link

How to update Unity Editor UI after receiving data from server #58

Open FattyQi opened 1 year ago

FattyQi commented 1 year ago

I am trying to update the texutre on Editor UI after receiving the path from server side. The C# code is shown as below. After triggering the event, I can successfully get the lastImagePath, but cannot execute the code inside UnityThread.executeInUpdate. Do you know how to fix this?

socket.On("generationResult", (response)=>
                {
                    JArray jarray = JArray.Parse(response.ToString());
                    JToken jtoken = jarray[0];
                    string basename = (string)jtoken["url"];     
                    lastImagePath = Path.Combine(installationFolder, basename);
                    Debug.Log(lastImagePath);
                    UnityThread.executeInUpdate(() =>
                    {
                        Debug.Log("test1");
                        // load texture from file
                        resultsTexture = new Texture2D(2, 2);
                        ImageConversion.LoadImage(resultsTexture, File.ReadAllBytes(lastImagePath));
                    });
                });
itisnajim commented 1 year ago

as in the README if you want to play with unity game objects (eg: rotating an object) or saving data using PlayerPrefs system use this instead:

socket.OnUnityThread("spin", (response) =>
{
    objectToSpin.transform.Rotate(0, 45, 0);
});

replace spin with your event generationResult