AR (Augmented Reality) is becoming popular in enterprises , there is a need of integrating AR experience built using Unity into Native Android Application that has been already developed.
If you are looking to add Unity Player activity into your existing Android project, this post is for you.
The steps given are simple and straight forward and should not take much time to merge the Unity project into your current android project.
My assumption here is that
- You have already built an AR experience using Unity ( let us call it as UnityApp)
- You want to call this UnityApp from within your (AndroidApp) - Native android app . (say with a click of “ARButton” in MainActivity)
Follow below mentioned steps
- Go to “Player Setting” of UnityApp project and change your package name to that of your AndroidApp
- Click checkbox “Export Project” in your “Build setting”. Click “Export” button so that Android code is available. You are going to merge UnityApp code manually into your existing native AndroidApp
- Copy unity-classes.jar , UnityAds.aar , VuforiaWrapper.aar (if you are using Vuforia) from /libs folder of UnityApp to /libs folder of AndroidApp.
- Copy jniLibs folder from UnityApp to AndroidApp ( in /src/main folder)
- Copy assets folder from UnityApp to AndroidApp (in /src/main folder)
- Go to your Android Studio , open AndroidApp , right click on to your project , New → Activity → Empty Activity (uncheck “generate layout File” option). Name it as UnityPlayerActivity activity. Uncheck Backwards Compatibility option and click Finish.
- Overwrite UnityPlayerActivity.java from UnityApp to your AndroidApp
Now that you have UnityPlayerActivity created its time to change AndroidMainfest.xml file.
- AndroidApp AndroidMainfest file will need set of additional parameters which we will copy from UnityApp Manifest file
- Add all the meta-data information from UnityApp Manifest file to AndroidApp Mainfest file in “application” section. (sample example below)
<meta-data
android:name="unity.build-id"
android:value="1420ed4b-5a1c-4e60-84f9-9cefe66ebea0" />
<meta-data
android:name="unity.splash-mode"
android:value="1" />
<meta-data
android:name="unity.splash-enable"
android:value="True" />
- Add use-features and use-permission to main section of Manifest file
- Add following tags from UnityApp Manifest file to section of UnityPlayerActivity in AndroidApp Mainfest
- theme (android:theme=" @ /UnityThemeSelector" to be copied from application section)
- configChanges
- hardwareAcclerated
- launchMode
- screenOrientation
- any other parameters based on your UnityApp project that are reflected in your Manifest file.
- Make changes in style.xml of AndroidApp to accommodate Unity theme styles as per style.xml of UnityApp
Now next step is to make necessary changes in build.gradle (module level) of AndroiApp
Please note that Unity generates just one build.gradle which we will use to copy necessary parameters into module level build.gradle
- Add repositories, lintOptions ,aaptOptions, buildTypes, packagingOptions and bundle to android section (sample below)
repositories {
flatDir {
dirs 'libs'
}
}
lintOptions {
abortOnError false
}
aaptOptions {
noCompress = ['.unity3d', '.ress', '.resource', '.obb', 'vuforia/creditcard.dat', 'vuforia/creditcard.xml']
}
buildTypes {
debug {
minifyEnabled false
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
jniDebuggable true
}
release {
minifyEnabled false
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
signingConfig signingConfigs.debug
}
}
packagingOptions {
doNotStrip '*/armeabi-v7a/*.so'
doNotStrip '*/x86/*.so'
}
bundle {
language {
enableSplit = false
}
density {
enableSplit = false
}
abi {
enableSplit = true
}
}
- Add
ndk { abiFilters 'armeabi-v7a', 'x86' }
to defaultConfig section - Add following implementation to dependencies section
implementation(name: 'UnityAds', ext: 'aar')
implementation(name: 'VuforiaWrapper', ext: 'aar')
Now you will add click button event to “ARButton” in MainActivity so that you can invoke the Unity activity with click of this button. Sample code below
ARButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Opening Unity 3D", Toast.LENGTH_LONG).show();
Intent unityIntent = new Intent(MainActivity.this, UnityPlayerActivity.class);
MainActivity.this.startActivity(unityIntent);
}
});
That’s All !! Get ready now to build your project and test !!