带有多个jar的Unity3D(android jar + pure java lib) [英] Unity3D with multiple jars (android jar + pure java lib)
问题描述
我是Unity3D的新手,我已经用Java完成了一些项目,这些项目需要包含在我的Unity3D项目中.
I'm new to Unity3D and I have some projects already done with Java which need to be included in my Unity3D project.
我想在Unity3D项目中添加多个jar,但是当它们在Unity项目中运行时,它们之间似乎没有任何依赖关系.
I want to add multiple jars into Unity3D project but it looks like jars are not have any dependency between them when they are running in Unity project.
为此,
-
我试图在Assets/Plugins/Android文件夹中添加所有相关的jar(该文件夹中有一些纯Java jar文件,其中有一个Android jar来调用jar的方法)
I tried to add all the jars related in Assets/Plugins/Android folder (Multiple some pure java jar files in the folder with an Android jar to invoke the methods of jars)
我试图只制作一个jar文件(通过创建一个包含所有jar的Android项目)
I tried to make only one jar file (By creating an Android project with all the jars included)
使用这两种方法,我可以将项目构建为基于Android的项目,但是它们显示错误,表明相关的库不存在
With these 2 methods, I can build my project as Android based, but they showed errors say related library is not exist
有什么办法可以将多个jar库用于依赖于其他jar的Unity3D项目中?
Is there any way I can use multiple jar library into Unity3D project with dependency to other jars?
换句话说,我想要这种结构
In other words, I want this structure
|主项目(单位)| --dependency-> | Jar lib 1 | --dependency-> | Jar lib 2 |
| Main Project(Unity) | --dependency--> | Jar lib 1 | --dependency--> | Jar lib 2 |
- Jar lib 1:库我需要直接调用方法
- lib lib 2:lib 1使用的库
推荐答案
有什么办法可以将多个jar库用于Unity3D项目 依赖其他罐子?
Is there any way I can use multiple jar library into Unity3D project with dependency to other jars?
否.
如果您有许多相互依赖的jar库,则必须将它们组合为一个.是的,必须将罐子合并为一个,然后您可以建立一个桥梁,Unity可以使用它来访问它们.
If you have many jar libraries that depend on each other, you have to combine them into one. Yes, the jars must be merged into one, then you can make a bridge that Unity can use to access them.
Jar lib 1:库我需要直接调用方法
Jar lib 1: Library I need to call method directly
Jar lib 2:lib 1使用的库
Jar lib 2: A library which lib 1 uses
在这种情况下,您必须在库1 中导入库2 .然后在库1 中创建函数,然后调用Jar 库2 中的函数.然后,您可以构建库1 和库2 .
In this scenario, you have to import the Lib 2 in Lib 1. Then make functions in Lib 1 that calls functions in Jar Lib 2. You can then build Lib 1 and Lib 2.
在Eclipse中,项目-> 全部构建.然后将它们都导出到一个jar文件中.同样,在Eclipse中,文件-> 导出... -> Java -> JAR文件 .接下来,如果两个库都出现,则选择它们.使用以下设置:
In Eclipse, Projects->Build All. Then export them both into one jar file. Again, in Eclipse, File->Export...->Java->JAR file. Next, then select both Libs if they show up. Use the settings below:
Java:
//来自Lib/Jar的东西 2 (假设其中是功能代码)
//Stuff from Lib/Jar 2 (Lets assume this is the function code in it)
public class Lib2{
public static Lib2 instance = new Lib2();
public double getRandomNumber() {
double advancedRandomNumber = 5;
return advancedRandomNumber;
}
}
//来自Lib/Jar 1 ( com.roxy.app )包的东西(假设其中是功能代码)
//Stuff from Lib/Jar 1 (com.roxy.app) package (Lets assume this is the function code in it)
public class Lib1{
public static Lib1 instance = new Lib1();
public double getMagicRandomNumber() {
double advancedRandomNumber = 15;
return advancedRandomNumber;
}
//Bridge to access Stuff from jar 2
public double getExtraMagicRandomNumber()
{
return Lib2.instance.getRandomNumber();
}
}
C#:
private AndroidJavaClass androidClass;
private AndroidJavaObject androidFunction;
#if UNITY_ANDROID
if (androidClass == null || androidFunction == null)
{
androidClass = new AndroidJavaClass("com.roxy.app.Lib1");
androidFunction = androidClass.GetStatic<AndroidJavaObject>("instance");
}
#endif
然后您可以使用以下方法从Lib1
调用getMagicRandomNumber()
函数:
Then you can call getMagicRandomNumber()
function from Lib1
with:
double adRand = androidFunction.Call<double>("getMagicRandomNumber");
然后从Lib1
中的桥接函数中调用Lib2
中的函数.
And call function from Lib2
from the bridged function made in Lib1
.
double adRand = androidFunction.Call<double>("getExtraMagicRandomNumber");
就是这样.并不难,但这可能不进行编译,因为我直接在此编辑器中键入了它.可能需要进行少量修改.
That's it. It's not hard but this may not compile because I typed it directly to this Editor. Minor modification may need to be made.
这篇关于带有多个jar的Unity3D(android jar + pure java lib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!