Android Studio Unity3d Plugin



If you are building native Android plugins for Unity with Android Studio here are a couple of items that I struggled with. Note that all examples are using Unity 4.6 and Android Studio.

Version information Unity 5.3.1f1, Android Studio 2.2.3 Export aar plugin available for Unity One, create a Android Studio project 1) click on File-New-New Project, open the 'Create New Project' dialog box, select a appro.

  1. There are hundreds of plugins out-there for Android Studio and the number grows daily. This is mainly due to two reasons. The first is the fact that Android Studio is the industry accepted go-to IDE for creating Android apps.
  2. Steps to create a native Android plugin for Unity in Java using Android Studio – Part 1 (of 2) I’ve created a number of Android / Java plugins for Unity using Android Studio so thought I’d document the process. In Part Two, I’ll show how to add the plugin into Unity3D and use it.

1. Unity Android plugin folder needs a jar file to function – but Android Studio build process does not produce a class jar file.

Android Studio Unity3d Plugin

Android Studio Install Plugin

Studio

It turns out that Android Studio actually does create a jar file during the build process and stores it in an intermediate location (on the Mac it is in ‘build/intermediates/bundles/release/’).

Based on suggestions from here I created two new tasks in my gradle file – cleanjar and makejar – after a build I run both and now have the required jar file in my ‘build/outputs/’ folder.

task clearJar(type: Delete) {
delete ‘build/outputs/MergeVRBridge.jar’
delete ‘build/outputs/AndroidManifest.xml’
}

task makeJar(type: Copy) {
from(‘build/intermediates/bundles/release/’)
into(‘build/outputs/’)
include(‘classes.jar’)
include(‘AndroidManifest.xml’)
rename (‘classes.jar’, ‘MergeVRBridge.jar’)
}

2. If your Android library file requires a context – you can pass it to your lib from Unity.

AndroidJNI.AttachCurrentThread();
androidClass = newAndroidJavaClass(com.mergelabs.MergeVR.MergeVRBridge);

using(AndroidJavaClassactivityClass = newAndroidJavaClass(com.unity3d.player.UnityPlayer)) {
activityContext = activityClass.GetStatic<AndroidJavaObject(currentActivity);
}
androidClass.CallStatic(setContext, activityContext);
androidClass.CallStatic<int>(mergeVRInit);

and in your Android Java clase you’ll have the appropriate function

private static Context mContext;

static public void setContext(Context context) {

mContext = context;
}

Best android studio plugins

3. You need bluetooth permissions set in your Android plugins manifest file.

Menu

Android Studio Unity3d Plugin Download

If you just copy and paste the manifest file from your Android Studio build directory to the Unity Android plugin directory you will see compile and build errors. It appears in Unity 4.6 that if you place a manifest file in the Android plugin directory it overrides the Unity created manifest file for the entire app.

The best solution is to find the Unity generated manifest file, copy it to a temp directory and modify by adding any permissions you need (in my case bluetoth related), then add to the Android plugins folder – Unity will use this manifest file as the default and all will be well.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221; package=”com.merge.demo” android:versionName=”1.0″ android:versionCode=”1″ android:installLocation=”preferExternal”>

<uses-permission android:name=”android.permission.BLUETOOTH”/>
<uses-permission android:name=”android.permission.BLUETOOTH_ADMIN”/>
<uses-feature android:name=”android.hardware.bluetooth_le” android:required=”true”/>

<supports-screens android:smallScreens=”true” android:normalScreens=”true” android:largeScreens=”true” android:xlargeScreens=”true” android:anyDensity=”true” />

<application android:theme=”@android:style/Theme.NoTitleBar” android:icon=”@drawable/app_icon” android:label=”@string/app_name” android:debuggable=”false”>
<activity android:name=”com.unity3d.player.UnityPlayerNativeActivity” android:label=”@string/app_name” android:screenOrientation=”sensorLandscape” android:launchMode=”singleTask” android:configChanges=”mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
<category android:name=”android.intent.category.LEANBACK_LAUNCHER” />
</intent-filter>
<meta-data android:name=”unityplayer.UnityActivity” android:value=”true” />
<meta-data android:name=”unityplayer.ForwardNativeEventsToDalvik” android:value=”false” />
</activity>
</application>
<uses-sdk android:minSdkVersion=”18″ android:targetSdkVersion=”21″ />
<uses-feature android:glEsVersion=”0x00020000″ />
<uses-feature android:name=”android.hardware.touchscreen” android:required=”false” />
<uses-feature android:name=”android.hardware.touchscreen.multitouch” android:required=”false” />
<uses-feature android:name=”android.hardware.touchscreen.multitouch.distinct” android:required=”false” />
<uses-permission android:name=”android.permission.WAKE_LOCK” />
</manifest>

This last couple days, I try to integrate my program in unity with android studio. I am not an expert in both platform but I try to learn it for my class project. To be honest, I have experience working with android studio, for me it is a really good tool to build your android apps, however if you want to develop a games app, it will be little difficult working in this platform. That is why, I try to work with unity, but this is really unfamiliar environment for me, so to make my project can be done, I try to combine both this two tools.

I don`t know this is a stupid idea or not, but maybe some of you want to use this method, then I will help you.

Best Android Studio Plugins

1. Install both android studio and unity in your PC (Don`t worry both are free)

2. Build your apps in both platforms, in my case I build spotify like program in android studio and the game apps in unity.

3. If you done with the design in both platforms. Go to unity and go to Edit -> Preferences ->External Tools. You must locate the SDK and JDK location in your computer. If you don´t know the location of SDK, just open your android studio and click SDK manager button, and new windows will open, and see SDK android location in that windows.

For JDK, if you don´t have one, just download it in and choose the new java development kit provide by oracle, after that just install it in your laptop and don´t forget to remember the location of that file.

4. After you located both those file, right know you able to build your android program from unity. To make sure you can combine your unity program with android studio, copy your android manifest.xml file from android studio than in unity in assets folder make new folder name plugins, and inside plugins make new folder name android. After that paste the manifest.xml in this folder.

5. if you already copied the manifest.xml, then in unity go to file -> build settings. In platform choose android, and choose this settings.

6. After that you will get new file that have same type with android studio. Open that file you already build from unity in android studio.

7. After open the unity file in android studio it will build gradle. Then the gradle build finish, move the java and res file from your android studio project you want to combine with unity. Because you already copied the manifest file so, in this unity project android studio will follow that manifest file. If there is no folder like raw, or drawable in unity android studio, just crate one.

8. Don`t forget to change the package name in your java folder, it must follow the unity package name, you can see the example in UnityPlayerActivity.java file

9. Don`t forget to create button and intent that will open this file UnityPlayerActivity.java, to go to Unity layout game

10 At the end, go to manifest.xml in unity android studio project. And put UnityPlayerActivity.java activity in that mainfest or copy this script below, with some change in android:name part Then build your program in Android studio

After that just build the file in android studio.

The result below:

Related