Unity Web Browser

Published: Mar 20, 2021 by Voltstro

Recently, I wanted to have a web to be displayed within my game. Ok, no problem, I'll get a web browser engine, get it to render to an image and boom and I will be done.

But nope, it wasn't that easy of course. So, I set out to sail the seas of the internet to find or develop a solution.

I had a few requirements for my embedded browser:

  • It needs to be cross-platform
  • Up to date
  • Support modern day HTML5 and CSS
  • Open-Source
  • Be used for displaying the web, not as an UI framework

Initial attempts

At first, I thought I could just use the CefSharp project in Unity and use CEF. But the problem with CefSharp is that its Windows only, and my game will be supporting Windows and Linux, so it's a no go for that.

Ok, well what other options are there then? Well, there is another CEF wrapper called CefGlue, and it targets mono with Windows and Linux, so thats perfect. But once again, there is another issue, as shown by this project, CEF with CefGlue does work in Unity, but it crashes when you enter play mode for the second time. This is due to how Unity handles native plugins, and the way that CEF was programmed.

Once Unity loads a native plugin, it never unloads it, and once CEF loads, it never let goes of it external files (such as cef.pak), so what was happening was CEF was loading external files, but never unloading them, so it would go to load them again, but they were already in use, causing Unity to crash.

Ultralight

Another is Ultralight, a modern-day web browser engine, made by the same people behind the now defunct Awesomium project, and its significantly lighter then CEF. But even though it advertises itself to be able to embed in games, it currently suffers the same issues with CEF… bruh. Hopefully in the future they can flush out this problem. Ultralight also has some weird licensing, with a large chunk of the code closed-sourced and under a proprietary license.

Servo

Servo is a web browser engine that was first developed by Mozllia and is now in the hands of the Linux Foundation. Its goals is to be a modern day, cross-platform engine written in the Rust programming language. It's also open-source under the MPL-2.0 license. However, like the others, it's got problems. Its major problem is that it's still an experimental browser, ok that's fine for the most part, half the software I use anyway is beta or experimental software, but the big question is, does it work in Unity???? Well yea, supposably. This project I found uses it in Unity, but its MacOS only, yuck. However supposably it should work with Windows and Linux, Servo itself is cross-platform.

"Ok, so fix it and compile it for yourself retard", that might be what you are saying, but I have no fucking clue on how to use or compile Rust.

In the future, if I am bothered enough to understand the basics of Rust, then maybe this might be a good contender to replace the browser engine with.

So, what did you do?

Well, there is one way, and that's to render the browser in a separate process and send data between the two processes.

So that's exactly what I did. I programmed a process in .NET 5 that uses CefGlue and CEF to render the browser, and used a ZeroMQ to communicate between that process and Unity. And with .NET 5's ability to compile into a single application, it comes as a single ~30mb file (not including the external CEF libs).

After a few weeks slaving over Unity and Visual Studio, I finally got it to display a website. I also added the ability to handle keyboard and mouse events and use multi-threading for communication.

I made the entire thing open source under the LGPL-3.0 license; you can find the project here.

Using it in your own project

I designed the project to use a Unity package, so you can install it with the Unity package manager with this git URL:

https://gitlab.com/Voltstro-Studios/WebBrowser/Package.git

I am still flushing out some minor issues, so if you find an issue, please report it on the repo.

Screenshots

Here are some screenshots of it working in the editor and as a player build.

Screenshot 1

Screenshot 2

Screenshot 3

Screenshot 4

Share

Latest Posts

Introducing VoltRpc

Hello, it’s me again, making my once a semester blog post. This blog post is going to be about my new .NET library, VoltRpc.

Unity Web Browser

Recently, I wanted to have a web to be displayed within my game. Ok, no problem, I'll get a web browser engine, get it to render to an image and boom and I will be done.

Time For a Name Change

The time has finally come! The time where I finally change my name. Introducing my new name: “Voltstro”!