前言
上章 android cocoscreator 相互调用(三)
这章讲述 android unity 中的java 与c# 相互调用
1 准备
win7 64
androidstudio4.1.1 unity2017.4.30
两种方式如下:
1> android 编译个插件(AAR 或 JAR) ,放到unity 工程里 \Assets\Plugins ,再由UNITY 调用
2> unity 导出android 工程 ,增加java代码由unity调用
****java 调用unity 用
UnityPlayer.UnitySendMessage(gameObject, functionName, funcParam);
******
两种 我们都试下,
2 android 插件调用(第一种方式)
1: 创建个Android 工程,在增加个 android module (点击file > new > new module)
2 : 拷贝unity classes.jar 到 上步创建的module 的libs目录下
再 右键 classes.jar add as library 弹出 点OK
因为要编译64位才选择il2cpp目录下的,mono 可不能编译64位啊
aar 代码如下
package com.example.myandroidunity;
import android.util.Log;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
public class MainActivity extends UnityPlayerActivity {
//新增方法供unity 调用
public int Add(int a,int b) {
return a+b;
}
//unity call android call unity
public void sendmsgtounity(String str){
//UnityPlayer.UnitySendMessage(gameObject, functionName, funcParam);
Log.d("MainActivity",str);
UnityPlayer.UnitySendMessage("Main Camera","onmessage","adroid"+str);
}
}
3: 修改 module 下面的 build.gradle 文件
implementation 修改为 compileOnly 只编译不包含到里面,因为最后还是要放到unity 里,unity里自带
classes.jar
4: make module
5: 新建 unity 工程
1>把 编译的aar 与 aar 里包含的 AndroidManifest.xml (把aar 复制一个,改名 为*.zip 再把 AndroidManifest.xml 解压出来,打包时会自动合并 AndroidManifest.xml)放到Assets\Plugins\Android
2>unity 工程 UI 布局
代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class main : MonoBehaviour {
public UnityEngine.UI.Button btn1;
public UnityEngine.UI.Button btn2;
public UnityEngine.UI.Text txt;
public UnityEngine.UI.InputField inputField;//输入框本身
AndroidJavaClass jc =null;
AndroidJavaObject jo =null ;
private string strcon = "" ;
// Use this for initialization
void Start () {
if (Application.platform == RuntimePlatform.Android){
jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
}
inputField.onEndEdit.AddListener(inputFieldEnd);//文本输入结束时会调用
}
// Update is called once per frame
void Update () {
}
public void onButton1(){
if(jc != null) {
txt.text = jo.Call<int>("Add", 10, 30).ToString();
}
}
public void onButton2(){
if(jc != null) {
jo.Call("sendmsgtounity",strcon);
}
}
void inputFieldEnd(string e)
{
// print("当编辑结束时:"+e);
strcon = e;
}
//接收UnityPlayer.UnitySendMessage("Main Camera","onmessage","adroid"+str);
void onmessage(string str)
{
try
{
if(!string.IsNullOrEmpty( str ))
{
txt.text = str ;
}else{
}
}catch
{
txt.text ="onmessage error";
}
}
}
3> 在unity 直接选择android 编译工程,验证结果
点击Button1点击Button2
到这里 第一种方式完成了
3 第二种调用方式
1: 复制个上面的unity 工程,把Assets\Plugins 目录删掉
代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class main : MonoBehaviour {
public UnityEngine.UI.Button btn1;
public UnityEngine.UI.Button btn2;
public UnityEngine.UI.Text txt;
public UnityEngine.UI.InputField inputField;//输入框本身
AndroidJavaClass jc =null;
// AndroidJavaObject jo =null ;
private string strcon = "" ;
// Use this for initialization
void Start () {
if (Application.platform == RuntimePlatform.Android){
// jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
// jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
//unity project export 3 files
//1 UnityPlayerActivity.java public class UnityPlayerActivity extends Activity
//2 UnityPlayerNativeActivity.java public class UnityPlayerNativeActivity extends UnityPlayerActivity
//3 UnityPlayerProxyActivity.java public class UnityPlayerProxyActivity extends Activity
//com.test.unitycallandroid packname + UnityPlayerActivity classname
//or new java class //可以创建个新类给unity 调用
jc = new AndroidJavaClass("com.test.unitycallandroid.UnityPlayerActivity");//com.test.unitycallandroid packname + UnityPlayerActivity classname
// jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
}
inputField.onEndEdit.AddListener(inputFieldEnd);//文本输入结束时会调用
}
// Update is called once per frame
void Update () {
}
public void onButton1(){
if(jc != null) {
txt.text = jc.CallStatic<int>("Add", 10, 30).ToString();
}
}
public void onButton2(){
if(jc != null) {
jc.CallStatic("sendmsgtounity",strcon);
}
}
void inputFieldEnd(string e)
{
// print("当编辑结束时:"+e);
strcon = e;
}
//接收UnityPlayer.UnitySendMessage("Main Camera","onmessage","adroid"+str);
void onmessage(string str)
{
try
{
if(!string.IsNullOrEmpty( str ))
{
txt.text = str ;
}else{
}
}catch
{
txt.text ="onmessage error";
}
}
}
2:导出工程
包名一定要跟 代码里的 一样(eg:com.test.unitycallandroid)
jc = new AndroidJavaClass("com.test.unitycallandroid.UnityPlayerActivity");//com.test.unitycallandroid packname + UnityPlayerActivity classname
3:用androidstudio 打开工程
修改 UnityPlayerActivity.java 增加静态函数
为了与上一个工程结果不一样,函数里 稍微修改了下
代码如下
//新增方法供unity 调用
public static int Add(int a,int b) {
return 2*(a+b);
}
//unity call android call unity
public static void sendmsgtounity(String str){
//UnityPlayer.UnitySendMessage(gameObject, functionName, funcParam);
Log.d("MainActivity",str);
UnityPlayer.UnitySendMessage("Main Camera","onmessage","[adroid]"+str);
}
4:编译工程 测试结果
点击button1
点击button2
4 c# 与 java 调用完成
工程代码如有需要后续再上传