6

I have code in Unity that calls a function programmed in Java for Android using AndroidJavaClass and AndroidJavaObject and I would like to pass this code to a dll in C# .Net but I can't find an easy way to call Java functions using C# .Net since the AndroidJavaObject and AndroidJavaClass classes are exclusive to the Unity dll, here is an example of my code:

Java (Android):

public class DeviceManager {
    public interface DeviceListener {
        void DoSomething();
    }

    private DeviceListener deviceListener;

    public DeviceManager(DeviceListener deviceListener){
        this.deviceListener = deviceListener;
    }
    
    public String[] getSomeData(){
        return new String[]{};
    }
}

C# (Unity):

public class DeviceListener : AndroidJavaProxy
{
    DeviceManager deviceManager;

    public DeviceListener(DeviceManager manager) : base("com.myapp.DeviceManager$DeviceListener")
    {
        this.deviceManager = manager;
    }

    public void DoSomething();
}

public class DeviceManager
{
    private AndroidJavaObject jo;

    public DeviceManager(){
         jo = new AndroidJavaObject("com.myapp.DeviceManager", new DeviceListener(this));
    }

    public string[] GetData(){
        AndroidJavaObject returnedData = jo.Call<AndroidJavaObject>("getSomeData");
        return AndroidJNIHelper.ConvertFromJNIArray<string[]>(returnedData.GetRawObject());
    }
}

What is the best way to do that example in C# .Net instead of Unity?

EDIT: my main question is related to know how to get objects and call functions from an .aar file in .NET Framework inside a dll C# file for Unity

EDIT 2: I found some information about Xamarin, maybe that is what I am looking for?: https://github.com/xamarin/monodroid-samples/tree/master/JavaIntegration/AarBinding But even though I could link an .aar file to a dll in C# that doesn't quite answer my question in the best way to create a code that works the same as the example described above

EDIT 3: Currently, to solve this problem, I am using the UnityEngine .dll in my Visual Studio project but I want to generate a .dll for use in other graphics engines so I would like to be able to find a different solution than using Unity's own .dll for solve this problem, hence I mentioned Xamarin since it is a good way to communicate .Net Framework with Android (Java) but I have not found any example that uses it outside of a mobile application created in Visual Studio since in my case I would need to obtain objects and call functions from the .aar file

2
  • Which scripting backend are you targeting? il2cpp or mono? Commented Sep 30, 2021 at 15:50
  • @HamidYusifli I am not actually sure, in Unity i have selected Mono in player settings > scripting backend with .Net standard 2.0 api compatibility level, but maybe it is useful to get also also a solution for il2cpp? I currently have code in C++ dll and then it is used by a C#(.Net framework) dll and Unity read that C# Wrapper dll, but I want that the .aar could be read also in the C# dll so Unity only need to call functions through that file. Commented Oct 1, 2021 at 7:37

2 Answers 2

1

It seems like you are looking to use an AAR plugin, I haven't done it myself, but Unity has a doc on how to integrate it in your project.

Then, once that library is compiled in Android studio and integrated in Unity, you can look at this example on how to call it from C# code.

I haven't done it myself, but the example should put you on the right track.

Regarding the scripting backend, I don't believe it will have any impact on this.

Edit Added Extract from the Example

Assets / Plugins / Android Place your native android .AAR file inside this folder. Now let’s write some code to call the plugin.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NativeCodeRunner : MonoBehaviour
{
    void Start(){
    CallNativePlugin();
}
//method that calls our native plugin.
public void CallNativePlugin() 
{
    // Retrieve the UnityPlayer class.
    AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    // Retrieve the UnityPlayerActivity object ( a.k.a. the current context )
    AndroidJavaObject unityActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");

    // Retrieve the "Bridge" from our native plugin.
    // ! Notice we define the complete package name.              
    AndroidJavaObject alert = new AndroidJavaObject("plugins.vsoft.com.library.Alert");

    // Setup the parameters we want to send to our native plugin.              
    object[] parameters = new object[2];
    parameters[0] = unityActivity;
    parameters[1] = "Hello World!";

    // Call PrintString in bridge, with our parameters.
    alert.Call("PrintString", parameters);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I have already an .aar plugin, what I'm really looking for is to communicate a dll in C# .Net framework with an .aar plugin so that in Unity I only have to communicate with the dll because my main objective is not to integrate only with Unity but also with other different platforms
@MaríaRomán Did you read this section from the Example? Assets / Plugins / Android Place your native android .AAR file inside this folder. Now let’s write some code to call the plugin. It seems pretty much what you want. Maybe I misunderstand what you're trying to achieve...
@MaríaRomán Ahh, I think I understand now, you want your DLL to include the communication with the AAR so inside Unity you don't have to worry for this ? If this is correct, then what you need is a Unity Package (UPM) but with a C# DLL compiled in it, on which you'd have made the code that communicates with the AAR.
Yes, I am currently doing that but being a dll that could be used in other graphics engines I would like to leave out the use of the same UnityEngine.dll in the visual studio project
@MaríaRomán Ok so then, yes you need to compile this DLL in a visual studio project that has nothing to do with Unity. Then include that DLL either in Unity or in another engine, like Xamarin or Unreal (haven't used unreal myself).
1

my main question is related to know how to get objects and call functions from an .aar file in .NET Framework inside a dll C# file for Unity

If you want to use AndroidJavaClass or AndroidJavaObject methods in your custom DLL, you should add references to the Unity DLLs. In Visual Studio, open the contextual menu for References in the Solution Explorer and select Add Reference. Then, select Browse > Select File.

At this stage, select the required .dll file, located in the UnityEngine folder.

using System; 
/* Unity */  
using UnityEngine;

namespace DLLTest {

    public class MyDLLTest {

    
    }
}

1 Comment

Yes, that is a good solution and that is what I am doing now but I would like to use the dll on platforms other than Unity, so I commented on Xamarin since it is a possible option to communicate .Net framework with Android, the problem is that I cannot find good examples for external use to a mobile application in visual studio but rather simple query calls for a dll and thus use this dll as an intermediate node in Unity or any other framework

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.