Wednesday, February 11, 2015

Problems with Google Play Plugin for Unity

Well, it seems Google Play Android Plugin has a few issues.

If you call Application.Quit() after a Google Play authentication, the app freeze for about 5 seconds. Too bad! That an ANR to the face. There's an open issue on github. Fortunately, there's a little workaround.

After some thought, I came to a workaround that seems to alleviate the problem, although it doesn't solve the underlying problem.

This is the workaround:

1) Make a new Android plugin. In this plugin you are going to send the Unity activity to the background.
public class ActivityUtils 
{
    private Activity activity;

    public ActivityUtils(Activity activity) {
        this.activity = activity;
    }

    public void finish() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() 
            {
                activity.moveTaskToBack(true);
            }
        });
    }
}

2) From Unity Android, call the finish() instead of Application.Quit().

public class MyApplication
{
    public static void QuitApplication()
    {
#if UNITY_ANDROID
        AndroidJavaClass playerClass = 
            new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject activity = 
            playerClass.GetStatic<AndroidJavaObject>("currentActivity");

        AndroidJavaObject activityUtils = 
            new AndroidJavaObject("com.google.unity.ActivityUtils", activity);
        activityUtils.Call ("finish");
#else
        Application.Quit();
#endif
    }
}

9 comments:

  1. How do we create a unity android plugin?

    ReplyDelete
    Replies
    1. Basically you need to have:
      1) java jdk installed.
      2) android sdk installed

      But if you are already compiling to android from unity you have them installed, so no problem.

      Now, to compile the plugin open a cmd terminal:

      1) Compile the java code. Note that output_dir must exist before compiling. Also look what is your android.jar path.
      > c:\path\to\java_jdk\bin\javac.exe path_to_your_java_files\files.java -bootclasspath C:\path\to\android_sdk\platforms\android-20\android.jar -d output_dir

      2) Generate a .jar from the compiled files.
      > c:\path\to\java_jdk\bin\jar.exe cvfM file_name.jar -c output_dir

      After that, your .jar file is generated. Now you have to copy that .jar file to Assets\Plugins\Android in your project. You also need an AndroidManifest.xml file in that same directory.

      There is an example of the content of the AndroidManifest.xml here:

      http://docs.unity3d.com/Manual/PluginsForAndroid.html

      Hope it helps. I think I'm going to make a more detailed tutorial on how to make android plugins. Stay tuned!

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Thank u so much!!!! It worked for me.

      I have a suggestion about the code you have given...

      #if UNITY_ANDROID
      AndroidJavaClass playerClass =
      new AndroidJavaClass("com.unity3d.player.UnityPlayer");
      AndroidJavaObject activity =
      playerClass.GetStatic("currentActivity");

      AndroidJavaObject activityUtils =
      new AndroidJavaObject("com.google.unity.ActivityUtils", activity);
      activityUtils.Call ("finish");
      // Add Application.quit();
      Application.quit();

      Shouldn't we add Application.quit() function at the end?? or else the application will be running in the background after moving to the background thread.

      Delete
    4. AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
      AndroidJavaObject activity = jc.GetStatic("currentActivity");
      activity.Call("moveTaskToBack" , true);

      That will do the same thing, but less work

      Delete
  2. I wrote about a similar issue, I give you the link to my site. slots for real money

    ReplyDelete
  3. Thank you so much for the post you do. I like your post and all you share with us is up to date and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. free games to win real money

    ReplyDelete
  4. Below you will understand what is important, the idea provides one of the links with an exciting site:
    betway casino

    ReplyDelete