Unity/Native連携
提供: 初心者エンジニアの簡易メモ
目次
Android-Unity連携
android側でaarライブラリを作成
以下の"aarライブラリ作成項目を確認してaarファイルを作成する
Android/開発環境/AndroidStudio/aarライブラリ作成読込方法 [ショートカット]
package com.example.mylibrary.renkeilib;
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
iOS-Unity連携
Unityに記述
using System.Runtime.InteropServices;
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
// DllImportでObjective-Cのラッパーメソッドを指定
[DllImport("__Internal")]
private static extern void _testStaticMethod();
[DllImport("__Internal")]
private static extern void _testMethod();
[DllImport("__Internal")]
private static extern void _testExecMethod();
void Start()
{
#if UNITY_IOS && !UNITY_EDITOR
_testStaticMethod(); // Swiftのstaticメソッド呼び出し
_testMethod(); // Swiftのインスタンスメソッド呼び出し
_testExecMethod(); // Swiftの別のインスタンスメソッド呼び出し
#endif
}
}
xcode側で、UnityRenkei.swiftを作成し、以下を貼り付ける
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")
}
}
UnityRenkeiBridge.m
#import "Unity-iPhone-Swift.h"
UnityBridge.m
#import <Foundation/Foundation.h>
#import "Unity-iPhone-Swift.h" // Bridging HeaderでSwiftクラスをインポート
@interface UnityBridge : NSObject
@end
@implementation UnityBridge
// Unityから呼び出せるSwiftメソッドのラッパー
void _testStaticMethod() {
bool flag = [UnityRenkei teststatic:true]; // Swiftのstaticメソッドを呼び出す
NSLog(@"Flag: %d", flag);
}
void _testMethod() {
UnityRenkei *renkei = [[UnityRenkei alloc] init];
bool flag = [renkei test:true]; // Swiftのインスタンスメソッドを呼び出す
NSLog(@"Flag: %d", flag);
}
void _testExecMethod() {
UnityRenkei *renkei = [[UnityRenkei alloc] init];
[renkei testexec]; // Swiftのインスタンスメソッドを呼び出す
}
@end
