Unity 3D Java plugin issues

Hi I wanted to open the gallery of android to pick an image and send the path to unity using plugins in java for android. However all my attempts fail. Could someone tell me what im doing wrong?

1.- Create a class in eclipse

    package com.Areku.GIS;
    
    import Areku.GIS.R;
    import Areku.GIS.R.id;
    import Areku.GIS.R.layout;
    import android.app.Activity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
    
    	 private static final int SELECT_PICTURE = 1;
    	 
    	    private String selectedImagePath;
    	    private ImageView img;
    	 
    	    public void onCreate(Bundle savedInstanceState) {
    	        super.onCreate(savedInstanceState);
    	        setContentView(R.layout.activity_main);
    	 
    	        img = (ImageView)findViewById(R.id.ImageView01);
    	 
    	        ((Button) findViewById(R.id.Button01)).setOnClickListener(new OnClickListener() {
    	                    public void onClick(View arg0) {
    	                        Intent intent = new Intent();
    	                        intent.setType("image/*");
    	                        intent.setAction(Intent.ACTION_GET_CONTENT);
    	                        startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
    	                    }
    	                });
    	    }
    	 
    	    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	        if (resultCode == RESULT_OK) {
    	            if (requestCode == SELECT_PICTURE) {
    	                Uri selectedImageUri = data.getData();
    	                selectedImagePath = getPath(selectedImageUri);
    	                System.out.println("Image Path : " + selectedImagePath);
    	                img.setImageURI(selectedImageUri);
    	            }
    	        }
    	    }
    	 
    	    public String getPath(Uri uri) {
    	        String[] projection = { MediaStore.Images.Media.DATA };
    	        Cursor cursor = managedQuery(uri, projection, null, null, null);
    	        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    	        cursor.moveToFirst();
    	        return cursor.getString(column_index);
    	    }
    	    public static String test() {
    	    	return "here";
    	    	}
    }

2 Then I create a unity3d C# script as following.

using UnityEngine;
using System.Collections;
using System;

 public class CompassJNI : MonoBehaviour {

	static string	zValue;

	// Use this for initialization
	void Start () {
		AndroidJNI.AttachCurrentThread();
	}

	static public void StartGal() {
		using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {

			//using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic("currentActivity")) {

				AndroidJavaClass cls_CompassActivity = new AndroidJavaClass("com.Areku.GIS.MainActivity");

				//cls_CompassActivity.CallStatic("Init", obj_Activity);

				zValue = cls_CompassActivity.CallStatic<String>("test");

			}
					Debug.Log("Compass values are " + zValue.ToString());

		}
	}

Then I compile the class as .jar and place it at Plugins/Android.

And I put a simple function which is test to debug the communication but i wasn’t able to succeed.

Please Help being stuck 2 weeks already

I don’t think it will work this way

If you want to bridge calls to android java you still need a JNI (.so) as well as the Jar. C++ bridge to be able to do calls from Unity to Java you can check the samples from the documentation:

Check out the Java Plugin Example at the bottom of the page

Hope that will help

I couldnt run it i have this

DllNotFoundException: javabridge
CallJavaCode.OnGUI () (at Assets/CallJavaCode.cs:16)

Does anyone have the same problem?

I’ve always gotten my android plugins / libs to work as follows:

    1. write java libs in Eclipse
    1. include classes.jar (from Unity) in the eclipse project (project->properties->java build path->add external jar)
    1. write C to store pointers to java methods and act as a bridge
    1. place C file in eclipse project jni folder (create it if it doesn’t exist)
    1. create a make file (.mk)
    1. compile the C file via cygwin (this will create the .so)
    1. refresh the project in eclipse
    1. compile eclipse project to .jar
    1. copy jar and .so to Unity->plugins->android
    1. access the functions by name (declared in the C file compiled to the .so) in unity by

    [DllImport(“YOURLIBNAME”)]
    public static extern void YOURFUNCTIONNAME(FUNCTION PARAMS);

This may or may not be the best way but it has always worked for me.

.- Raph -

DId you find a solution? I have the same Problem, the onActivityResult never get called

Yessss !!! I found a solution…
It is an old question but it might help the others.

first, The problem:

UnityPlayer.currentActivity is The activity which starts the activity and hence is The one which will receive onActivityResult(xx).

So, I tried extending UnityPlayerActivity, UnityPlayerNativeActivity and UnityPlayerProxyActivity and defining onActivityResult in it but it didn’t worked.

Conclution: UnityPlayer.currentActivity is not any of these three.

Solution:

this is not just a solution its a Super solution because you will not need to extend unity’s Activity anymore :wink:

  1. create a Activity which works as a router b/w UnityPlayer.currentActivity and Your target Activity ( gallery for example ).

  2. Define it in your AndroidManifest file with action like this:

  3. instead of opening gallery directly you will start router activity and router will start Gallery activity.

    Intent intent = new Intent();
    intent.setAction(“androidnativeactions.Router”);
    UnityPlayer.currentActivity.startActivityForResult(intent, Router.GalleryRequest);

  4. inside Router.java → onCreate() you will start gallery activity

    Intent intent = new Intent();

    intent.setType(“image/*”);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, GalleryRequest);

  5. Now, because you have started gallery activity from Router activity you will receive onActivityResult() on Router activity instead of UnityPlayer.currentActivity. Go ahead and define onActivityResult in Router.java

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == Activity.RESULT_OK){
    if(requestCode == GalleryRequest){
    // fetch image uri or do whatever you like here

     			// send it to Unity ;)
     			UnityPlayer.UnitySendMessage("xx", "xx", "xx");
     			
     		}
     	}else{
    
     		System.out.print("Result code in router:"+resultCode);
     	}
     	
     	setResult(resultCode);
     	finish();
     }
    
  6. Note: do not forget to finish this Router Activity otherwise you will get stuck in b/w without any User Interface :stuck_out_tongue:

I am working on Android plugin with lots of features(free) and will post the code when I’m done.