facebook twitter hatena line email

Unity/Native連携/Swift連携

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

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();

    [DllImport("__Internal")]
    private static extern string _testStaticStrMethod(string message);

    [DllImport("__Internal")]
    private static extern int _testStaticNumberMethod(int number);
#endif
    void Start()
    {
#if UNITY_IOS && !UNITY_EDITOR
        bool flag = _testMethod(true);
        bool flagstatic = _testStaticMethod(true);
        bool flagfalse = _testMethod(false);
        bool flagstaticfalse = _testStaticMethod(false);
        string strstatic = _testStaticStrMethod("hoge");
        int numberstatic = _testStaticNumberMethod(123);
        _testExecMethod();
        Debug.Log("flag=" + flag);
        Debug.Log("flagstatic=" + flagstatic);
        Debug.Log("flagfalse=" + flagfalse);
        Debug.Log("flagstaticfalse=" + flagstaticfalse);
        Debug.Log("strstatic=" + strstatic);
        Debug.Log("numberstatic=" + numberstatic);
#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")
    }
    @objc public static func teststaticstr(_ message: String) -> String {
        print("Received String from Unity: \(message)")
        let modifiedMessage = "Modified message: \(message)"
        return modifiedMessage
    }
    @objc public static func teststaticnumber(_ number: Int) -> Int {
        print("Received Int from Unity: \(number)")
        let modifiedNumber = number + 10
        return modifiedNumber
    }
}

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のインスタンスメソッドを呼び出す
    }
    const char* _testStaticStrMethod(const char* message) {
        NSString *nsMessage = [NSString stringWithUTF8String:message];  // Convert C string to NSString
        NSString *result = [UnityRenkei teststaticstr:nsMessage];  // Call Swift method
        // return [result UTF8String];  // Convert NSString back to C string
        const char* cResult = strdup([result UTF8String]);  // Duplicate the string to prevent memory issues
        return cResult;  // Return the duplicated C string
    }
    int _testStaticNumberMethod(int number) {
        int result = [UnityRenkei teststaticnumber:number];  // Call Swift method
        NSLog(@"Static number result: %d", result);
        return result;
    }
}

出力

flag=True
flagstatic=True
flagfalse=False
flagstaticfalse=False
testexec
strstatic=Modified message: hoge
numberstatic=133

参考

https://qiita.com/ohbashunsuke/items/8f3b7c733fc70a180941