- Home /
Calling Android NDK function from Unity Script
So I'm creating an Android app that uses Unity ... I am getting some assetbundle from Unity but I do not know the url before Unity starts. Thus Unity needs to call a function on the Native (Android) side to get the url.
I have been lost for awhile on how to do this (the Unity documentation is quite terrible imho). I decided to use the NDK to help me out. Without Unity everything about my library file works out... now the issue is calling these C functions in Unity.
Here is my lib:
#include <jni.h>
include
include
define DEBUG_TAG "NDK_blahy"
extern "C" {
jstring Java_com_blah_blah_getURL(JNIEnv * env, jobject this);
void Java_com_blah_blah_setURL(JNIEnv * env, jobject this, jstring URL);
}
jstring url;
void Java_com_blah_blah_setURL(JNIEnv * env, jobject this, jstring URL)
{
url = URL;
jboolean isCopy;
const char * szLogThis = (*env)->GetStringUTFChars(env, URL, &isCopy);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis);
(*env)->ReleaseStringUTFChars(env, URL, szLogThis);
}
jstring Java_com_lyfelotto_blah_blah_getURL(JNIEnv * env, jobject this)
{
return url;
}
My unity code loads the library just fine (using [DllImport ("libname")]).
Now, if I load the function "correctly" like this private static extern jstring Java_com_lyfelotto_blah_blah_getURL(JNIEnv * env, jobject this)
bad things happen
Have I gone about this the wrong way? (Like I said, all I need to do is get a string). Any ideas?
Your answer
Follow this Question
Related Questions
Unity editor scripts stopped working. 0 Answers
Multiple Plugins on Android 3 Answers
Plugin Android that doesn't work anymore in Unity 4.3 0 Answers
How to add i-ad for android unity game? 0 Answers
Android native plugin question 1 Answer