unity多人java,具有多个jar的Unity3D(android jar + pure java lib)

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:

NHQ6T.png

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值