Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Switching between XAML and Unity thread

Discussion in 'Windows' started by nekk1, Oct 9, 2013.

  1. nekk1


    Nov 27, 2012
    Hello guys.

    I am having incredibly hard time with WP8 and Unity. I have an app that starts with XAML menu, and from there you can navigate to the Unity game. Everything works fine, except few things:

    • Sometimes the app crashes right after locking the phone
    • Sometimes the app crashes when you go back to the XAML part of the app from the Unity part
    It crashes with the following message, but doesn't throw exception, or starts the debugger.
    Code (csharp):
    1. The program '[2800] TaskHost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
    I'm using the following function that triggers when you click the "Back" button in the Unity app.
    Code (csharp):
    1. private void UnityPause_PauseExit(object sender, EventArgs e)
    2.         {
    3.             System.Diagnostics.Debug.WriteLine("EXITED FROM PAUSE");
    4.             Dispatcher.BeginInvoke(() => {
    6.                 if (NavigationService.CanGoBack)
    7.                 {
    8.                     NavigationService.GoBack();
    9.                 }
    10.                 else
    11.                 {
    12.                     NavigationService.Navigate(new Uri("/Views/Menu.xaml", UriKind.Relative));
    13.                 }
    15.             });
    16.         }
    Can you give me a direction what to do, or even where is the problem?
    Also, if you know a better way of switching between Unity and XAML pages, please let me know.

  2. tswalk


    Jul 27, 2013
    to do the callback from Unity, it must be done on the UI thread, but is triggered via the delegate UnityEvent (as shown how to do in the examples here: )

    so your called method would contain something like the following:

    Code (csharp):
    2. private void UnityPause_PauseExit(object arg)
    3. {
    4. AppCallbacks.Instance.InvokeOnUIThread(new AppCallbackItem(() =>
    5.            {
    6.                  if (NavigationService.CanGoBack)
    7.                       {
    8.                          // do stuff
    9.                       }else
    10.                             {
    11.                                // do something else
    12.                             }
    13.            }), false);
    14. }
    However, I do not believe you will be able to use navigation or session state manager to handle anything unity related, unless it is some data set you've been pumping between unity engine and the XAML frontend.

    in my experiences so far, there is no way to dynamically change swapchainbackgroundpanels and maintain the current unity engine instance running.. it dies along with hooked event handles etc... so, currently performing any XAML page navigation will fail. You need to change mind set to think "single page" with two layers.. one for XAML stuff and DirectX in background frame buffer while using callbacks between.

    hope that helps.
    Last edited: Oct 10, 2013
  3. nekk1


    Nov 27, 2012
    Thanks for the reply tswalk.

    The thing is, it SOMETIMES fail, not everytime. We were led into thinking that that was the right way, and we continued working on the app. When it came to beta testing, it started failing.

    I'll try the AppCalbacks stuff, and report if there is a success.
  4. tswalk


    Jul 27, 2013
    ya, I can understand.. I hope that does help you.. I would be "very" curious if you're able to get navigation working like that :)

    I tried to.. and failed gloriously.
  5. nekk1


    Nov 27, 2012
    I tried with this approach, but failed. Tried making the UnityBridge static, thinking that would help, but I failed there too.
    If anyone knows a solution, please post in this thread.