首页 > 代码库 > Unity3D 调用Java,Java调用Unity3D

Unity3D 调用Java,Java调用Unity3D

1,无返回值
AndroidJavaClass fee = new AndroidJavaClass("com.
wiker.Test");

fee.CallStatic("print", "Hello", "World");

 

 

Java源码

package com.wiker

 

public class Test{

 

public static void print(String str1,String str2){

System.out.println(str1);

System.out.println(str2);

}

}

 

java程序将打印

Hello

World

 

2,有返回值

AndroidJavaClass fee = new AndroidJavaClass("com.wiker.Test");

stringstr = fee.CallStatic<string>("getStr","Hello", "World");

Debug.log(str);

 

Java源码

package com.wiker

 

public class Test{

 

public static void getStr(String str1,String str2){

return str2+" "+str2;

}

}

 

Unity3d控制台将打印Hello world

 

 

Java调用Unity3d

UnityPlayer.UnitySendMessage("MainEvent","Success", "");

将调用MainEvent中的绑定的脚本中的Success方法

MainEvent是空的GameObject,绑定一个脚本
 

 

一般Unity调用Java的时候如果需要回调则会用到Java调用Unity3D,如调用支付宝等

Unity3D 调用Java,Java调用Unity3D