I would like to build a VNC client using Unity. I am wondering if there are any plugins, libraries, packages, tutorials, etc that I can use to help me along the way?
Any input or guidance on how I might achieve this will be greatly appreciated!!
Find a .net vnc library whose api has some way to get at the stream of image data.
Draw that image data to a Texture2D.
Attach that Texture2D to some surface in unity.
Really, youāre asking for something really weird. Youāre not really going to find many unity users that have implemented this in itself.
Thing is, youāll definitely find lots of blogs/tuts/info about implementing VNC in .Net/Mono. All you need to do make the connection from unity to thatā¦ which is rendering the stream to a Texture2D, and forwarding the mouse/keyboard events to the server.
The source code is available for download. This means we can rip it apart and take a look at how stuff is done.
Now, the directions on the site tell you to use the WinForms control called āRemoteDesktopā for including it in your projectā¦ but that we canāt do, because weāre in unity. So what we need to do is mimic this winforms control.
So if we load up the project, and open that file, we can see how they connect the vnc, and how they draw it to winforms control. Youāll be connecting to vnc in the same exact way, but instead of drawing to the winform control, youāll write it to a texture2d.
Digging through it, it appears to store a System.Drawing.Bitmap, and on the VncUpdate event of the vnc client, it draws the image data to this Bitmap. Which then in the OnPaint event (this is a winforms event used to draw the graphics of the control) it draws that bitmap to the control.
This, is the part we wantā¦ thing is System.Drawing isnāt supported in Unity. So we need to rip this dependency out of it. How this is getting drawn is the VncUpdate hands us an IDesktopUpdater that accepts a Bitmap to draw to. All we have to do is convert this IDesktopUpdater to support a Texture2D call, implement said interface, and then go into the factory that creates the IDesktopUpdater and make it create our custom version again.
Itād probably be a good measure to purge any and all code that references System.Drawing and System.Windows.Forms. As just having it can cause issues in unity, even if you donāt access it.
I did some extra digging. To purge the System.Windows.Forms and System.Drawing dependenciesā¦
thereās 6 classes to remove:
RemoteDesktop
PasswordDialog #these are just the display controls which we want to mimic
VncClippedDesktopPolicy
VncScaledDesktopPolicy
VncDesktopTransformPolicy
VncDesignModeDesktopPolicy #these are behaviour policies for how the controls above should act depending the state of the winform.
Also, in VNCClient, thereās a bit of code at line 390 that attempts to sync with the WinForms UI threadā¦ this should be removed, just call the VncUpdate event. You will have to sync to the unity thread when handling the eventā¦ but honestly, that syncing should be handled by the event receiver, not the event sender (not sure why the put such a dependency into the VncClient classā¦ thatās bad design).
see:
// In order to play nicely with WinForms controls, we do a check here to
// see if it is necessary to synchronize this event with the UI thread.
if (VncUpdate.Target is System.Windows.Forms.Control) {
Control target = VncUpdate.Target as Control;
if (target != null)
target.Invoke(VncUpdate, new object[] { this, e });
} else {
// Target is not a WinForms control, so do it on this thread...
VncUpdate(this, new VncEventArgs(er));
}
And lastly, several classes have minor references to types like āPointā, āRectangleā and āBitmapā from the System.Drawing namespace. Youād just change these to use āVector2ā, āRectā, and āTexture2Dā instead.
Iām really interested in this, was any progress made on this?
Iām a Java dev thatās new to C#/.Net and Windows APIs in general (and totally new to Unity or anything like it). So my progress will be slow, but for now Iām going to poke with lordofductās suggestions.
@barab , Iām in a similar boat. Did you make any progress?
Right now Iām converting all of the Rectangles and Bitmaps to their respective Rects and Texture2Ds, but Iām running into troubles converting BitmapData into a Texture2D equivalent.
Hi, looking for the same to create a virtual desktop concept in VR. If you guys are able to share code or discuss id like to help with getting that last issue with texture solved
Hey there,
6 years later, this trail still have interests.
@YohanB did you succeed in your project? Iām looking to do exactly the same. @cfloutier Thanks for sharing. Did you had any progress on your project? Iām about to try your integration.
Do you guys have any other leads about remote desktop streaming frameworks?
Cheers
Hi, Iām also interested about remote desktop streaming. @Untoldecay did you have any luck with the integration of @cfloutier 's project? I see that it is quite old, made with Unity 5.