Author:- Tushar Sonu Lambole
WORK Flow From Unity to Android
Before starting this steps be prepare with the following files. can be find in any project of Vuforia AR sample unityPackage.
files are available at location "Assets/Plugins/Android/" in Vuforia unity Sample project.
A) QCAR.jar
B) QCARUnityPlayer.jar
Steps.
1) Create a Unity project; for ease of reference we refer to it as “AndroidPlugin” with importing Vuforia libraries.
2) Create a new Android project in Eclipse, for ease of reference we call it "AndroidUnityPlugin". we assume the main activity will then be called RootActivity.
3) Right-click on the project in Eclipse, go to Properties > Java Build Path > Libraries, and add the following two libraries as “external jars” (both located under the Assets/Plugins/Android/ folder of your Unity project):
QCAR.jar
QCARUnityPlayer.jar
4) Open the RootActivity.java file in Eclipse, and make the following code changes:
A) Make RootActivity extend QCARPlayerActivity instead of Activity
B) Remove the line “setContentView(…)” from the onCreate() method.
C) Add a custom public method called showMessage() to the RootActivity class, and fill it with some code to show a Toast message with text.
The code of your RootActivity should look like the following:
package com.qualcomm.plugins;
import android.os.Bundle;
import android.widget.Toast;
import com.qualcomm.QCARUnityPlayer.QCARPlayerActivity;
public class RootActivity extends QCARPlayerActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void showMessage(final String message) {
this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(RootActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
}
}
5) Right-click on the project, go to Properties -> Android, tick the “IsLibrary” checkbox to turn your Android project into a library; this should make Eclipse generate a .JAR file (e.g., called “AndroidUnityPlugin.jar”) and store it in the “/bin” folder of your Eclipse project.
6) Open the “/bin” folder and copy the JAR library from that folder to the “Assets/Plugins/Android/” folder of your Unity project.
7) Open and edit the AndroidManifest.xml file located in the same “Assets/Plugins/Android/” directory.
8) Create the name of the main activity with your fully qualified Activity name, e.g., “com.my.org.RootActivity”
Code should look like this
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qualcomm.QCARUnityPlayer"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-feature android:name="android.hardware.camera" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:debuggable="false">
<activity android:name="com.qualcomm.QCARUnityPlayer.QCARPlayerProxyActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
</activity>
<activity android:name="com.qualcomm.QCARUnityPlayer.QCARPlayerActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
</activity>
<activity android:name="com.qualcomm.QCARUnityPlayer.QCARPlayerNativeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<meta-data android:name="android.app.lib_name" android:value="unity" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
<activity android:name="com.unity3d.player.VideoPlayer"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
</activity>
<activity
android:name="com.mindspacetech.androidunityplugin.RootActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
9) Create a C# script, call it AndroidBridge. create method showNativePopup. Add following code
public void showNativePopup(string message){
#if UNITY_ANDROID
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
//Invoke the "showMessage" method in our Android Plugin Activity
//string message = "Detected trackable: ";
jo.Call("showMessage", message);
#endif
}
10) call above method on button click, anywhere in application
11) build and run the application.
-------------------------------------------------------------------------------------------------------------
WORK Flow From android TO Unity
Before start this steps will require UnityPlayer classes.Jar
in Mac will find at location "/Applications/Unity/Unity.app/Contents/PlaybackEngines/AndroidPlayer/bin"
Right-click on the project in Eclipse, go to Properties > Java Build Path > Libraries, and add the following
two libraries as “external jars” (both located under the Assets/Plugins/Android/ folder of your Unity project):
1) To close android app and go back to Unity open RootActivity.java add following code
private final Handler mHandler = new Handler();
public static final String EVENT_CLOSED = "EVENT_CLOSED";
@Override
public void onBackPressed() {
mHandler.post(new Runnable() {
@Override
public void run() {
try {
// because UnitySendMessage is static
Class<UnityPlayer> c = com.unity3d.player.UnityPlayer.class;
Method method = c.getMethod("UnitySendMessage", new Class[] { String.class, String.class, String.class });
method.invoke(null, "MyAndroidBridge", "OnApplicationClose", EVENT_CLOSED); // reveiver null
} catch (NoSuchMethodException e) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
}
});
super.onBackPressed();
}
2) Update c# script in unity project "AndroidPlugin" script name as "AndroidBridge". Copy the following code.
public static event Action<string> OnApplicationClose; // same as defined in android activity
// Init the class instance
static AndroidBridge instance;
public static void Initialize()
{
Debug.Log("Application Init");
if (instance == null)
{
GameObject newGameObject = new GameObject("MyAndroidBridge"); // Here name is same as of android method.invoke statement
newGameObject.AddComponent<AndroidBridge>();
instance = newGameObject.GetComponent<AndroidBridge>();
}
}
//Callback from Java or Objective-C which notifies an event
//param : "EVENT_OPENED", "EVENT_CLOSED"
//Method name starts small "o" and Event name is capital "O"
void onApplicationClose(string eventStr){
//Debug.Log("EasyCodeScanner - onScannerEvent eventStr=:"+eventStr);
if (OnApplicationClose != null)
{
OnApplicationClose(eventStr);
}
}
Upate showNativePopup() method add following code to check the instance is not null and make the method static.
public static void showNativePopup(string message){
if (instance==null) {
Debug.LogError("launchScanner error : AndroidBridge must be initialized before.");
return;
}
//////// below is previous Code
#if UNITY_ANDROID
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
//Invoke the "showMessage" method in our Android Plugin Activity
//string message = "Detected trackable: ";
jo.Call("showMessage", message);
#endif
////// Till here
}
3) To interact with this methods we will create now the inteface Type class. Create new C# file name it "AndroidBridgeInterface"
A) Create Method as follow
//Callback which notifies an event
//param : "EVENT_OPENED", "EVENT_CLOSED"
void onApplicationClose(string eventStr){
Debug.Log("AndroidBridgeInterface - onApplicationClose:"+eventStr);
}
B) Add following code to Start method
void Start() {
//Initialize AndroidBridge
AndroidBridge.Initialize();
//Register Action
AndroidBridge.OnApplicationClose += onApplicationClose;
}
C) void Ondestroy method
void OnDestroy() {
//Unregister
AndroidBridge.OnApplicationClose -= onApplicationClose;
}
D) we can write the code to call the android application code here.
public void showMessage()
{
AndroidBridge.showNativePopup("Hello World");
}