Unity Webview For Windows

0 views
Skip to first unread message

Jesper Sahu

unread,
Aug 4, 2024, 8:15:18 PM8/4/24
to rrorexratinc
Thiswas a very hard assignment due to all the reflection necessary and deep understanding of the unity callback mechanism. It took me more than a month to figure how to do it correctly. So let me help you make it less than 5 minutes .

Unity has its own wrapper for chromium, so you don't really need to use the OS API or implement it by yourself, though it may be useful in some other projects. Furthermore, implement chromium by yourself is REALLY expensive when it comes to size. If you are going to distribute you code, you should better think about it.


Up until 5.4 I was capable to use the web-view DLL (by reflection) invoke its initialization and receive a functioning web-view window that can be docked or float and call web-view functions like "onLoadError" to receive callbacks with no problem ... but this is not the current case.


In general, just think about the Asset-store. The Asset-store window magically uses the web from unity. Well ... once you decompile unity you see that there is no magic at all, just a very disorganize code with lots of inner hacking and there is a very good reason why it is not served as API.


So what you need in order to call an asset store window is "rectangle", "this" prameter- which is the actual window, and (GUIVIEW).m_Parent-which is a private member that exists in editorwindow and holds the docking area. That is actually the most problematic part. By reflection you cant always get the m_parent. But this pararmeter is necessary when docking or un docking a window.


I found this DLL "UnityEditor.Web.WebViewEditorWindowTabs" which was almost perfect. worked as expected regarding to window util such as dock and un dock, but in order to recieve callbacks or call additional chromium features I needed the instance of the window, the object webview and the actual window. There is no easy way to get them, unfortunatly.


I ended up implementing my own "Create" function that creates the window and take the webview at the same time. The nice thing about this DLL is the the m_parent is not requierd and I don't need to use "set host" reflection function.


So yes, I coppied the webvieweidtor decompiled "create window" template and took the webview object, the window and its instant. But I also wanted this script to talk with js, so I had to make an instant of jsproxymgr and give it all my globals. See the attached code for explenations.


From this point it gets to be a lot easier since I have all I need to work with chromium. Now I have the window, the window instant, and my webview object that I reflected from the DLL. I still want all the chromium functionality and for that I used reflection. For example:


So now I have a way to set up a web view window that can be docked and undocked, the webview object to call unity chromium wrapper and all that is left is to see how I can get callbacks from the HTML to my window.


For this I used unityAsync this mechanism acts like coroutine. It is well documented way of send and recieve messages from js to unity by json format. I started by invoke the init with a link to the html file:


The first parameter is the name of the window, the second parameter is the full file name and the four other parameters are the rect (x ,y ,width , height). Motice that I take the actual window and not just scriptableObject instance here with "var w". It will become useful when I execute js or load url. You have to make sure you have the same name of functions in the script such as in the html and don't forget to set them to be public.


I'm currently working on a mixed reality project in Unity for the hololens 2, where I need to implement WebView functionality. I downloaded the webview (preview) component via the mixed reality tool. In this project I am mainly trying to add vocal commands that will interact directly with the webview component (scroll up and down). For that i first followed the instructions on the microsoft documentation on how to add vocal command in unity for hololens 2 apps -us/training/modules/use-eye-tracking-voice-commands/6-5-exercise-voic....



Then the last step was to write and add the script that would handle the scrolling. Within the script I use the namespace "using Microsoft.MixedReality.WebView;" do not seems to handle javascript commands therefore i get this error ( error CS1061: 'WebView' does not contain a definition for 'ExecuteScriptAsync' and no accessible extension method 'ExecuteScriptAsync' accepting a first argument of type 'WebView' could be found (are you missing a using directive or an assembly reference?)).


To address this issue I browsed microsoft documentation and found out that "using Microsoft.Web.WebView2.Core". Adding this namespace seems to resolve the javascript issue, however this gives me another issue (error CS0234: The type or namespace name 'Web' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?))



I've tried to manually add the Microsoft.Web.WebView2 package through the NuGet Package Manager in Visual Studio, but the package doesn't appear to be recognized in Unity, nor does it appear in my packages-lock.json file. I understand that Unity does not natively support NuGet packages, and WebView2 is a .NET package typically used in .NET projects, not Unity projects.



I've also tried to find a WebView2 wrapper or plugin for Unity, and I found a reference to a "Mixed Reality WebView plugin (Preview) -us/windows/mixed-reality/develop/advanced-concepts/webview2-unity-plu..." on Microsoft's website, but the link provided leads I could not do much with. I couldn't find any other resources, documentation, or download links for this plugin.


I would appreciate any assistance or guidance on how to correctly implement WebView2 in my mixed reality Unity project. Specifically, I'm looking for information on how to add the Microsoft.Web.WebView2 package to a Unity project, or how to use the Mixed Reality WebView plugin (if available).


Hi, I am new to unity development in magic leap 2. I am currently looking for a method to display an interactable webpage in unity. However, the open source WebViewObject seems not working on magic leap, are there any other possible solution other than purchasing the plugin from unity asset store? (I am not sure if they works on magic leap 2 and they are all expensive.)


I am on unity 22.3.17 f1, and I am using windows 10. I am using magic leap 2 and using MLSDK 1.5. When I try to run the sample scene from the WebViewObject in unity, it shows error that "Webview is not supported on this platform". As I need to use Aim Simulation to test my project, I have to use unity editor to run the scene which may not be avoidable.


The first thing I wanted to get working was a good solution for speech to text so we could do the online searches with our voice. Also I think voice is a great method of interaction in AR, at least until we have a good hand tracking solution. I know that Android has some native speech to text functionality so a quick google search will help us find some plugins for Unity.


Now I know literally nothing about native Android development but I wanted to challenge myself so I figured I would just try to write some bridge code for this library and turn it into an Android plugin for use in Unity.

Again, this was a mistake and lead to hours of frustration.


The next weird issue is that this voice functionality can only be run on the main thread or else you will get errors. In order to do this in Unity you have to tell the functions and the plugin to run on the UI Thread as an AndroidJavaRunnable like the picture above.


this allows you to render a WebView to a texture and its even interactable, which is great.

I thought this was the answer until I tried it and found out that is was blocking all my clicks from unity.


So using those ports and my hosting servers IP I can get a hello world example working. Next I install the WebShot module and create a small script that I can pass a URL and it will return me an image of the website at that web address. Now I can start that node script and send an http POST request from Unity to the specific IP and port number of my server which will return me a byte array that is the image of that website.

Thank GOD. Now another problem is when I close my terminal the process ends and quits listening. I do some more research and find a module called forever. NPM install forever and now I can navigate to forever and do forever start the script and it will continue running until I log in and stop it again.


Now we can start both scripts so cd into the nodescripts folder and do node getimage.js And then open up a new terminal window and do node getlinks.js Leave both terminal windows running and go back to the editor. If we press play everything should work fine. We can also go to file, build settings, and hit build and run to get it on our phone! If you wanna stop the servers just hit control c or command q to close the whole terminal.

3a8082e126
Reply all
Reply to author
Forward
0 new messages