By Jc

Creating the first activity. The first activity works as an intermediary that will communicate with Unity and will call the main activity of the Android project. So the code for this activity will be like this:


package gr.ceid.upatras;


import com.qualcomm.QCARUnityPlayer.QCARUnityPlayer;

import android.content.Intent;

import android.os.Bundle;


public class Binding extends com.qualcomm.QCARUnityPlayer.QCARPlayerActivity {


@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

}


public static void buttonClicked() {


QCARUnityPlayer.currentActivity.runOnUiThread(new Runnable() {

public void run() {

//Log.i("Binding Activity", "called succesfully");

Intent intent = new Intent(QCARUnityPlayer.currentActivity.getApplicationContext(), PatrasMap.class);

QCARUnityPlayer.currentActivity.startActivity(intent);

}

});

}

}

 

Note 1: The Activity we created extends the QCARPlayerActivity and not the Activity class. This is the reason we added the jars in the build path in the first place.

Note 2: We created a static function that starts a new Activity and we left the onCreate() function empty.

Note 3: We have to wrap the code of the static function inside a new Runnable(), otherwise it will crash when Unity will make calls to this function.


The next thing we have to do is to create the second activity of the project. The code for this activity:


package gr.ceid.upatras;



import android.app.Activity;

import android.os.Bundle;

import android.widget.Toast;


public class PatrasMap extends Activity {


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        RelativeLayout relativeLayout = new RelativeLayout(this);



        TextView tv = new TextView(this);

        tv.setText("Custom Message");


        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(

                RelativeLayout.LayoutParams.WRAP_CONTENT,

                RelativeLayout.LayoutParams.WRAP_CONTENT);

        lp.addRule(RelativeLayout.CENTER_IN_PARENT);


        tv.setLayoutParams(lp);


        relativeLayout.addView(tv, lp);


        setContentView(relativeLayout);

        tip();     

    }

  

    public void tip()

    {

        Toast.makeText(this, "Activity called. Plugin is working!", Toast.LENGTH_LONG).show();      

    }

}

 

Note 1: We create the layout programmatically because otherwise i get Resource not found exception. This needs to be checked but i haven’t done it yet.


Now, we have to declare our activities in the manifest. The AndroidManifest.xml file of the Android project should look like this:


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="gr.ceid.upatras"

    android:versionCode="1"

    android:versionName="1.0">


    <uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>



    <application

        android:icon="@drawable/ic_launcher" android:label="@string/app_name"> <!--  android:installLocation="preferExternal" --> 

        <activity android:name="gr.ceid.upatras.PatrasMap" android:label="@string/app_name">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity android:name="gr.ceid.upatras.Binding"

            android:theme="@android:style/Theme.Black.NoTitleBar"

android:configChanges="keyboardHidden|orientation">

        </activity>

    </application>

</manifest>

 

The final step from Eclipse side is to export the project as a jar file. Not entirely sure for this step but just do it anyways: before you export the project head to the Properties and under the Java Build Path again and click the Order and Export tab and select the three jars we imported at the beginning.




To export the project right click on it and select Export... Follow the steps and place it anywhere you like.

If everything went well, we have finished the Eclipse side and we can proceed with the Unity side.



Unity Side


Let’s start by copying the jar we created earlier in the Unity project under the “Assets/plugin/Android”. Now we have to complete the existing AndroidManifest.xml file that we will find in that directory. So this file must look like this:


<?xml version="1.0" encoding="utf-8"?>

<manifest

    xmlns:android="http://schemas.android.com/apk/res/android"

    package="gr.ceid.upatras" <!--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"/>

   

    <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:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity android:name="com.qualcomm.QCARUnityPlayer.QCARPlayerActivity"

                  android:label="@string/app_name"

                  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: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:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">

        </activity>

        <activity android:name="gr.ceid.upatras.Binding"></activity>

        <activity android:name="gr.ceid.upatras.PatrasMap"

          android:theme="@android:style/Theme.Dialog"

          android:configChanges="orientation|keyboard|keyboardHidden"/>

       

    </application>

</manifest>




Note 1: At the beginning of the file we change the package name to the one we used in the Android project. Make sure is exactly the same!

Note 2: Near the end of the file we add the two activities of the Android project.

 

OK! Final step...Lets create the script that will call our activities!


In my case i have a script that does a lot of things but the part we need is inside the OnGUI() function. So the code will look like this:


public static AndroidJavaClass plugin;


void Start ()

{

    if( Application.platform == RuntimePlatform.Android ) {

plugin = new AndroidJavaClass("gr.ceid.upatras.Binding");

}


    //....................

}


void OnGUI()

{

if (GUI.Button(new Rect(10,10,80,20),"Show list"))

{

Debug.Log(plugin);

plugin.CallStatic("buttonClicked");

}

    //....................

}

 

That’s it! Hopefully if you complete these steps, then by pressing the button the buttonClicked function will be called and the main activity of your application will start. This example will just toast a message but you can write your things in the activity!


I hope i didn’t miss anything.. But in any case i will keep this post updated, as i walk it through and finding new things.