I'm new to Unity3D and I have some projects already done with Java which need to be included in my Unity3D project.
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.
For this,
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)
I tried to make only one jar file (By creating an Android project with all the jars included)
With these 2 methods, I can build my project as Android based, but they showed errors say related library is not exist
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
| Main Project(Unity) | --dependency--> | Jar lib 1 | --dependency--> | Jar lib 2 |
Jar lib 1: Library I need to call method directly
Jar lib 2: A library which lib 1 uses
解决方案
Is there any way I can use multiple jar library into Unity3D project
with dependency to other jars?
NO.
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: Library I need to call method directly
Jar lib 2: A library which lib 1 uses
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.
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:
//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;
}
}
//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("instance");
}
#endif
Then you can call getMagicRandomNumber() function from Lib1 with:
double adRand = androidFunction.Call("getMagicRandomNumber");
And call function from Lib2 from the bridged function made in Lib1.
double adRand = androidFunction.Call("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.