Search Unity

Calling an Android method from Unity

Discussion in 'Android' started by blackDiamond, Apr 2, 2012.

  1. blackDiamond

    blackDiamond

    Joined:
    Apr 2, 2012
    Posts:
    2
    Hello All,

    I followed the link here http://unity3d.com/support/documentation/Manual/Android-Integrating Unity With Eclipse.html to integrate eclipse and unity.

    I am successfully able to run an unity from eclipse.

    What I really want to do is I have a button in Unity interface. And when that button is pressed, I want to call a function foo() in Android Activity.

    But the problem is I cannot call this function inside android Activity . My code is as follows

    Android:

    Code (csharp):
    1. public class MainActivity extends UnityPlayerActivity {
    2.     /** Called when the activity is first created. */
    3.     @Override
    4.     public void onCreate(Bundle savedInstanceState) {
    5.         super.onCreate(savedInstanceState);
    6.     }
    7.    
    8.    // call this method from unity
    9.     public static void foo(){
    10.         Log.d("TEST","Foo method is called is called");  // this method is never called
    11.        
    12.     }
    13. }

    And in Unity cSharp script in button press handler my code is
    Code (csharp):
    1.  
    2.  
    3. if(GUI.Button(new Rect(20,70,80,20), "Call Android Method")) {
    4.  
    5.         using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
    6.  
    7.             using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
    8.  
    9.                 AndroidJavaClass cls_MainActivity = new AndroidJavaClass("com.me.callActivity.MainActivity");
    10.  
    11.                 cls_MainActivity.CallStatic("Init", obj_Activity);
    12.  
    13.                 cls_MainActivity.CallStatic("foo");
    14.                
    15.  
    16.             }
    17. }
    18.  
    19.  
    My applilcation doesn't crash. But when i see the log in eclipse i get the JNI error

    JNI: Unable to find method id for 'foo' (static)

    Am I missing something? Do I have to do manually as described in
    http://randomactsofdev.wordpress.com/2011/08/19/accessing-the-android-compass-through-unity-3d/

    Thank you,
    BlackDiamond
     
  2. HazeTI

    HazeTI

    Joined:
    Jan 12, 2012
    Posts:
    83
    I think you've got one step too many in there. The current activity which you're getting is the main activity so you should be using that to call your methods.

    Try:

    Code (csharp):
    1.  
    2. if(GUI.Button(new Rect(20,70,80,20), "Call Android Method"))
    3. {
    4.     using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    5.     {
    6.         using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
    7.         {
    8.             obj_Activity .CallStatic("foo");
    9.         }
    10.     }
    11. }
    12.  
     
  3. blackDiamond

    blackDiamond

    Joined:
    Apr 2, 2012
    Posts:
    2
    Hello HazeTI,

    I did as you said and it works now.


    Thank you,
    BlackDiamond
     
    Last edited: Apr 4, 2012
  4. dttngan91

    dttngan91

    Joined:
    Nov 21, 2013
    Posts:
    80
    My issue is that I need to call startActivity in foo function (switch from unity view to another activity android). I can get context by passing parameter obj_Activity from unity to foo funtion
    Code (csharp):
    1. using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
    2.             using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
    3.                 AndroidJavaClass cls_MainActivity = new AndroidJavaClass("com.example.TestActivity");
    4.                 cls_MainActivity.CallStatic("foo", obj_Activity);
    5.             }
    6.         }
    In foo function, my code is
    Code (csharp):
    1. public static void foo(Context c){
    2.         Intent intent = c.getPackageManager().getLaunchIntentForPackage("com.example.NextActivity");       
    3.         c.startActivity(intent);
    4.     }
    It cause errors
    Code (csharp):
    1. AndroidJavaException: java.lang.NullPointerException
    2. at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <filename unknown>:0
    3. ...
    4.  
    It seems like it cannot find package name. Can anyone help me?
     
  5. dttngan91

    dttngan91

    Joined:
    Nov 21, 2013
    Posts:
    80
    Anybody can help? I really need this!
     
  6. dttngan91

    dttngan91

    Joined:
    Nov 21, 2013
    Posts:
    80
    Anyway, I have found solution. It is no need to implement java file and add it in Plugins/Android like this tutorial http://randomactsofdev.wordpress.com/2011/08/19/accessing-the-android-compass-through-unity-3d/

    You just make an activity A extends UnityPlayerActivity and add meta code in that activity in AndroidManifest.xml
    Code (csharp):
    1. <meta-data
    2.                 android:name="unityplayer.ForwardNativeEventsToDalvik"
    3.                 android:value="false" />
    If you want to call method in android from unity, simply declare these static method in activity A

    Here is my simple Unity project and Android project. My Android project has 3 activities switch between Android and Unity: WelcomeActivity (Android) <-> MainActivity (Unity) <-> PhotoActivity (Android). My Unity project has a cube moving around with two next and back button to switch screen.
    http://www.mediafire.com/download/ejfjsega9cdqxdm/UnityAndroidIntegration.zip

    Some useful tutorial here
    http://www.rbcafe.com/Softwares/Unity/Documentation/Manual/Android-Launch%20an%20Android%20Application%20from%20a%20Unity%20Application.html
     
  7. dttngan91

    dttngan91

    Joined:
    Nov 21, 2013
    Posts:
    80
    I'm sorry for the wrong source code uploaded. This is a new version has been tested
    http://www.mediafire.com/download/wdbcmx3iczl924a/UnityAndroidIntegration_updated.zip
     
  8. RaviVohra

    RaviVohra

    Joined:
    Mar 8, 2019
    Posts:
    5