Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNITY JNI android plugin GetApplicationContext()

I'm developing a plugin for Unity Android using JNI. I create the .jar file with my Java code, and then .so file with the c++ bridge. Everything works fine when I call my library from Unity side. So, my problem is that in several parts of my Java code, I need to access to the context of the main activity (UnityPlayer3d). I need a pointer to this activity to call the function GetApplicationContext().

How can I obtain this pointer? I think about passing the pointer from Unity to my Java class as a parameter, but first I have to get it in Unity side.

like image 285
user1782945 Avatar asked Dec 29 '25 22:12

user1782945


1 Answers

Here's some code to get the activity JNI style:

var unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var unityActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
like image 114
Eric Laberge Avatar answered Jan 01 '26 11:01

Eric Laberge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!