Passing Message from a Alert Dialog Box to Unity3D – Part I

In one of my previous post I shared how to call a simple toast message by creating a native android plugin for Unity3D.

In this post we will turn up the notch and create a android plugin to display a alert dialog box with two buttons ‘yes‘ and ‘no‘, which when pressed will pass message in Unity3D so that we can write the logic accordingly.

And it going to take a while so empty your bladders.

 

So lets begin

  • Create a new android project in android studio, name it – ‘DialogBoxPlugin‘.

 

  • Add a empty activity and leave others settings as it is and click ‘finish‘.

 

 

  • The final project will look something like below – we will write all the logic in package name – com.plugin.android.dialogboxmodule‘.

 

  • Change the project view from Android -> Project.

 

 

Here comes a bit tricky part as we are going to pass a message from native android dialog box to unity activity, we need a reference of unity activity which had triggered this dialog box for this we are going to use UnityPlayer class.

  • Go to path C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes. It may vary if you have installed Unity3D on some other path. Copy the magical class named – classe.jar.

 

  • In android studio expand dialogboxmodule and in libs folder paste the classes.jar from above.

 

  • Switch back to android view in project settings and create a class ShowNativeDialogBox.java  in com.plugin.android.dialogboxmodule. Paste the code below in the java file we have just created. Now in my case when I started referring UnityPlayer class android studio showed error – no such class definition. I tried cleaning, rebuilding and again cleaning and rebuilding. But it went away after I restarted the android studio, maybe this is some issue with the android studio if anyone knows solution then leave a comment. So this is a heads up, don’t freak out if same happens with you.

Let us dissect the code –

public static void ShowDialogPopup(String title, String message, String yesButtonText, String noButtonText)

This is a method that we will call from Unity3D project from C# to invoke the Alert Dialog Box, arguments name are pretty obvious :D.

AlertDialog.Builder dialogPopupBuilder = new AlertDialog.Builder(new ContextThemeWrapper(UnityPlayer.currentActivity, GetTheme()));
dialogPopupBuilder.setTitle(title);
dialogPopupBuilder.setMessage(message);

The above lines of code create a new AlertDialogBox and set title and message that we want. It also set the theme by calling GetTheme with help of ContextThemeWrapper.

dialogPopupBuilder.setPositiveButton(yesButtonText, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        UnityPlayer.UnitySendMessage("AndroidCallBacks", "HandleCallBacks", "0");
    }
});
dialogPopupBuilder.setNegativeButton(noButtonText, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        UnityPlayer.UnitySendMessage("AndroidCallBacks", "HandleCallBacks", "1");
    }
});

This is the soul of this whole project, – ‘AndroidCallBacks’ is a gameobject in unity3D project that will receive messages from this java class and messages will be 0 for yes and 1 for no. We will see how to create AndroidCallBacks gameobject in Unity3D and how it will handle these messages further in this post.

  • Now as we had done in Simple Android plugin for Unity3D – Part 1. Go to Build> Make Module ‘dialogboxmodule’, after gradle is done building the jar right click on the ‘dialogboxmodule‘ module in the project window and click ‘Show in Explorer‘.
  • Go to  DialogBoxPlugin\dialogboxmodule\build\intermediates\bundles\debug , here will find the classes.jar which we are going to use in Unity3d project to call Alert Dialog Box.

 

Ok, I have changed my mind, I have to break this post into two otherwise it will be just scrolling up and down while following this post.

Continue reading…

Download this project or clone it from GitHub

If you are curious and want to know about this project, then leave a comment or email me at contact@nipundavid.com

If you are interested to know more about the work I have done than do the same as above 😀

 

Please follow and like us:

Leave a Reply