Issue Summary:
When running Unity standalone with my custom iOS framework included, I have no issues using the following code to interact between Unity and my iOS framework via DllImport and Swift’s @_cdecl functions:
[DllImport("__Internal")]
private static extern void StartTracking();
[DllImport("__Internal")]
private static extern void RegisterPoseUpdateCallback(PoseUpdateDelegate callback);
Corresponding Swift Code (in iOS Framework):
@_cdecl("StartTracking")
public func StartTracking() {
PoseDetectorViewModel.shared.startTracking()
}
@_cdecl("StopTracking")
public func StopTracking() {
PoseDetectorViewModel.shared.stopTracking()
}
@_cdecl("RegisterPoseUpdateCallback")
public func RegisterPoseUpdateCallback(callback: @escaping PoseUpdateCallback) {
PoseDetectorViewModel.shared.registerPoseUpdateCallback(callback: callback)
}
Problem:
I need to switch to running Unity as a library due to limitations with Unity not being able to modify the resolution on an external screen on iOS (more context here: Render Cap on Secondary Display for iOS).
However, when running Unity as a library, I keep encountering linker issues when trying to call the above Swift functions from Unity C#. The linker errors look something like this:
Undefined symbol: _RegisterPoseUpdateCallback
Undefined symbol: _StartTracking
Linker command failed with exit code 1 (use -v to see invocation)
Undefined symbols for architecture arm64:
"_RegisterPoseUpdateCallback", referenced from:
_HandPositionManager_RegisterPoseUpdateCallback_m5C0626F19C89FF7EED2C41CC4A53DDAF21DA96DB in libGameAssembly.a[7](svyqflz67bft.o)
"_StartTracking", referenced from:
_HandPositionManager_StartTracking_m82E6D51300253F38AB23C0F11BC55573A421D0CB in libGameAssembly.a[7](svyqflz67bft.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Context:
• Unity Version: 2022.3.36f1
• Xcode Version: 15.4
• iOS Version: 18.0
The framework works fine in standalone Unity builds, but something about switching to Unity as a library is causing the linker to fail to recognize these Swift-exposed functions. I’ve tried adding a bridging header and confirmed that the Swift functions are correctly marked with @_cdecl to ensure they’re callable from C#.
What I’ve Tried:
1. Bridging Header: Created a bridging header with #import "UnityExports.h". However, this did not resolve the issue.
2. Clean & Rebuild: Attempted cleaning the project and rebuilding, but the same errors persist.
3. Ensuring Target Membership: Verified that all necessary files (Swift, Objective-C) are included in the correct target for the UnityFramework.
4. @_cdecl Functions: Double-checked that the function names are consistent between Unity C# and Swift, and that they are properly marked for C interoperability.
Questions:
• Has anyone successfully integrated a Swift framework with Unity as a library?
• Are there any additional steps required to properly expose Swift functions to Unity in this scenario?
• Could this be related to how Unity as a library changes the linking behavior or function visibility?
Thanks for any help or suggestions!