Unity和安卓通信之Unity里访问安卓的jar包

       最近做项目要用到安卓里的一些原生的方法,而Unity本身又实现不了。俗话说,好记性不如烂笔头,所以把测试过程记录一下。以后用起来自己也方便。

      本测试主要测试一个安卓原生开发的APP传递消息,Unity开发的APP接收消息进行相关处理,当然这两APP是装同一个安卓设备上。测试只涉及到Unity方面接收数据先打印出来。两外就是测试Unity里调用jar里的静态方法和非静态方法。

       先是找安卓开发的同事,让其帮忙出了一个jar包。用jd-gui.exe工具可以查看jar里的源码。点击jd-gui.exe 直接启动这个工具:

点击File -->Open File  找到jar打开:

把jar 导入Unity放在Plugin/Android 下:

同时也得导入一份AndroidMainfest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xx.xxxx" android:versionName="1.0" android:versionCode="1" android:installLocation="preferExternal">
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
  <application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="true" android:isGame="true" android:banner="@drawable/app_banner">
    <activity android:name="com.gxxxx.unitymoduletest.MainActivity" android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
        <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
      </intent-filter>
    </activity>
  </application>
  <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" />
  <uses-feature android:glEsVersion="0x00020000" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="25" />
  <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
  <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
  <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
</manifest>

Unity上创建测试代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour {
    public Text text1;
    public Text text2;
    public Text text;
    public Text text4;
    public string AndroidClassName = "com.gxxx.unitymoduletest.MainActivity";
    public string GetLotMsgJsonMethod = "getLotMsgJson";
    public string GetIssMsgJsonMethod = "getIssMsgJson";
    public string GetHistoryTrendMethod = "getHistoryTrend";
    public string GetTestMethod = "getTestMethod";
    private AndroidJavaClass Ajc;
    public void FetchAndroidData()
    {
        if (Application.platform != RuntimePlatform.Android)
            return;
        //调用静态方法测试
        var LotMsgJson = Ajc.CallStatic<string>(GetLotMsgJsonMethod);
        if (!string.IsNullOrEmpty(LotMsgJson)) {
            text1.text = LotMsgJson;
            Debug.Log(LotMsgJson);
        }


        var IssMsgJson = Ajc.CallStatic<string>(GetIssMsgJsonMethod);
        if (!string.IsNullOrEmpty(IssMsgJson))
        {
            text2.text = IssMsgJson;
            Debug.Log(IssMsgJson);
        }


        var HistoryTrend = Ajc.CallStatic<string>(GetHistoryTrendMethod);
        if (!string.IsNullOrEmpty(HistoryTrend))
        {
            text.text = HistoryTrend;
            Debug.Log(HistoryTrend);
        }
    }

    private void Awake()
    {
        Ajc= new AndroidJavaClass(AndroidClassName);
    }
    private void Start()
    {
        FetchAndroidData();
    }

    //启动另外一个APP  调用非静态方法
    public void OpenOtherApp() {
        text4.text = "1";
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        jo.Call("UpApp");
        text4.text = "2";       
    }
    //调用非静态方法测试
    public void TestButton() {
        StartCoroutine(GetStr());
    }

    IEnumerator GetStr() {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        jo.Call("setTestMethod");
        yield return new WaitForEndOfFrame();
        var str = pluginClass.CallStatic<string>(GetTestMethod);
        text4.text = str;
    }
    public void QuictApp()
    {      
        Application.Quit();
    }
}

对于静态方法的调用: 

 AndroidJavaClass jc= new AndroidJavaClass(AndroidClassName);

 var LotMsgJson = jc.CallStatic<string>(“方法名”)

对于非静态方法的调用:

       AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");//必须这样写
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");//必须这样写
        jo.Call("方法名");     

Unity里把测试代码绑在MainCamera 上,同时按钮把对应方法绑定上:

最后发布Apk包装安卓设备进行测试。

测试结果:

    启动安卓原生开发的app,点击打开Unity开发程序,就会打开Unity开发的app,同时进入后执行到了包里的静态方法。

点击测试按钮: text4显示出了jar包里的hell word ,说明执行到了包里的非静态setTestMethod 方法,然后通过再调用静态方法getTestMethod返回来结果。

点击打开APP,会打开安卓的APP程序,本APP进入后台运行。

猜你喜欢

转载自blog.csdn.net/hemiaoyuan1989/article/details/105732516