Consuming a WCF Service from Unity3D

Unity3D is running MONO.Net and WCF implementation was for some time being worked on under “Moonlight 3.5” for MONO.  It has been merged into the main distribution of MONO for a while now; and yes, Unity3D supports it.  I will assume if you are reading this, you know how to create and host WCF services so I will not touch that at all.

Assumption: You have a WCF service running on your local IIS server at: http://localhost:8080/MyService.svc

There are a few infrastructural steps involved on the Unity3D side:

  • Setting API compatibility
  • Add plugins to project tree
  • Generate and add client proxy
  • Add gateway specific to the Unity3D application

Setting API compatibility

By default, Unity3D uses a stripped down version of the MONO framework.  You can consider this similar to .Net 4.0’s Client Profile versus Full version.  This setting can be changed through the menu bar: Edit -> Project Settings -> Player.  It needs to be set to 2.0 and not 2.0 Subset.

Add plugins to project tree

Unity3D’s runtime will enumerate a specific folder for assemblies and make those references available to the scripting engine “globally”.  At the root level, simply create “Plugins”.  Any assembly you package and use as a business layer should be deployed here.

In the new container folder, you need to add the MONO assembly compilations for the standard WCF assemblies you’d normally use in a Microsoft .Net Framework runtime.  Navigate to your Unity3D installation directory and start diving into the mono 2.0 location (for a point of reference, mine is: C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0).

You will need to copy three assemblies into the Plugins container:

  • System.Runtime.Serialization
  • System.Security
  • System.ServiceModel

Generate and add client proxy

Add another container named “ClientProxies”.  This can technically be nested under “Scripts” or whatever folder convention / structure you may have or desire (Plugins is the only engine specific folder).  Now run the Visual Studio Command Prompt and navigate to the ClientProxies folder (this will have the generated proxy code placed here automatically, you can alternatively generate it anywhere on your filesystem and move/copy it into this container).  Generate the proxy class using SVCUTIL

svcutil -out:MyServiceClient.cs http://localhost:8080/MyService.svc?wsdl

This will generate the same files that get created when adding a Web Service reference through Visual Studio.

Add gateway specific to the Unity3D application

Now all that remains is writing a gateway script that will use the newly created client proxy as an object and execute methods off the service.  You do this the same as any ofther WCF in-code call.  You will access to the MyServiceClient object type now, and can pass in the endpoint and binding information through the constructor.

Thats all!


10 Responses to Consuming a WCF Service from Unity3D

  1. forrestuv says:

    WIll it work on iPhone/Android?

    • stpdev says:

      I don’t have either mobile license so I can’t verify.

      I see no reason why it wouldn’t. It’d be no different than using an modern web app via the devices browser. It would be making the same AJAX calls to web apis.

      The code is all supported by MONO. Give it a try and let me know if you have the license.

  2. Valerio says:

    Do you know if it works even in WebPlayer and if it needs Unity3D Pro?

    Thank you very much.

    • stpdev says:

      It does not require pro. WCF is not socket level, it sits on top… which means indie version will work with this.

      I will test out the web player later tonight.

  3. forrestuv says:

    actually it won’t work on iOS/Android 😦

  4. Adam says:

    Why won’t this work on iOS/Android?

    I am trying to consume a wcf service from a unity3d scene and my target platform is mobile. Is there a reason or workout to get this functionality?

    • stpdev says:

      It may be a mobile limitation. Mobile works best with using REST calls and JSON parsing back and forth. I will put an example together. I can’t test on mobile still, as I only have Unity Pro. I was waiting till 4 is released to get mobile addons.

      I understand why iOS or Android wouldn’t be able to handle WCF natively (as it is a Microsoft implementation of a industry standard… Java has its own implementation and the two can talk over the “standards”). Now, RESTful services are completely different and far more agnostic. It has performance implications of course since its HTTP gets/posts/puts/deletes, but its agnostic since it performs the same operations the device’s web browser makes.

      • Nico says:

        The problem is that WCF relies on run-time bytecode emission to generate proxy classes for your web service interfaces (specifically, through the System.Runtime.Remoting namespace). Bytecode emission is not supported on iOS, because all code has to be compiled ahead of time on that platform. The same thing probably applies for Android as well, though I’m less familiar with the limitations of that platform.

        You can read more about the limitations of MonoTouch (as used by Unity on iOS) here:

  5. Adam Emrick says:

    Thank you so much for posting this. It helped a ton!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: