最近ではAndroidのネイティブメソッドがプロジェクト内で使われるようになりましたが、Unity自体では実装できません。ことわざにあるように、良い記憶力は悪い文章よりも悪いので、テストのプロセスを記録してください。これからますます便利になりそうです。
このテストでは主に、Android でネイティブに開発されたメッセージの送信を行う APP と、Unity で開発されたメッセージの受信および関連処理を行う APP をテストします (当然、2 つの APP は同じ Android デバイスにインストールされます)。テストでは、Unity がデータを受信して最初に出力するだけです。残りの 2 つは、Unity で呼び出される jar 内の静的メソッドと非静的メソッドをテストするためのものです。
まず、Android 開発の同僚に jar パッケージの作成を手伝ってくれるように頼みました。jd-gui.exe ツールを使用して、jar 内のソース コードを表示します。jd-gui.exe をクリックして、このツールを直接起動します。
[ファイル] --> [ファイルを開く] をクリックして、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("method"と記述する必要があります)名前」このように);
Unity では、テスト コードは MainCamera にバインドされ、ボタンは対応するメソッドをバインドします。
最後に、テスト用に Android デバイスをラップする APK をリリースします。
試験結果:
Android でネイティブに開発されたアプリを起動し、クリックして Unity 開発プログラムを開くと、Unity で開発されたアプリが開き、同時に入力後、パッケージ内の静的メソッドが実行されます。
テスト ボタンをクリックします。 text4 は、jar パッケージ内にヘル ワードを表示します。これは、パッケージ内の非静的 setTestMethod メソッドが実行され、静的メソッド getTestMethod を呼び出すことによって結果が返されることを示します。
クリックして APP を開くと、Android APP プログラムが開き、APP がバックグラウンドで実行されます。