Plugins

Alright, I hate asking for stuff like this, but I’m at a total loss when it comes to where to start with the new plugins stuff. I’m pretty sure I’m not the only one.

I know it was supposed to be easier to use now, but I just don’t where to begin learning it. There’s still not a lot of documentation on the Android side of things, and the only plugin documentation I believe is referring to how it used to work. Any tutorials or examples anyone can point to? I’d really like to get admob working, as a first step.

I’d also like to know. I’ve read through this: http://unity3d.com/support/documentation/Manual/Plugins.html#AndroidPlugins and downloaded the sample projects, however I’m still at a loss. I think this might be due to my lack of knowledge on Android development without Unity. It’s not clear to me what build_plugins.sh is doing, nor what’s inside libjavabridge.so. Is it possible to use plugins to pause Unity and switch to a web view or take an in-game screenshot?

Just done the admob on and off, it is a bunch of work, have a look at the openfeint wrapper.

On the java side I have 3 functions in the overridden activity (could have been done with 2)

        private Handler handler = new Handler() 
	{
         public void  handleMessage(Message msg) 
         {
        	 switch (msg.what)
        	 {
        	 case 0:
        		 if (adVisible)
        		 {
        			 adView.setVisibility(View.INVISIBLE);
        			 adView.startAnimation( hideAnimation );
        			 adVisible = false;
        		 }
        		 break;
        	 case 1:
        		 if (!adVisible)
        		 {
        			 adView.setVisibility(View.VISIBLE );
        			 adView.startAnimation( showAnimation );
        			 adVisible = true;
        		 }
        		 break;
        	 default:
        		 break;
        	 }
         }
    };
	
	public void EnableAds()
	{
		handler.sendEmptyMessage(1);
		//adView.setVisibility(View.VISIBLE );
	}
	
	public void DisableAds()
	{
		handler.sendEmptyMessage(0);
		//adView.setVisibility(View.INVISIBLE );
	}

And then on the c# side, some JNI code to call them

    public void EnableAds()
    {
#if UNITY_ANDROID
		JavaVM.AttachCurrentThread();

		// first we try to find our main activity..
		IntPtr cls_Activity = JNI.FindClass("com/unity3d/player/UnityPlayer");
		int fid_Activity = JNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;");
		IntPtr obj_Activity = JNI.GetStaticObjectField(cls_Activity, fid_Activity);
		//Debug.Log("obj_Activity = " + obj_Activity);

		IntPtr cls_OurAppNameActivityClass = JNI.FindClass("com/DefiantDev/OurAppName/OurAppNameActivity");

		int startAdsMethod = JNI.GetMethodID(cls_OurAppNameActivityClass, "EnableAds", "()V");
		//Debug.Log("m_startAdsMethod = " + startAdsMethod);

		if (JNI.IsInstanceOf(obj_Activity, cls_OurAppNameActivityClass) != 0)
		{
			//Debug.Log("Activity IS a OurAppNameActivity");

			JNI.CallVoidMethod(obj_Activity, startAdsMethod);
		}
#else
        m_adShowing = true;
#endif //UNITY_ANDROID
    }

	
	public void DisableAds()
    {
#if UNITY_ANDROID
		JavaVM.AttachCurrentThread();

		// first we try to find our main activity..
		IntPtr cls_Activity = JNI.FindClass("com/unity3d/player/UnityPlayer");
		int fid_Activity = JNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;");
		IntPtr obj_Activity = JNI.GetStaticObjectField(cls_Activity, fid_Activity);
		//Debug.Log("obj_Activity = " + obj_Activity);

		IntPtr cls_OurAppNameActivityClass = JNI.FindClass("com/DefiantDev/OurAppName/OurAppNameActivity");

		int stopAdsMethod = JNI.GetMethodID(cls_OurAppNameActivityClass, "DisableAds", "()V");
		//Debug.Log("m_startAdsMethod = " + stopAdsMethod);

		if (JNI.IsInstanceOf(obj_Activity, cls_OurAppNameActivityClass) != 0)
		{
			//Debug.Log("Activity IS a OurAppNameActivity");

			JNI.CallVoidMethod(obj_Activity, stopAdsMethod);
		}
#else //UNITY_ANDROID
        m_adShowing = false;
#endif //UNITY_ANDROID

    }

DTreble thanks for your example.

I thing I manage java part. But I have problem with C# part.

The type or namespace name `IntPtr' could not be found. Are you missing a using directive or an assembly reference?

Can you explain c# part little more to me?

Thanks

Yea, it looks like you’re missing a namespace declaration.

Adding this at top of your script, should do, since the IntPtr class belongs to the System namespace

using System;

Thanks. I tried that, but then I get:

The name `JavaVM' does not exist in the current context
The name `JNI' does not exist in the current context

Any other tips?

That code relies on the libjni.so, JavaVM.cs and JNI.cs found in the OLD openfeint example. These resources have since been rolled into unity and you can structure your calls to use the new internal ones.

Although I haven’t checked, the new openfaint, admob or mobclix examples should use the new calling convention. I would check them for an example. I haven’t personally used it yet.

Thanks

Dan

Thanks! Its working now!!! :slight_smile:

Hey guys,
I am working along the same lines but I am having loads of problems making it work.I am using a java plugin in my project and trying to call it with the help of JNI.I am not getting any errors,but the app is not opening in the Experia.I have attached my code snippet.The function I am calling from java takes no arguments and returns an integer.Plzzzzz help.

My bridge,
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System;

public class CallJavaCode : MonoBehaviour {

private IntPtr JavaClass;
private int getFlag;
void Start ()
{
// attach our thread to the java vm; obviously the main thread is already attached but this is good practice…
JavaVM.AttachCurrentThread();

// first we try to find our main activity…
IntPtr cls_Activity = JNI.FindClass(“com/unity3d/player/UnityPlayer”);
int fid_Activity = JNI.GetStaticFieldID(cls_Activity, “currentActivity”, “Landroid/app/Activity;”);
IntPtr obj_Activity = JNI.GetStaticObjectField(cls_Activity, fid_Activity);
Debug.Log("obj_Activity = " + obj_Activity);

// create a JavaClass object…
IntPtr cls_JavaClass = JNI.FindClass(“org/example/ScriptBridge/JavaClass”);
int mid_JavaClass = JNI.GetMethodID(cls_JavaClass, “”, “(Landroid/app/Activity;)V”);
IntPtr obj_JavaClass = JNI.NewObject(cls_JavaClass, mid_JavaClass, obj_Activity);
Debug.Log("JavaClass object = " + obj_JavaClass);

// create a global reference to the JavaClass object and fetch method id(s)…
JavaClass = JNI.NewGlobalRef(obj_JavaClass);
getFlag = JNI.GetMethodID(cls_JavaClass, “getFlag”, (Ljava/lang/void;)Ljava/lang/int;);

int flagId=JNI.GetFieldID(cls_JavaClass, “flag”, “()Ljava/lang/int;”);
Debug.Log("JavaClass global ref = " + JavaClass);
Debug.Log("JavaClass method id = " + getFlag);
}

private string cacheDir = “view my car”;
void OnGUI ()
{
if (GUI.Button(new Rect (15, 125, 450, 100), cacheDir))
{
Debug.Log("getFlag calling ");
int cache=getMyFlag();
cacheDir=“My flag is”+cache;
Debug.Log("getCacheDir returned ");
}
}

private int getMyFlag()
{
// again, make sure the thread is attached…
JavaVM.AttachCurrentThread();

// get the Java String object from the JavaClass object
int myFlag= JNI.CallIntMethod(JavaClass,getFlag);
Debug.Log(“Step 1”);

// convert the Java String into a Mono string
// IntPtr stringPtr = JNI.GetStringUTFChars(str_cacheDir, 0);
Debug.Log(“Step 2”);
// String cache = Marshal.PtrToStringAnsi(stringPtr);
//JNI.ReleaseStringUTFChars(str_cacheDir, stringPtr);

Debug.Log(“Step 3”);

return myFlag;
}

}

My java class,

package org.example.ScriptBridge;

import android.app.Activity;

public class JavaClass
{
static int f = 5;
private Activity mActivity;
public JavaClass(Activity currentActivity)
{
mActivity = currentActivity;
}
public void setFlag()
{
f++;
}
public int getFlag()
{ //int cacheDir = mActivity.getMyFlag();

return f;
}

}

Plzzzzzzzzzzzzzzzzz help!!!

My error is coming in:
JavaVM.AttachCurrentThread();

I got $10 on your java signiture not matching your player signature.

File structure:
Plugins/Android/src/com/something/othersomething/myactivity.java

Player settings:
com.somethingcompletlydifferent.myawesomeapp

Hey,
Thanks for the reply DTreble!!!But no,that is not the problem.When I am playing the app in unity3d,it is throwing “DLLNotFoundException:jni”.
The control is jumping from ‘JavaVm.AttachCurrentThread’ to ‘void GUI’.So I think the FindClass,GetMethodId etc may not be getting executed.And when I comment ‘JavaVm.AttachCurrentThread’ ,the exception is moving to next line.What do I do:(?

Yeah, it wont work in editor at all, this is to be expected. Get a adb logcat on device, that will yield more clues.

When I run it on the device its showing some FMOB error etc.Any idea what that could be?

Hi, I have another problem. Hiding and displaying ads is working. But when the admob ad is picture and when it is hidden, the area where is normally displayed is somehow active. I cant get touch from unity in that area. Can anyone help?

I had the same problem, but I used the new AdMob (Google AdMob) SDK, and now everything works fine.

yes as wipeer said just update the AdMob SDKs

Does anyone have the working SDK for releasing the touch area occupied by admob. The newest SDK doesn’t work :frowning:

Would be appreciated if someone could post a link :slight_smile:

Would you mind to post the new code?

I’m not using the code provided in this example, but I’m having the same problem, anyway.
I’ve tried to replace the “JNI.” to “AndroidJNI.”, but more errors appear.

Any help with it, please? I’m really stuck trying to implement JNI.
I’ve tried several ways, but I guess I can’t get it running.

Same request here, if you please. Just can’t seem to make this work in Unity 4.1.