在罐子库的Android文件访问时使用Unity使用不 [英] Android files in jar library not accessible when using with Unity
问题描述
我是新来统一。我有我也想作为提供使用Unity插件一个Android库JAR。
I am new to Unity. I have an Android library jar that I want to offer also as a plugin with Unity.
在库jar我有一个包含文件temp.json一个文件夹的HTML。在jar文件(如果我将它解压缩)的结构是这样的: HTML / temp.json (这里请注意,罐子正常工作与团结之外的任何Android应用程序运行时)
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)创建的文件夹中的资产/插件/ 的Android在我的统一项目层次
1) Created folder Assets/Plugins/ Android in my Unity project hierarchy
2)放在那里我的库jar文件中,沿的AndroidManifest.xml
2) Placed my library jar file there along with AndroidManifest.xml
3)创建一个 bridge.cs ,我用它来调用的函数中的jar文件的文件
3) Created a bridge.cs file that I use to call the functions in the jar file
从罐子函数称为,但库的生命周期我在通话过程罐子在Android侧某处的code内:
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");
和我得到一个ResourceNotFound例外
and I get a ResourceNotFound exception
从我已阅读团结忽略这些文件(如以.json)包装时。因此,从我为了让他们在包装我创建了以下统一的结构时,罐明白了:
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
我意识到,被放置在 StreamingAssets中的任何文件文件夹获取库的资产文件夹复制。
I realise that any file that is placed within the StreamingAssets folder get copied in the assets folder of the library.
和Java中我现在请:
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.
任何人都可以请帮我/给我解释一下我所必须遵循的能够同时为Android上统一执行读取在Java端这些文件的程序?
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?
推荐答案
试试这个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();
}
}
和您的JSON文件保存到:
And save your json file to:
Assets/Plugins/Android/assets/html/temp.json
如果您仍然无法做到这一点,PLZ与我联系
If you still can not do it, plz contact me
BR,
弗兰克
这篇关于在罐子库的Android文件访问时使用Unity使用不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!