Unity3D-Python-Communication icon indicating copy to clipboard operation
Unity3D-Python-Communication copied to clipboard

About send pictures

Open yumianhuli2 opened this issue 2 years ago • 1 comments

Hello! 1、Should messages be sent only in child threads in order not to block the Unity main thread? 2、I need to send pictures,so I used client.SendFrame(Convert.ToBase64String(CameraHelper.CaptureFrame(Camera))); But I got an error from unity:UnityException: get_targetTexture can only be called from the main thread

So How to send pictures by your plugin?Thank you!

yumianhuli2 avatar Dec 04 '21 08:12 yumianhuli2

The child thread should be handling its own message queue and sending the message to the python server. You should not try to do this networking stuff in the main thread even if you can. It's a separation of concerns and will avoid you problems later.

You should create the base64 string from the main thread, then enqueue the string to the child thread's queue. The child thread then periodically dequeues the queue and send the base64 string to the python server.

If you want to always be sending the picture, I suggest that in an Update() function of a MonoBehavior, you can do the frame capturing and enqueue the base64 string. Because the Update() function will be called in the main thread.

offchan42 avatar Dec 05 '21 00:12 offchan42