@Tautvydas-Zilys we are developing a WebView plugin for IL2CPP in 5.6b9 (latest beta version) and we have the following problem: We have this script included in Unity (source code directly inside Unity) but we get weird errors like "exception of type system exception" when instantiating "ContentDialog" class, are we doing something wrong? It's possible to include source code directly or we have to precompile it?: Code (CSharp): using System; #if !UNITY_EDITOR && NET_4_6 && UNITY_WSA_10_0 using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Markup; #endif public class WebViewPlugin { private const string webViewDialogStyle = @"<Style xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Key='test' TargetType='ContentDialog'> <Setter Property='Template'> <Setter.Value> <ControlTemplate TargetType='ContentDialog'> <Grid> <ContentPresenter x:Name='Content' Content='{TemplateBinding Content}' HorizontalAlignment='Left' VerticalAlignment='Bottom' /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>"; #if !UNITY_EDITOR && NET_4_6 && UNITY_WSA_10_0 private ContentDialog dialog; #endif public void ShowWebView(double x, double y, double width, double height, string url) { #if !UNITY_EDITOR && NET_4_6 && UNITY_WSA_10_0 double screenWidth = Window.Current.Bounds.Width; double screenHeight = Window.Current.Bounds.Height; if (dialog != null) { dialog = new ContentDialog(); } dialog.Width = screenWidth; dialog.Height = screenHeight; dialog.MaxWidth = screenWidth; dialog.MaxHeight = screenHeight; dialog.Style = (Style)XamlReader.Load(webViewDialogStyle); WebView webView = new WebView(WebViewExecutionMode.SeparateThread); webView.Margin = new Thickness(x, 0, 0, y); webView.Width = width; webView.Height = height; webView.Navigate(new Uri(url)); dialog.Content = webView; dialog.ShowAsync(); #endif } public void HideWebview() { #if !UNITY_EDITOR && NET_4_6 && UNITY_WSA_10_0 if(dialog != null) { dialog.Hide(); } #endif } }
Hi, firstly, I'm happy you're taking your time to check out the beta . What you're doing should definitely be possible. Does this code work on .NET scripting backend? Are you calling it on the correct thread? Does the exception have no message? The exception gets thrown on line 30, right? By the way, while this has no functionality impact, you might want to use "#if ENABLE_WINMD_SUPPORT" instead of "!UNITY_EDITOR && NET_4_6", as it is enabled when you're allowed to reference windows runtime classes.
Thanks for the fast response. We haven't tried on .NET backend because we have a big project and we get errors of not existing classes (I don't know if in .NET backend uses .NET Core and in IL2CPP full .NET 4.6) so I don't know if that works. Yes, the exception get's thrown on line 30 that it's where initializes ContentDialog.
I forgot, also I can't build c++ solution in Unity If I don't include manually some dlls (listed below) from Unity install directory:
Yeah there was a bug that System.Runtime.WindowsRuntime.dll and System.Runtime.WindowsRuntime.UI.Xaml DLLs were not referenced automatically. It was fixed in beta 10, which should come out any day now. Can you enable break-on-exception setting in VS (Debug -> Windows -> Exception Settings -> Check "C++ Exceptions"), and paste the callstack where it gets thrown? It probably gets thrown due to a bad hresult returned from native code, if it is, can you check the hresult value? Also, which thread are you calling this code from?
Looks like you're getting somewhere! Thanks for the report, I'll investigate the error and get back to you.
Hey, just an update: I've got this fixed and it's on the way to 5.6. Not sure if it's going to make 5.6 final, as it's pretty close to its release, but if it doesn't, it will certainly be in the first patch release. Here's a screenshot from your project after the fix (I changed the URL in case you didn't want it shared publicly):
@Tautvydas-Zilys - Is there a manual workaround for those of us who can not currently upgrade to 5.6? I'm having a runtime issue with the System.Runtime.WindowsRuntime.dll not being able to be loaded (FileLoadException). I'm hoping there is something I can do manually in the project output from Unity.
@Tautvydas-Zilys I got this exception. Do you know what could it be? Stripping? A bug on Unity side? The relevant lines are this but I paste the entire stacktrace. Unity 2018.2.15f1 UWP IL2CPP .NET Standard 2.0 XAML (not d3d)
Creating the control and then showing it. If thats the cause the wrrw r it's really weird. How can I be sure? How can I run that code in the correct thread? Thanks.
It was that but now (it wasn't obvious seeing that stacktrace.. maybe you could make a more friendly exception?) I have another problem that I mentioned on this thread: https://forum.unity.com/threads/uwp-get-swapchainpanel-grid-in-il2cpp-xaml-build.502176/ Also, do you know if I need to call "InvokeOnUIThread" for EVERY WebView API call? Even property getter or setters?
Unfortunately that error comes from within windows - if Windows doesn't give us any information about what went wrong, we can't either. To be quite honest, I don't know. Generally, you can only interacted with XAML UI objects from the UI thread.
Hi @Tautvydas-Zilys I created a bug report. Case 1190321 One year later I came with the same exception but this time I invoke it in the correct thread. It has to be a regression because it worked on previous Unity versions. Test in D3D mode, in XAML works correctly. Also, that stacktrace it's not helpful. It's there a way to reveal that "Exception of type System.Exception was thrown"??? The code it's really simple: Code (CSharp): using UnityEngine; #if !UNITY_EDITOR && UNITY_WSA using System; using Windows.UI; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; #endif public class NewBehaviourScript : MonoBehaviour { #if !UNITY_EDITOR && UNITY_WSA void Start() { UnityEngine.WSA.Application.InvokeOnUIThread(async () => { var simpleWebView = new WebView(); // This also crashes //var simpleWebView = new WebView(WebViewExecutionMode.SeparateThread); simpleWebView.Source = new Uri("https://www.google.com"); var someDialog = new ContentDialog(); someDialog.Content = simpleWebView; await someDialog.ShowAsync(); }, false); } #endif }
I'm pretty sure you cannot use XAML controls in D3D app type. Perhaps that's the issue. But thanks for the bug report, we will take a look.