Unity3d 与android studio通信
unity3d 2018.3.5 与android 通信。
unity3d 与android 通信 有很多同学 不太熟悉 android 开发 记录一下
1, 安装 android studio
官网 下载: https://developer.android.google.cn/studio
无脑安装 一路 next .
创建工程
创建一个空的工程 。 这个工程没有啥用 起啥名字 也无关紧要。
2,创建 android lib
将unity3d 的 classes.jar 拖入 libs:
unity3d 安装目录 Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes.jar
创建脚本:
插入代码:
package com.test.libs;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.unity3d.player.UnityPlayer;
//这个就是unity3d 包
import com.unity3d.player.UnityPlayerActivity;
//android 里面每一个界面 都是一个activity 所以启动一个 界面
public class MainActivity extends UnityPlayerActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public int Add (int a ,int b)
{
/// java 掉 c# 代码
//Gloable 哪个物体
///JavaCall哪个方法
//hello 参数
UnityPlayer.UnitySendMessage("Gloable","JavaCall","hello");
return a + b ;
}
public static int Add2 (int a ,int b)
{
/// java 掉 c# 代码
//Gloable 哪个物体
///JavaCall哪个方法
//hello 参数
UnityPlayer.UnitySendMessage("Gloable","JavaCall","hello");
return a + b ;
}
}
填写配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<font color=blue > blue </font>
***#######注意这里换成自己的 包名***
package="com.test.libs"
android:versionCode="1"
android:versionName="1.0" >
<application>
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:icon="@drawable/app_icon">
**#######注意这里换成自己的 包名**
<activity android:name="com.test.libs.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
</manifest>
build 出来
编译出来东西 变成 zip 将libs 里面的classes.jar删除然后在换成 .aar
unity3d 工程
创建脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestMyLib : MonoBehaviour
{
AndroidJavaObject jo;
// Start is called before the first frame update
void Start()
{
}
public void JavaCall(string tmpStr)
{
Debug.Log("android call C# =="+ tmpStr);
}
// Update is called once per frame
void Update()
{
Touch[] touches = Input.touches;
if(touches.Length > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
//调用java 类里面的 add 方法 并传参数 1,2
var tmp= jo.Call<int>("Add", 1, 2);
Debug.Log("C# 调用 android2222 ==="+tmp);
}
}
}
}
工程设置
plugins 里面 必须建立 Andoird 这个文件夹 不然调用不到。
真机上 查看
插上 usb 线 andorid studio 自带 logcat 可以查看 log 日志
总结
配置麻烦 调用简单
java 调用 c#
/// java 掉 c# 代码
//Gloable 哪个物体
///JavaCall哪个方法
//hello 参数
UnityPlayer.UnitySendMessage("Gloable","JavaCall","hello");
c# 调用 java
//前面两句是死的 就得这么写
//查找 类
AndroidJavaClass jc = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);
//查找activity 实例
jo = jc.GetStatic(“currentActivity”);
//这句才调用
//调用java 类里面的 add 方法 并传参数 1,2
var tmp= jo.Call(“Add”, 1, 2);