Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

UWP, IL2CPP and Xaml controls

Discussion in 'Windows' started by bdominguezvw, Feb 23, 2017.

  1. bdominguezvw

    bdominguezvw

    Joined:
    Dec 4, 2013
    Posts:
    92
    @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):
    1. using System;
    2. #if !UNITY_EDITOR && NET_4_6 && UNITY_WSA_10_0
    3. using Windows.UI.Xaml;
    4. using Windows.UI.Xaml.Controls;
    5. using Windows.UI.Xaml.Markup;
    6. #endif
    7.  
    8.     public class WebViewPlugin {
    9.  
    10.         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'>
    11.                                                    <Setter Property='Template'>
    12.                                                        <Setter.Value>
    13.                                                            <ControlTemplate TargetType='ContentDialog'>
    14.                                                                <Grid>
    15.                                                                    <ContentPresenter x:Name='Content' Content='{TemplateBinding Content}' HorizontalAlignment='Left' VerticalAlignment='Bottom' />
    16.                                                                </Grid>
    17.                                                            </ControlTemplate>
    18.                                                        </Setter.Value>
    19.                                                    </Setter>
    20.                                                </Style>";
    21. #if !UNITY_EDITOR && NET_4_6 && UNITY_WSA_10_0
    22.         private ContentDialog dialog;
    23. #endif
    24.         public void ShowWebView(double x, double y, double width, double height, string url) {
    25. #if !UNITY_EDITOR && NET_4_6 && UNITY_WSA_10_0
    26.             double screenWidth = Window.Current.Bounds.Width;
    27.             double screenHeight = Window.Current.Bounds.Height;
    28.  
    29.             if (dialog != null) {
    30.                 dialog = new ContentDialog();
    31.             }
    32.             dialog.Width = screenWidth;
    33.             dialog.Height = screenHeight;
    34.             dialog.MaxWidth = screenWidth;
    35.             dialog.MaxHeight = screenHeight;
    36.  
    37.             dialog.Style = (Style)XamlReader.Load(webViewDialogStyle);
    38.  
    39.             WebView webView = new WebView(WebViewExecutionMode.SeparateThread);
    40.             webView.Margin = new Thickness(x, 0, 0, y);
    41.             webView.Width = width;
    42.             webView.Height = height;
    43.             webView.Navigate(new Uri(url));
    44.  
    45.             dialog.Content = webView;
    46.  
    47.             dialog.ShowAsync();
    48. #endif
    49.         }
    50.  
    51.         public void HideWebview() {
    52. #if !UNITY_EDITOR && NET_4_6 && UNITY_WSA_10_0
    53.             if(dialog != null) {
    54.                 dialog.Hide();
    55.             }
    56. #endif
    57.         }
    58.     }
    59.  
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,666
    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.
     
  3. bdominguezvw

    bdominguezvw

    Joined:
    Dec 4, 2013
    Posts:
    92
    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.
     
  4. bdominguezvw

    bdominguezvw

    Joined:
    Dec 4, 2013
    Posts:
    92
    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:

     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,666
    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?
     
  6. bdominguezvw

    bdominguezvw

    Joined:
    Dec 4, 2013
    Posts:
    92
    Finally you released beta 10 so we tried it with another error:

    Case 887036

     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,666
    Looks like you're getting somewhere! Thanks for the report, I'll investigate the error and get back to you.
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,666
    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):

    upload_2017-3-14_18-43-3.png
     
  9. bdominguezvw

    bdominguezvw

    Joined:
    Dec 4, 2013
    Posts:
    92
    Thanks! I hope that get's into 5.6 final.
     
  10. bdominguezvw

    bdominguezvw

    Joined:
    Dec 4, 2013
    Posts:
    92
  11. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,666
    Unfortunately it did not. It is scheduled to land in a 5.6 patch release soon.
     
  12. bdominguezvw

    bdominguezvw

    Joined:
    Dec 4, 2013
    Posts:
    92
    Ok, thanks.
     
  13. longda

    longda

    Joined:
    Dec 13, 2014
    Posts:
    9
    @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.
     
  14. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    705
    @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)

     
    Last edited: Nov 18, 2018
  15. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,666
    Creating the XAML control on the wrong thread, perhaps? What is your plugin is trying to do?
     
  16. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    705
    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.
     
  17. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,666
    Use UnityEngine.WSA.Application.InvokeOnUIThread().
     
  18. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    705
  19. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,666
    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.
     
  20. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    705
  21. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    705
    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.

    upload_2019-10-9_23-6-25.png

    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):
    1. using UnityEngine;
    2. #if !UNITY_EDITOR && UNITY_WSA
    3. using System;
    4. using Windows.UI;
    5. using Windows.UI.Xaml;
    6. using Windows.UI.Xaml.Controls;
    7. #endif
    8.  
    9. public class NewBehaviourScript : MonoBehaviour {
    10.  
    11. #if !UNITY_EDITOR && UNITY_WSA
    12.     void Start() {
    13.         UnityEngine.WSA.Application.InvokeOnUIThread(async () => {
    14.             var simpleWebView = new WebView();
    15.             // This also crashes
    16.             //var simpleWebView = new WebView(WebViewExecutionMode.SeparateThread);
    17.             simpleWebView.Source = new Uri("https://www.google.com");
    18.             var someDialog = new ContentDialog();
    19.             someDialog.Content = simpleWebView;
    20.             await someDialog.ShowAsync();
    21.         }, false);
    22.     }
    23. #endif
    24.  
    25. }
     
    Last edited: Oct 10, 2019
  22. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,666
    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.