Native plugin to write native code in Swift for Unity.
Download unity-swift.unitypackage from link below:
- Open your project in Unity.
- Open the downloaded package by double-click or choose Assets menu > Import Package > Custom Package... to import plugin into your project.
- Plugin files are imported into UnitySwift folder.
See Example/Assets/Main/Main.unity and UIController.cs.
See unity-replay-kit-bridge/Example/Assets/UnityReplayKitBridge at swift · miyabi/unity-replay-kit-bridge for an actual native plugin example.
// Example.swift
import Foundation
public class Example : NSObject {
static func callUnityMethod(_ message: String) {
let uf = UnityFramework.getInstance()
// Call a method on a specified GameObject.
uf?.sendMessageToGO(
withName: "CallbackTarget",
functionName: "OnCallFromSwift",
message: message)
}
}
// Example.swift
import Foundation
public class Example : NSObject {
@objc public static func swiftMethod(_ message: String) {
print("\(#function) is called with message: \(message)")
}
}
Step 2: Include UnityFramework/UnityFramework-Swift.h and define C functions to wrap Swift classes in .mm file (Objective-C++).
// Example.mm
#import <Foundation/Foundation.h>
#import <UnityFramework/UnityFramework-Swift.h>
// Required
// This header file is generated automatically when Xcode build runs.
extern "C" {
void _ex_callSwiftMethod(const char *message) {
// You can access Swift classes directly here.
[Example swiftMethod:[NSString stringWithUTF8String:message]];
}
}
// Example.cs
using System.Runtime.InteropServices;
public class Example {
#if UNITY_IOS && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern void _ex_callSwiftMethod(string message);
#endif
// Use this method to call Example.swiftMethod() in Example.swift
// from other C# classes.
public static void CallSwiftMethod(string message) {
#if UNITY_IOS && !UNITY_EDITOR
_ex_callSwiftMethod(message);
#endif
}
}
Example.CallSwiftMethod("Hello, Swift!");
iOS 9 or later
Unity 2019.4.1f1
Xcode 11.5