「Unity/Native連携/Android連携」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「 =Android-Unity連携= ==android側でaarライブラリを作成== 以下の"aarライブラリ作成項目を確認してaarファイルを作成する Android/開...」) |
(→javaバージョン) |
||
| (同じ利用者による、間の1版が非表示) | |||
| 行20: | 行20: | ||
public void testexec() { | public void testexec() { | ||
Log.i("test", "testexec"); | Log.i("test", "testexec"); | ||
| + | } | ||
| + | public static String teststrstatic(String message) { | ||
| + | return "Static received: " + message; | ||
| + | } | ||
| + | public static int testnumberstatic(int num) { | ||
| + | return num + 10; | ||
} | } | ||
} | } | ||
| 行48: | 行54: | ||
bool flagfalse = jo.Call<bool>("test", false); | bool flagfalse = jo.Call<bool>("test", false); | ||
bool flagstaticfalse = jo.CallStatic<bool>("teststatic", false); | bool flagstaticfalse = jo.CallStatic<bool>("teststatic", false); | ||
| + | string strstatic= jo.CallStatic<string>("teststrstatic", "hoge"); | ||
| + | int numberstatic= jo.CallStatic<int>("testnumberstatic", 123); | ||
jo.Call("testexec"); | jo.Call("testexec"); | ||
Debug.Log("flag=" + flag); | Debug.Log("flag=" + flag); | ||
| 行53: | 行61: | ||
Debug.Log("flagfalse=" + flagfalse); | Debug.Log("flagfalse=" + flagfalse); | ||
Debug.Log("flagstaticfalse=" + flagstaticfalse); | Debug.Log("flagstaticfalse=" + flagstaticfalse); | ||
| + | Debug.Log("strstatic=" + strstatic); | ||
| + | Debug.Log("numberstatic=" + numberstatic); | ||
} | } | ||
catch (System.Exception e) | catch (System.Exception e) | ||
| 行65: | 行75: | ||
<pre> | <pre> | ||
I Unity : flag=True | I Unity : flag=True | ||
| − | |||
I Unity : flagstatic=True | I Unity : flagstatic=True | ||
| − | |||
I Unity : flagfalse=False | I Unity : flagfalse=False | ||
| − | |||
I Unity : flagstaticfalse=False | I Unity : flagstaticfalse=False | ||
| − | I Unity : | + | I Unity : strstatic=Static received: hoge |
| + | I Unity : numberstatic=133 | ||
</pre> | </pre> | ||
2024年9月11日 (水) 23:47時点における最新版
目次
Android-Unity連携
android側でaarライブラリを作成
以下の"aarライブラリ作成項目を確認してaarファイルを作成する
Android/開発環境/AndroidStudio/aarライブラリ作成読込方法 [ショートカット]
javaバージョン
UnityRenkei.java
package com.example.mylibrary;
import android.util.Log;
public class UnityRenkei {
public static boolean teststatic(boolean flag) {
return flag;
}
public boolean test(boolean flag) {
return flag;
}
public void testexec() {
Log.i("test", "testexec");
}
public static String teststrstatic(String message) {
return "Static received: " + message;
}
public static int testnumberstatic(int num) {
return num + 10;
}
}
Android-unity連携する
- aarをAssets/Plugins/Android/の下に配置する
- 以下コードを実行する
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
[SerializeField] Button button;
void Start()
{
button.onClick.AddListener(CallMethod);
}
void CallMethod()
{
#if UNITY_ANDROID && !UNITY_EDITOR
try
{
AndroidJavaObject jo = new AndroidJavaObject("com.example.mylibrary.UnityRenkei");
bool flag = jo.Call<bool>("test", true);
bool flagstatic = jo.CallStatic<bool>("teststatic", true);
bool flagfalse = jo.Call<bool>("test", false);
bool flagstaticfalse = jo.CallStatic<bool>("teststatic", false);
string strstatic= jo.CallStatic<string>("teststrstatic", "hoge");
int numberstatic= jo.CallStatic<int>("testnumberstatic", 123);
jo.Call("testexec");
Debug.Log("flag=" + flag);
Debug.Log("flagstatic=" + flagstatic);
Debug.Log("flagfalse=" + flagfalse);
Debug.Log("flagstaticfalse=" + flagstaticfalse);
Debug.Log("strstatic=" + strstatic);
Debug.Log("numberstatic=" + numberstatic);
}
catch (System.Exception e)
{
Debug.LogError("Failed to call Android method: " + e.Message);
}
#endif
}
}
このように表示されればとりあえず成功。Android実機のみで、UnityEditorだと動作しないので注意。
I Unity : flag=True I Unity : flagstatic=True I Unity : flagfalse=False I Unity : flagstaticfalse=False I Unity : strstatic=Static received: hoge I Unity : numberstatic=133
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicateエラーが起こったとき
コピーミスなどにより、Assetrs/Plugins/Android/classes.jarがあるとクラスが2重となっている可能性がある。 一旦classes.jarを消してみる。
参考
AndroidJavaObject公式:https://docs.unity3d.com/ja/current/ScriptReference/AndroidJavaObject.html Unity-Android-Plugin:https://qiita.com/keidroid/items/455c61de9355eff2a907
