RTMP Communication with Unity3D using C#
I'm very excited to share with you some information about what we at DNBmedia tried to achieve in the last couple of months. We're very interested in the use of Unity3D, as you could read in my last post. Especially integration with a browser and communication with Flash is important for us. In that last post I talked about communication on a single page between Unity, a browser and through that with Flash. What kind of communication you do need more when you think about Flash? That's right, RTMP communication! And that's exactly where we've been working on in the last couple of months. For those who don't know; in Unity3D you can program in C# and use DLL's. So what we've decided to do was to implement the RTMP protocol in a DLL library. This resulted in having a very hard job on reverse engineering the protocol with just a little documentation. I need to give my best colleague Stijn Raaijmakers all the credits for that, who's a very talented programmer with advanced C# and Actionscript skills.
We're still working on the code and have some issues which we need to solve, so no source code this time, but only a small "proof of concept" version.
Warning 1: Before opening any links from below, I need to warn you that your browser might crash (Mac browsers will crash in any case after a while)! Unfortunately the use of Unity3D makes the library less stable than a stand alone project purely written in C sharp.
Warning 2: This is all about technique, and we did not spend any time on making this look good.
Demo 1 - Unity3D and Flash combination
To show you that both Flash and Unity can connect to the same server and do something with that data, I've build a very simple Unity3D scene where you can move around. Other visitors on the page will see you as a moving cube. On the bottom we have a 2D Flash representation of the scene and a square which you can control by using your arrow keys. However, Unity3D will react on each key you press when you're in that same browser window. So when you're controlling the square in Flash, you will control the Unity scene as well. This is not easy to control, so to simulate a moving visitor in the scene I've added a button "turn automove on". When it works you should be able to see a cube animating in rounds somewhere around you in the Unity3D scene. This will be almost in real time.
Demo 2 - Chat and 3D game combination
One of the most basic ways to show you have communication between a client and a server is a chat. So that's what we've build to test things out. There's a C sharp program as well which communicates with this server (not posted here). When you're lucky you'll find other visitors on the page where you can have a chat with ![]()
Another thing we started implementing in this demo is some little multiplayer 3D game. That's not finished yet, but it will give you an idea what might be possible.
Our goal for the client side is to make it look like as much on Flash as possible. This will make it easy to use and understand for Flash users. So to make a connection you just have to type:
client = new RtmpClient(); //Add listeners client.OnConnect += new RtmpClient.ConnectHandler(client_Connected); client.OnError += new RtmpClient.ErrorHandler(client_OnError); client.OnDisconnect += new RtmpClient.DisconnectHandler(client_OnDisconnect); client.OnInvoke += new RtmpClient.InvokeHandler(client_OnInvoke); //Connect! client.Connect(url, username, password); //Send update client.Invoke("sendMessage", new RtmpResponderDelegate(messageResponder), "Hello this is a test" ); //Receive result (as a responder in Flash) public void messageResponder(bool success, object returnValue) { //Do something } //Receive invokes, based on listener from above public void client_OnInvoke(string function, params object[] args) { //Do something }
On the server side we're using Red5, but it should run on any other server which provides RTMP communication. However we haven't tested that yet.
To achieve this I have to thank some sources for their help, since without them we had even a harder time developing this.
- Red5 - They've done a great job on reverse engineering the RTMP protocol. The best after all is that their source code is open sourced, so this was a real good source to see what's going on under the hood of this undocumented protocol.
- Sander Wichers - Cause of his work on Mammoth (a C++ open source RTMP server) he faced recently the same challenges as we did and was able to point us in the right way when needed.
- Red5 mailing list - A very active community, willing to help and a good archive for information.
- OSFlash - RTMP protocol draft
- Gnash - basic RTMP documentation
When you're familiar with Unity3D you might know there are other options to make multiplayer games in Unity3D. One of them is the built-in networking, which could fit your needs in some cases, but when you want to go advanced it's too limited. An other option is using a SmartFoxServer which should make it possible to have a connection with Flash and Unity3D as well (haven't found any examples about that by the way). They've done a great job with their libraries. In my case I have some reasons why not to use their option and want to use something over RTMP.
When time allows it, we've big plans with extending this library and this will be just the top of the iceberg.
To be continued....

[…] More details available from Paul’s blog […]
Nice work, Paul, i look forward to seeing this in action.
nice Paul. Yeh I am working on some similar stuff I should share eventually. I am making udp servers with twisted in python, mono c# services (all multiplatform) to interface with flash and unity3d with json and compressed json. The rtmp protocol is awesome but really digging the RTMFP and udp protocols to get larger user bases and better authoratative servers.
You are onto some cool stuff here thanks for sharing.
Interesting to know that you’re working on somthing similar as well Ryan! UDP support will defiantly have advantages above tcp! Have a look at Chris Allen’s blog where he talks about UDP as well in combination with red5.
Keep me posted about the progress you’re making!
[…] RMTP Communication with Uity3D Using C# Updated from previous link to thinkinflash.com Rate this: 3.0 Share and Enjoy: […]
[…] entry Unity3D vs Flash Or Not- Blogosphere Roundup, I misattribute a blog entry that he had written RTMP Communication with Unity3D Using C# to another blog where the entry had been cut-n-pasted, Verbatim. This got me to thinking about the […]
Really nice work Paul, hope it will come for windows soon:)
[…] Paul Tondeur " Blog Archive " RTMP Communication with Unity3D using C# (tags: unity3d) […]
[…] Paul Tondeur " Blog Archive " RTMP Communication with Unity3D using C# (tags: unity3d) […]
[…] entry Unity3D vs Flash Or Not- Blogosphere Roundup, I misattribute a blog entry that he had written RTMP Communication with Unity3D Using C# to another blog where the entry had been cut-n-pasted, Verbatim. This got me to thinking about the […]
[…] RMTP Communication with Uity3D Using C# Updated […]
[…] Flash Or Not- Blogosphere Roundup, I misattribute a blog entry that he had written RTMP Communication with Unity Using C# to another blog where the entry had been cut-n-pasted, Verbatim. This got me […]
[…] RMTP Communication with Uity3D Using C# Updated […]
[…] compiling The first version of the Flash vs Unity Blog roundup. He had written a blog entry about communicating with Unity in C# using RTMP Paul has also done several presentations including at Unite 2009 and writes frequently about […]
[…] RMTP Communication with Uity3D Using C# Updated […]
Hello,
You say :
>There’s a C sharp program as well which communicates >with this server (not posted here).
Where can i download srcipt C# using RTMP for testing in unity3D ?
[…] RTMP Communcation with Unity3D Using C# from Paul Tondeur […]
[…] RMTP Communication with Uity3D Using C# Updated […]