Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

How to reference a class (type) defined in .aar library, in method declaration?

Discussion in 'Scripting' started by unity_V16sPb0I8SVOuw, Aug 28, 2019.

  1. unity_V16sPb0I8SVOuw

    unity_V16sPb0I8SVOuw

    Joined:
    Apr 1, 2019
    Posts:
    10
    Hello,

    I have a MyLocation class defined in a Java project compiled to an .aar file. I need to pass the MyLocation type as a function parameter in my Unity classes's declaration. E.g.

    Code (CSharp):
    1. public class MyClass {
    2.  
    3.      private MyLocation loc;
    4.      public MyClass() {}
    5.  
    6.      public doSomething(int arg1, bool arg2, MyLocation arg3){
    7.           loc = arg3;
    8.      }
    9. }
    I am aware that I can create an object of type MyLocation using AndroidJavaObject but here I need to specifically point to the class type in the method declaration.

    Do you know how I can achieve this?

    Many thanks!
     
    anycolourulike likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    31,167
    If you don't use the AndroidJavaObject method, then you can instead write your own wrapper for the MyLocation class in C# and then put translation functions into it to access the .aar file via standard C# interop calls, just making it a hollow shell that maps from C# into Java.
     
    anycolourulike likes this.
  3. unity_V16sPb0I8SVOuw

    unity_V16sPb0I8SVOuw

    Joined:
    Apr 1, 2019
    Posts:
    10
    Can you provide an example of how I may be able to do that?

    To be more specific about my problem, I am trying to pass java class type defined in .aar into my C# AndroidJavaProxy interface override method declaration as a parameter. I have a MyInterface Java interface and MyLocation Java class defined in a project compiled to an .aar file.

    I need to pass the MyLocation type as a function parameter in my Unity interface implementation's onLocationReceived method declaration, so that I can override the interface's onLocationReceived (MyLocation loc) callback method.

    I have tried overriding it with onLocationReceived(object o) and onLocationReceived(AndroidJavaClass ajc) but the method is not recognised as an override - I know this because it does not exhibit the expect behaviour of the callback. Therefore, I've concluded that it has to be the exact same class type for it to work. I cannot modify the .aar as it is not my product.

    My code in android:
    Code (CSharp):
    1. public class MyLocation { ... }
    2.  
    3. public interface MyInterface {
    4.       public void onLocationReceived(MyLocation loc)
    5. }
    Unity:
    Code (CSharp):
    1. public class MyUnityInterfaceImplementation : AndroidJavaProxy {
    2.  
    3.      public MyUnityInterfaceImplementation () : base ("..") {}
    4.  
    5.      public onLocationReceived(MyLocation loc){ // Error: MyLocation not defined in current context
    6.           // AndroidJavaObject obj = loc;
    7.           // loc.Call(...);
    8. }
     
    anycolourulike likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    31,167
    I've not done much with Android specifically, but it might be you have to write some Java side code of your own to call the AAR file for you and marshal it back to you via return of another polled function.

    Generally the less you have going on between the C# and the native layer the better. Try to wrap the the third party interface with something that lets you get at it without callbacks across that boundary.

    As for the specific how-tos, there are scads of examples scattered around google. I wouldn't know which one is more current or more relevant so it really is something you're going to have to research. There is a lot of stuff to look through:

    https://stackoverflow.com/questions/9731990/calling-c-sharp-code-from-unmanaged-c
     
    anycolourulike likes this.
  5. unity_V16sPb0I8SVOuw

    unity_V16sPb0I8SVOuw

    Joined:
    Apr 1, 2019
    Posts:
    10
    I've tried implementing it with pointers but the function within the .aar would not accept the arguments. In the end I've had to create my own .aar that implements the function and create a custom interface with which I can work in Unity.
     
    anycolourulike likes this.