- Home /
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?
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
Hi I got it eorking, it opens the gallery but i cannot return the path. Can someone please tell me what is wrong with this code?
public static void showGallery() {
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
intent = new Intent(
Intent.ACTION_PIC$$anonymous$$,
android.provider.$$anonymous$$ediaStore.Images.$$anonymous$$edia.EXTERNAL_CONTENT_URI);
UnityPlayer.currentActivity.startActivityForResult(intent, RESULT_LOAD_I$$anonymous$$AGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_I$$anonymous$$AGE && resultCode == RESULT_O$$anonymous$$ && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { $$anonymous$$ediaStore.Images.$$anonymous$$edia.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
UnityPlayer.UnitySend$$anonymous$$essage("$$anonymous$$ain Camera", "Path", picturePath);
}
}
Thanks in advance
Answer by SolidSnake · Aug 09, 2013 at 08:55 AM
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:
http://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html
Check out the Java Plugin Example at the bottom of the page
Hope that will help
Answer by linkthewise · Aug 09, 2013 at 11:29 AM
I couldnt run it i have this
DllNotFoundException: javabridge CallJavaCode.OnGUI () (at Assets/CallJavaCode.cs:16)
Does anyone have the same problem?
Answer by Raph3004 · Aug 13, 2013 at 02:33 PM
I've always gotten my android plugins / libs to work as follows:
1) write java libs in Eclipse
2) include classes.jar (from Unity) in the eclipse project (project->properties->java build path->add external jar)
3) write C to store pointers to java methods and act as a bridge
4) place C file in eclipse project jni folder (create it if it doesn't exist)
5) create a make file (.mk)
6) compile the C file via cygwin (this will create the .so)
7) refresh the project in eclipse
8) compile eclipse project to .jar
9) copy jar and .so to Unity->plugins->android
10) 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 -
Answer by zeusrami · Sep 03, 2013 at 11:31 PM
DId you find a solution? I have the same Problem, the onActivityResult never get called
Answer by _Yash_ · Apr 08, 2015 at 08:52 PM
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 ;)
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:
<activity android:name="com.yash.androidnativeactions.router.Router">
<intent-filter>
<action android:name="androidnativeactions.Router" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
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 :P
I am working on Android plugin with lots of features(free) and will post the code when I'm done.
Your answer
Follow this Question
Related Questions
How to use Android Api in Unity 1 Answer
No OpenGL Context in Unity above 2017.1.2f1 0 Answers
Using AndroidJavaClass in a java Unity3d plugin 0 Answers
Using Java Plugin to add an Android Edittext view 0 Answers
How to talk to Java plugin 0 Answers