简体   繁体   中英

Calling a method with multiple arguments in AAR from within Unity3D C# script

I've got a class with the following method:

public static int add( int a, int b ){
    return a + b;
}

and I'm trying to call it from Unity Script with

var ajc = new AndroidJavaClass( "com.mil.rfcommunitylib.BluetoothClassic" );
int result = ajc.CallStatic<int,int>( "add", new int[] { 1, 2 } );

but I get

AndroidJavaException: java.lang.NoSuchMethodError at my logcat.

What's wrong? Works with methods without arguments, so we can assume I set up everything correctly.

Instead of calling:

var ajc = new AndroidJavaClass( "com.mil.rfcommunitylib.BluetoothClassic" );
int result = ajc.CallStatic<int,int>( "add", new int[] { 1, 2 } );

you should actually use:

var ajc = new AndroidJavaClass( "com.mil.rfcommunitylib.BluetoothClassic" );
int result = ajc.CallStatic<int>( "add", 1, 2 );

If you look closely in documentation: link you will see that in your situation your function returns int so it should be javaClass.CallStatic<int>(functionName, params ...) , and you pass the arguments which the function accept as separate params after the name of the function, not as an array of the same param types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM