Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_0coc8MqFnp74NA · Nov 21, 2019 at 04:59 PM · native plugin

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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...

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_0coc8MqFnp74NA · Nov 22, 2019 at 05:44 PM 0
Share

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

avatar image unity_0coc8MqFnp74NA unity_0coc8MqFnp74NA · Nov 25, 2019 at 03:55 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges