Unity3D: android plugin development

In the process of creating a game application in Unity3d we are faced with a problem when necessary to find out what software is installed on the client's Android-device.

Of course, we do not collect personal information about our users. This need was added due to the fact that our customer has decided to give some bonuses to users only if the users has any software of customer.

In some words we have one question: if the software is installed on this mobile device or not.

We used the latest version Unity version and Android SDK as well.

Since the requirement was only for the Android OS, it was decided to add unity3d plugin written for Android by java. Here we are faced with several challenges, knowing the decision we would have saved a lot of time.

 

Start.

The java code was simple:

public class SomeAppTools
{
   public static boolean isSomeAppUnlockable(Context context)
   {
       List packages = context.getPackageManager().getInstalledPackages(0);
       for(PackageInfo pi : packages) // for all user apps
       {
           if (pi.packageName.equalsIgnoreCase("com.SomeApp.SomeApp2015")
              || pi.packageName.equalsIgnoreCase("com.SomeApp.SomeApp2016")
           {
               return true; // we found it
           }
       }
       return false;
   }
}

Java packet building

First, the decision was a simple, as we suggested. Java-script SomeAppTools.java with a single function in a single class first compiled into byte code SomeAppTools.class, then created SomeAppTools.jar package.

The resulting package was put in the folder Assets\Plugins\Android\.

According to the plan of unity developers, all packages of Android-application jar in that folder are included in the resulting apk-file.

We failed to assemble apk-file of game. When assembling apk-file - error occurred, we were not able to overcome. We also tried to mention our package in the manifest file. Also it did not help.

When assembling apk-file studio Unity3d in Temp/Staging folder collects all necessary. Analyzing the contents of this folder, you can draw conclusions about the presence / absence of the necessary libraries and the manifest file elements.

We do not have overcome the error is not because we are lazy, but because we have decided to try the way, which all recommended for Unity3d plugins.

 

Eclipse Studio for java-developers

A better way is to use the Eclipse studio for java-developers. There were no adventures. Assembled jar from the first time added to the apk-file of the game. Without any changes to the manifest.

Why the jar, compiled from the command line, did not work, we do not know. We are the programmers, who put at the heart time and result. The result has been achieved. Time did not want to spend.

 

Call of Android functions from unity3d. Transfer context of unity3d in android function.

We also understand that the call of the code of the plug-in should be framed in a conditional block only for Android. This is necessary to ensure that this part of the code will only include in the application distribution for the Android OS:

#if UNITY_ANDROID && !UNITY_EDITOR
...
bool b = isSomeAppUnlockable(...)
...
#endif

  

To access the native Android-functions needed to get the object AndroidJavaObject, representing a package that implements the class. Then get the object AndroidJavaClass, which is a class that implements this function itself.

Since the function - static, its call is made using CallStatic().

 

As a parameter of the function we needed to transmit the device context. Android programmers must know what it is.

public static bool _isSomeAppUnlock()
{
  #if UNITY_ANDROID && !UNITY_EDITOR
  using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  {
    AndroidJavaObject playerActivityContext = actClass.GetStatic("currentActivity");
    bool ret;
    using(AndroidJavaClass jniSomeAppClass = new AndroidJavaClass("com.southmedia.SomeApp.SomeAppUnlock"))
    {
      ret = jniSomeAppClass.CallStatic("isSomeAppUnlockable", playerActivityContext);
    }
    return ret;
  }
  #endif
  return false; //!!
}



Contact us

Email: This email address is being protected from spambots. You need JavaScript enabled to view it.
Skype: south-media.com
Phone: +7 903 4057009, +7 928 1260550
Top
Локальный сервер Денвер.
Установка Joomla на локальный сервер.