Hi @jwtan,
Thank you for sharing this, I spent countless hours to fix Unity + Swift problem. Without your explanation I couldn't understand what are the problems. I wish I could've found this repo earlier.
I think you should share this on unity forums because this is a hidden gem here :)
So, I have a project that I want to integrate with a swift library but I can't use the callback on swift:
I create a header to define my callback
// SwiftToUnityBridge.h#ifndefSwiftToUnityBridge_h#defineSwiftToUnityBridge_h#ifdef__cplusplus// gives error without itextern"C" {
#endiftypedefvoid (*SwiftCallback)(); //<- I define the callback here#ifdef__cplusplus
}
#endif#endifThen, add a callback on mm file
// SwiftToUnityBridge.mm
#import <UnityFramework/UnityFramework-Swift.h>
extern "C"
{
void UnityOnStart()
{
[[SwiftToUnity shared] UnityOnStart];
}
+ void CallWithACallback(SwiftCallback callback)+ {+ [[SwiftToUnity shared] callWithCallback: callback];+ }
}
and, define a function on swift
// SwiftToUnity.swift
import Foundation
@objc public class SwiftToUnity: NSObject
{
@objc public static let shared = SwiftToUnity()
@objc public func UnityOnStart()
{
UnitySendMessage("Cube", "OnMessageReceived", "Hello World!");
}
+ @objc public func call(callback: @escaping SwiftCallback)+ {+ callback();+ }
}
And moved the header from project to public (I don't know if I should do this)

After that, I added the header to UnityFramework.modulemap
framework module UnityFramework {
umbrella header "UnityFramework.h"
export *
module * { export * }
module UnityInterface {
header "UnityInterface.h"
+ header "SwiftToUnityBridge.h"
export *
}
}But I can't find out why it still doesn't let me use the callback

Generated UnityFramework-Swift.h:

Thank you for sharing this.
Hi @jwtan,
Thank you for sharing this, I spent countless hours to fix Unity + Swift problem. Without your explanation I couldn't understand what are the problems. I wish I could've found this repo earlier.
I think you should share this on unity forums because this is a hidden gem here :)
So, I have a project that I want to integrate with a swift library but I can't use the callback on swift:
I create a header to define my callback
Then, add a callback on mm file
and, define a function on swift
// SwiftToUnity.swift import Foundation @objc public class SwiftToUnity: NSObject { @objc public static let shared = SwiftToUnity() @objc public func UnityOnStart() { UnitySendMessage("Cube", "OnMessageReceived", "Hello World!"); } + @objc public func call(callback: @escaping SwiftCallback)+ {+ callback();+ } }And moved the header from project to public (I don't know if I should do this)

After that, I added the header to UnityFramework.modulemap
framework module UnityFramework { umbrella header "UnityFramework.h" export * module * { export * } module UnityInterface { header "UnityInterface.h" + header "SwiftToUnityBridge.h" export * } }But I can't find out why it still doesn't let me use the callback


Generated UnityFramework-Swift.h:
Thank you for sharing this.