- Home /
Accessing config file from C/C++ android native plugin
Hello,
I need to implement some C library as a Unity native plugin for Android. This library uses a config file with some params. I cannot filnd a way how to access some file inside apk inside C plugin. For instance this example https://en.wikibooks.org/wiki/OpenGL_Programming/Android_GLUT_Wrapper#Accessing_assets allowing to access StreamingAssets files requires 'activity' variable of ANativeActivity type with AssetManager object. this activity variable is passed in android_app struct as a param of android_main.
But we dont have access to it inside a unity plugin, do we? How to get AssetManager or ANativeActivity object inside a native plugin?
Answer by unity_0coc8MqFnp74NA · Nov 21, 2019 at 08:33 PM
Seems the needed assetManager object could be retrieved from the getAssets() method of an object derived from UnityPlayerActivity java object. Unity provides docs how to create such own activity object...
Oh! I put my config file in /Assets/Strea$$anonymous$$gAssets/data dir And I was able to read it in my c++ plugin with this code:
// it is important to extend UnityPlayerActivity and retrieve asset$$anonymous$$anager there with getAssets()
JNIEXPORT jint JNICALL
Java_com_DefaultCompany_TestAndroidSO_$$anonymous$$yUnityPlayerActivity_loadConfigFile(JNIEnv* env, jclass clazz, jobject asset$$anonymous$$anager)
{
char fname[] = "data/config";
// assu$$anonymous$$g asset$$anonymous$$anager is passed from java
AAsset$$anonymous$$anager* mgr = AAsset$$anonymous$$anager_fromJava(env, asset$$anonymous$$anager);
// skipped error checking for brevity
AAsset* asset = AAsset$$anonymous$$anager_open(mgr, fname, AASSET_$$anonymous$$ODE_STREA$$anonymous$$ING);
size_t len = AAsset_getLength(asset);
char *bufp = malloc(len + 1);
size_t nread = AAsset_read(asset, bufp, len);
bufp[nread] = '\0';
// print bufp:
__android_log_print(ANDROID_LOG_INFO, "myplugin", "file content: %s, bufp);
free(bufp);
AAsset_close(asset);
return 0;
}
And this is $$anonymous$$yUnityPlayerActivity.java: I just put it into Assets/java dir and it was compiled by Unity itself:
package com.DefaultCompany.TestAndroidSO;
import com.unity3d.player.*;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.$$anonymous$$eyEvent;
import android.view.$$anonymous$$otionEvent;
import android.view.View;
import android.view.Window;
import android.view.Window$$anonymous$$anager;
import android.util.Log;
import android.content.res.Asset$$anonymous$$anager;
public class $$anonymous$$yUnityPlayerActivity extends UnityPlayerActivity
{
// Setup activity layout
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.i("$$anonymous$$yUnityPlayerActivity", "Overridden $$anonymous$$yUnityPlayerActivity onCreate called!");
Asset$$anonymous$$anager asset$$anonymous$$anager = getAssets();
int rc = loadConfigFile(asset$$anonymous$$anager);
Log.i("$$anonymous$$yUnityPlayerActivity", "loadConfigFile rc=" + rc);
}
public static native int loadConfigFile(Asset$$anonymous$$anager asset$$anonymous$$anager);
/** Load jni .so on initialization */
static {
System.loadLibrary("plugin");
}
}
Your answer
Follow this Question
Related Questions
Use native plugin to do heavy job? 1 Answer
pass texture2d or image to native android 0 Answers
Native type conversions 0 Answers
Which DXGI_FORMAT is supported by Unity's DDS Importer 0 Answers
FBO always 0 in native plugin IOS 0 Answers