Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Native messagebox in WP8 plugin?

Discussion in 'Windows' started by pumpkinszwan, Jun 13, 2014.

  1. pumpkinszwan

    pumpkinszwan

    Joined:
    Feb 6, 2014
    Posts:
    214
    I'm working on a WP8 Unity plugin (based on the sample/tutorial on this site), and all works correctly. But when I try to create a method to display a native Windows Phone messagebox it doesn't work. I get nothing.

    Is this because the native messageboxes are async? Is there something I need to do to get this to work in Unity?

    My code is below. Note: the plugin itself works (I can execute the other methods in it), but this method doesn't produce the desired result of showing a native messagebox.

    Code (csharp):
    1.  
    2. public static void DisplayMessageBoxOK(string message, string title)
    3. {
    4. MessageBox.Show(message, title, MessageBoxButton.OK);
    5. }
    6.  
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,491
    Hi,

    you can only display the message box from the UI thread. Try this:

    Code (csharp):
    1.  
    2. public static void DisplayMessageBoxOK(string message, string title)
    3. {
    4.        Deployment.Current.Dispatcher.BeginInvoke(() =>
    5.          MessageBox.Show(message, title, MessageBoxButton.OK));
    6. }
    7.  
     
    pumpkinszwan likes this.
  3. pumpkinszwan

    pumpkinszwan

    Joined:
    Feb 6, 2014
    Posts:
    214
    Thank you Tautvydas, that works perfectly.
     
  4. vnunity

    vnunity

    Joined:
    Sep 23, 2014
    Posts:
    3
    How do I get the result
    when I use MessageBox with 2 button OK and Cancel
     
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,627
    Method Show() returns, which button was pressed.
     
  6. Santosh-Patil

    Santosh-Patil

    Joined:
    Apr 1, 2013
    Posts:
    7
    Hi,

    I want to call Application.Quit() when user press the ok button in message box. Is there any code to implement in unity?
    My code is:

    Code (CSharp):
    1.  
    2. Deployment.Current.Dispatcher.BeginInvoke(() =>
    3.             {
    4.                var result =  MessageBox.Show("Teting WP8 Plugin", "Test", MessageBoxButton.OKCancel);
    5.  
    6.                 if(result == MessageBoxResult.OK)
    7.                 {
    8.                     //unity windows phone application should be closed
    9.                 }
    10.             });
    Cheers.
     
  7. van_ustwo

    van_ustwo

    Joined:
    Jul 10, 2012
    Posts:
    82
    I would like to know too. The Porting document doesn't explain it well with a simple sample app.
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,491
    UnityPlayer.winmd exports a function "BeginInvoke". You can use that to safely get back to Unity's thread.
     
  9. van_ustwo

    van_ustwo

    Joined:
    Jul 10, 2012
    Posts:
    82
    Not quite understand.
    I'm porting a game to WP8. My game is crashing due to Unity need to run on main thread.
    In WSA, there is WSA.Application.InvokeOnAppThread. How would I do this on WP8?
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,491
    You need to get back on main thread inside your plugin, correct? When building it, add UnityPlayer.winmd to your references (it can be found in Editor\Data\PlaybackEngines\WP8Support\Players\ARM\Master\). Once done, you can use UnityPlayer.UnityApp.BeginInvoke method in a similar fashion as you do on WSA with InvokeOnAppThread.
     
  11. van_ustwo

    van_ustwo

    Joined:
    Jul 10, 2012
    Posts:
    82
    Thanks :) It works