简体   繁体   中英

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 <string.h>  
#include <android/log.h>  

#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?

I suggest ditch the NDK. Use the Android java plugin.

In YourPlugin.cs:

using UnityEngine;
using System.Collections;
using System.IO;

#if UNITY_ANDROID
public class UnityUrlPlugin {

  // Logs the user out and invalidates the token
  public static string getUrl() {
    if( Application.platform != RuntimePlatform.Android )
      return;

    var pluginClass = new AndroidJavaClass("com.you.UnityUrlPlugin") ;
    AndroidJavaObject plugin = pluginClass.CallStatic<AndroidJavaObject>("instance");
    return plugin.Call<string>("getURL");
  } 
}
#endif

Then, in UnityUrlPlugin.java:

package com.you;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Environment;

public class UnityUrlPlugin {

  private static UnityUrlPlugin m_instance;

  public static UnityUrlPlugin instance() {
    if(m_instance == null)
      m_instance = new UnityUrlPlugin();
    return m_instance;
  }

  private UnityUrlPlugin(){
  }

  public String getURL() {
    return "http://blah.com";
  }
}

And throw UnityUrlPlugin.jar in to Assets/Plugins/Android folder.

No need for NDK!

You could likely send the 'dynamic' url in your call to getURL. Do something like:

return plugin.Call<string>("getURL", new object[] {"http://thisIsMyUrl.com"});

and then your java looking like this:

public String getURL(Object newUrl) {
    return newUrl.ToString();
}

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