Unity/Native連携
目次
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");
}
}
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);
jo.Call("testexec");
Debug.Log("flag=" + flag);
Debug.Log("flagstatic=" + flagstatic);
Debug.Log("flagfalse=" + flagfalse);
Debug.Log("flagstaticfalse=" + flagstaticfalse);
}
catch (System.Exception e)
{
Debug.LogError("Failed to call Android method: " + e.Message);
}
#endif
}
}
このように表示されればとりあえず成功。Android実機のみで、UnityEditorだと動作しないので注意。
I Unity : flag=True I Unity : (Filename: ./Runtime/Export/Debug.bindings.h Line: 43) I Unity : flagstatic=True I Unity : (Filename: ./Runtime/Export/Debug.bindings.h Line: 43) I Unity : flagfalse=False I Unity : (Filename: ./Runtime/Export/Debug.bindings.h Line: 43) I Unity : flagstaticfalse=False I Unity : (Filename: ./Runtime/Export/Debug.bindings.h Line: 43)
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
javaをkotlinにする場合
UnityRenkei.javaを削除し、UnityRenkei.ktを追加する
UnityRenkei.kt
package com.example.mylibrary
import android.util.Log
class UnityRenkei {
companion object {
@JvmStatic
fun teststatic(flag: Boolean): Boolean {
return flag
}
}
fun test(flag: Boolean): Boolean {
return flag
}
fun testexec() {
Log.i("test", "testexec")
}
}
プロジェクト直下のbuild.gradleにbuildscriptを追加
buildscript {
ext.kotlin_version = "1.8.10"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:8.1.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id 'com.android.application' version '8.1.0' apply false
id 'com.android.library' version '8.1.0' apply false
id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
}
mylibrary/build.gradleを以下の通り修正
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.mylibrary'
compileSdk 34
defaultConfig {
minSdk 24
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
==="NoClassDefFoundError"エラーが出る場合===
エラー詳細
<pre>
Error Unity AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;
Error Unity java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;
対策方法
ProjectSettingsのAndroidのCustomMainGradleTemplateをチェック
Assets/Plugins/Android/mainTemplate.gradle に以下を追加
buildscript {
ext.kotlin_version = '1.8.0'
repositories {
google()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // Kotlinランタイムを追加
}
iOS-Unity連携
Unityに記述
#if UNITY_IPHONE
using System.Runtime.InteropServices;
#endif
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
#if UNITY_IPHONE
[DllImport("__Internal")]
private static extern bool _testStaticMethod(bool flag);
[DllImport("__Internal")]
private static extern bool _testMethod(bool flag);
[DllImport("__Internal")]
private static extern void _testExecMethod();
#endif
void Start()
{
#if UNITY_IOS && !UNITY_EDITOR
bool flag = _testMethod(true);
bool flagstatic = _testStaticMethod(true);
bool flagfalse = _testMethod(false);
bool flagstaticfalse = _testStaticMethod(false);
Debug.Log("flag=" + flag);
Debug.Log("flagstatic=" + flagstatic);
Debug.Log("flagfalse=" + flagfalse);
Debug.Log("flagstaticfalse=" + flagstaticfalse);
_testExecMethod();
#endif
}
}
Assets/Plugins/iOS/UnityRenkei.swiftを作成し、以下を貼り付ける。(xcode側では、Libraries/Plugins/iOS/にある。)
import Foundation
@objc public class UnityRenkei: NSObject {
@objc public static func teststatic(_ flag: Bool) -> Bool {
return flag
}
@objc public func test(_ flag: Bool) -> Bool {
return flag
}
@objc public func testexec() {
print("testexec")
}
}
Assets/Plugins/iOS/UnityBridge.mmを作成し、以下を貼り付ける。(xcode側では、Libraries/Plugins/iOS/にある。)
#import <UnityFramework/UnityFramework-Swift.h>
extern "C" {
// Unityから呼び出せるSwiftメソッドのラッパー
bool _testStaticMethod(bool flag) {
bool result = [UnityRenkei teststatic:flag]; // Swiftのstaticメソッドを呼び出す
NSLog(@"result: %d", result);
return result;
}
bool _testMethod(bool flag) {
UnityRenkei *renkei = [[UnityRenkei alloc] init];
bool result = [renkei test:flag]; // Swiftのインスタンスメソッドを呼び出す
NSLog(@"result: %d", result);
return result;
}
void _testExecMethod() {
UnityRenkei *renkei = [[UnityRenkei alloc] init];
[renkei testexec]; // Swiftのインスタンスメソッドを呼び出す
}
}
出力
flag=True flagstatic=True flagfalse=False flagstaticfalse=False testexec
