1

I am new to Unity. I have an Android library jar that I want to offer also as a plugin with Unity.

Within the library jar I have a folder html which contains a file temp.json. In the jar file (if I unzip it) the structure is like this: html/temp.json (Note here that jar works fine when running with any Android app outside Unity)

These are the steps that I followed to call my library through Unity:

1) Created folder Assets/Plugins/ Android in my Unity project hierarchy

2) Placed my library jar file there along with AndroidManifest.xml

3) Created a bridge.cs file that I use to call the functions in the jar file

Functions from the jar are called, but within the code of the jar somewhere on the Android side during the library lifecycle I call:

InputStream inputStream = MyClass.class.getClassLoader()
.getResourceAsStream("html/temp.json");

and I get a ResourceNotFound exception

From what I have read Unity ignores these files(like .json) when packaging. Therefore from what I understood in order to keep them in the Jar when packaging I created the following Unity structure:

Assets/Plugins/Android/assets/StreamingAssets/html/temp.json

I realise that any file that is placed within the StreamingAssets folder get copied in the assets folder of the library.

and in Java I call now:

inputStream = activity.getResources().getAssets().open("html/temp.json");

However I still get an exception that the file is not found.

Can anyone please help me/explain to me the procedure I have to follow to be able to read these files on the Java side while executing on Unity for Android?

twlkyao
  • 14,302
  • 7
  • 27
  • 44
andreasv
  • 689
  • 3
  • 11
  • 26
  • Can you tell me in which package `MyClass` is? – Mobiletainment Jan 07 '14 at 09:27
  • MyClass is the library class. This library works fine as a library in any Android project. The problem occurs only in Unity – andreasv Jan 07 '14 at 14:47
  • Did you try putting the json file in the resources folder in unity ? Because " all assets in the "Resources" folders will be included in a build."[link]http://docs.unity3d.com/Documentation/ScriptReference/Resources.html – Rafael F. Jan 08 '14 at 20:15
  • http://stackoverflow.com/questions/7835294/how-can-i-get-resources-from-jar-file – Tobrun Jan 09 '14 at 12:46
  • The only way to make it included when building in Unity as far as I achieved so far is by placing the res/ folder in tour Plugin/Android/ folder when building. However this constrains a lot the thing you can done with your project structure. – andreasv Jan 09 '14 at 13:12

1 Answers1

0

Try this code:

    private void readJson() {
    String json = null;
            try {

                InputStream is = getAssets().open("html/temp.json");

                int size = is.available();

                byte[] buffer = new byte[size];

                is.read(buffer);

                is.close();

                json = new String(buffer, "UTF-8");


            } catch (IOException ex) {
                ex.printStackTrace();
            }
    }

And save your json file to:

Assets/Plugins/Android/assets/html/temp.json

If you still can not do it, plz contact me

Br,

Frank

Frank Nguyen
  • 6,493
  • 3
  • 38
  • 37
  • Hello Frank thank you for the answer. I have already tried that. What seems to be the problem is that assets folder is not included when building the plugin, it is completely ignored... – andreasv Jan 10 '14 at 10:57
  • Assets folder will be included, for sure. You can check by change your file.apk -> yourfile.zip and then unzip. You can see assets folder. – Frank Nguyen Jan 10 '14 at 11:01
  • Yes Frank, assets folder is included, but seems not to be visible to the java code in the library since they cannot be references. The code above cannot track the file. – andreasv Jan 10 '14 at 16:01
  • Maybe you forgot something, check again. Because I checked and I can do it easily with above code. – Frank Nguyen Jan 11 '14 at 15:46