Archive March 2010

Untiy3D Asynchronous Sockets

Untiy3D Asynchronous Sockets

You can download this project here.

There is a lot of discussion on whether you can use threading or asynchronous sockets with Unity3D and with the web player in particular.

I’ve created this sample project to demonstrate how this is done.  You’ll see above that when you run it, the project attempts to make several asynchronous connections to web servers and download their default page.

There are a couple of basic things you should watch out for:

  1. Calls to Unity object or the UnityEngine API must be done on the main Unity application thread. You will need to martial values back to that thread. In this example, you’ll notice I don’t update the GUIText text value directly.  I update my own internal status string and only update the GUIText during an Update( ) call.
  2. You need to shut down your socket and thread operations when OnApplicaitonQuit( ) is called. Exceptions may be thrown on any asychronous callback and you need to be sure to catch and handle these.