Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Plugins

Discussion in 'Android' started by benhigg, Nov 11, 2010.

  1. benhigg

    benhigg

    Joined:
    Aug 3, 2010
    Posts:
    34
    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.
     
  2. elveatles

    elveatles

    Joined:
    May 2, 2009
    Posts:
    147
    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?
     
  3. DanTreble

    DanTreble

    Joined:
    Aug 31, 2010
    Posts:
    590
    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)

    Code (csharp):
    1.  
    2.         private Handler handler = new Handler()
    3.     {
    4.          public void  handleMessage(Message msg)
    5.          {
    6.              switch (msg.what)
    7.              {
    8.              case 0:
    9.                  if (adVisible)
    10.                  {
    11.                      adView.setVisibility(View.INVISIBLE);
    12.                      adView.startAnimation( hideAnimation );
    13.                      adVisible = false;
    14.                  }
    15.                  break;
    16.              case 1:
    17.                  if (!adVisible)
    18.                  {
    19.                      adView.setVisibility(View.VISIBLE );
    20.                      adView.startAnimation( showAnimation );
    21.                      adVisible = true;
    22.                  }
    23.                  break;
    24.              default:
    25.                  break;
    26.              }
    27.          }
    28.     };
    29.    
    30.     public void EnableAds()
    31.     {
    32.         handler.sendEmptyMessage(1);
    33.         //adView.setVisibility(View.VISIBLE );
    34.     }
    35.    
    36.     public void DisableAds()
    37.     {
    38.         handler.sendEmptyMessage(0);
    39.         //adView.setVisibility(View.INVISIBLE );
    40.     }
    41.  
    And then on the c# side, some JNI code to call them

    Code (csharp):
    1.  
    2.     public void EnableAds()
    3.     {
    4. #if UNITY_ANDROID
    5.         JavaVM.AttachCurrentThread();
    6.  
    7.         // first we try to find our main activity..
    8.         IntPtr cls_Activity = JNI.FindClass("com/unity3d/player/UnityPlayer");
    9.         int fid_Activity = JNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;");
    10.         IntPtr obj_Activity = JNI.GetStaticObjectField(cls_Activity, fid_Activity);
    11.         //Debug.Log("obj_Activity = " + obj_Activity);
    12.  
    13.         IntPtr cls_OurAppNameActivityClass = JNI.FindClass("com/DefiantDev/OurAppName/OurAppNameActivity");
    14.  
    15.         int startAdsMethod = JNI.GetMethodID(cls_OurAppNameActivityClass, "EnableAds", "()V");
    16.         //Debug.Log("m_startAdsMethod = " + startAdsMethod);
    17.  
    18.         if (JNI.IsInstanceOf(obj_Activity, cls_OurAppNameActivityClass) != 0)
    19.         {
    20.             //Debug.Log("Activity IS a OurAppNameActivity");
    21.  
    22.             JNI.CallVoidMethod(obj_Activity, startAdsMethod);
    23.         }
    24. #else
    25.         m_adShowing = true;
    26. #endif //UNITY_ANDROID
    27.     }
    28.  
    29.    
    30.     public void DisableAds()
    31.     {
    32. #if UNITY_ANDROID
    33.         JavaVM.AttachCurrentThread();
    34.  
    35.         // first we try to find our main activity..
    36.         IntPtr cls_Activity = JNI.FindClass("com/unity3d/player/UnityPlayer");
    37.         int fid_Activity = JNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;");
    38.         IntPtr obj_Activity = JNI.GetStaticObjectField(cls_Activity, fid_Activity);
    39.         //Debug.Log("obj_Activity = " + obj_Activity);
    40.  
    41.         IntPtr cls_OurAppNameActivityClass = JNI.FindClass("com/DefiantDev/OurAppName/OurAppNameActivity");
    42.  
    43.         int stopAdsMethod = JNI.GetMethodID(cls_OurAppNameActivityClass, "DisableAds", "()V");
    44.         //Debug.Log("m_startAdsMethod = " + stopAdsMethod);
    45.  
    46.         if (JNI.IsInstanceOf(obj_Activity, cls_OurAppNameActivityClass) != 0)
    47.         {
    48.             //Debug.Log("Activity IS a OurAppNameActivity");
    49.  
    50.             JNI.CallVoidMethod(obj_Activity, stopAdsMethod);
    51.         }
    52. #else //UNITY_ANDROID
    53.         m_adShowing = false;
    54. #endif //UNITY_ANDROID
    55.  
    56.     }
    57.  
     
  4. puss

    puss

    Joined:
    Mar 7, 2011
    Posts:
    4
    DTreble thanks for your example.

    I thing I manage java part. But I have problem with C# part.
    Code (csharp):
    1. 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
     
  5. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    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
    Code (csharp):
    1. using System;
     
  6. puss

    puss

    Joined:
    Mar 7, 2011
    Posts:
    4
    Thanks. I tried that, but then I get:
    Code (csharp):
    1. The name `JavaVM' does not exist in the current context
    2. The name `JNI' does not exist in the current context
    Any other tips?
     
  7. DanTreble

    DanTreble

    Joined:
    Aug 31, 2010
    Posts:
    590
    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
     
  8. puss

    puss

    Joined:
    Mar 7, 2011
    Posts:
    4
    Thanks! Its working now!!! :)
     
  9. diptiananthan

    diptiananthan

    Joined:
    Jan 21, 2011
    Posts:
    21
    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, "<init>", "(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();
     
    Last edited: Mar 9, 2011
  10. DanTreble

    DanTreble

    Joined:
    Aug 31, 2010
    Posts:
    590
    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
     
  11. diptiananthan

    diptiananthan

    Joined:
    Jan 21, 2011
    Posts:
    21
    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:(?
     
  12. DanTreble

    DanTreble

    Joined:
    Aug 31, 2010
    Posts:
    590
    Yeah, it wont work in editor at all, this is to be expected. Get a adb logcat on device, that will yield more clues.
     
  13. diptiananthan

    diptiananthan

    Joined:
    Jan 21, 2011
    Posts:
    21
    When I run it on the device its showing some FMOB error etc.Any idea what that could be?
     
  14. puss

    puss

    Joined:
    Mar 7, 2011
    Posts:
    4
    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?
     
  15. wipeer

    wipeer

    Joined:
    Mar 23, 2009
    Posts:
    13
    I had the same problem, but I used the new AdMob (Google AdMob) SDK, and now everything works fine.
     
  16. Akta

    Akta

    Joined:
    Oct 15, 2010
    Posts:
    119
    yes as wipeer said just update the AdMob SDKs
     
  17. mmuller

    mmuller

    Joined:
    Nov 23, 2010
    Posts:
    92
    Does anyone have the working SDK for releasing the touch area occupied by admob. The newest SDK doesn't work :(

    Would be appreciated if someone could post a link :)
     
  18. Miyavi

    Miyavi

    Joined:
    Mar 10, 2012
    Posts:
    58
    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.
     
  19. Miyavi

    Miyavi

    Joined:
    Mar 10, 2012
    Posts:
    58
    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.
     
  20. hyliandanny

    hyliandanny

    Joined:
    Mar 15, 2012
    Posts:
    5
    Same request here, if you please. Just can't seem to make this work in Unity 4.1.