从Unity脚本中调用的Andr​​oid

从Unity脚本中调用的Andr​​oid

本文介绍了从Unity脚本中调用的Andr​​oid NDK功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我创建一个Android应用程序,使用统一...我收到从Unity一些assetbundle,但我不知道网址统一开始之前。因此,统一需要调用一个函数在本地(安卓)侧,以便获得url。

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.

我已经失去了一段时间,就如何做到这一点(统一的文档是相当可怕恕我直言)。我决定使用NDK来帮助我。如果没有团结一切关于我的库文件的作品了...现在的问题是调用这些C函数的统一。

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.

下面是我的lib目录下:

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;
}

我的团结code加载库就好了(使用 [的DllImport(LIBNAME)] )。

现在,如果我正确的加载功能,像这样私有静态外部的jstring Java_com_lyfelotto_blah_blah_getURL(JNIEnv的* ENV,jobject这一点)不好的事情发生。

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?

推荐答案

我建议沟NDK。使用Android的Java插件。

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

在YourPlugin.cs:

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

然后,在UnityUrlPlugin.java:

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";
  }
}

和中资产/插件/ Android版文件夹扔UnityUrlPlugin.jar。

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

无需NDK!

这篇关于从Unity脚本中调用的Andr​​oid NDK功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:31