Unity导出iOS项目合并至其他iOS原生项目报错求助
Hey there, I’ve run into this exact headache multiple times when merging a Unity-exported iOS project into an existing native codebase. Let’s walk through the most common culprits and how to fix them—start with the top errors first, since a lot of downstream issues are just side effects of one root problem.
Unity’s iOS export relies on its own framework and a handful of Apple system frameworks that your native project might not have included yet:
- First, make sure you’ve copied the
UnityFramework.frameworkfrom the exported Unity project into your native project’s Frameworks folder. Then in Xcode, select your target, go to General > Frameworks, Libraries, and Embedded Content, set its status to Embed & Sign. - Double-check that all required system frameworks are added too—Unity typically needs:
CoreMotion,AVFoundation,CoreMedia,GameController,ARKit(if using AR features), andCoreLocation(if using location). Add any missing ones via the "+" button in the frameworks panel.
This is one of the biggest sources of errors. Your native project’s build settings might clash with Unity’s required config:
- Deployment Target: Unity often exports with a minimum iOS version (like 12.0 or higher). Make sure your native project’s Deployment Target (under General and Build Settings > Deployment) matches exactly what Unity used. Mismatched versions cause linker and API compatibility errors.
- Architectures: Unity now only supports
arm64for iOS. Go to Build Settings > Architectures and set Architectures toarm64, Valid Architectures to justarm64, and ensure Build Active Architecture Only is set toYesfor debug builds. - Other Linker Flags: Add these flags to your target’s Build Settings > Linking > Other Linker Flags:
-ObjC -framework UnityFramework - Header Search Paths: Add the path to Unity’s header files as a recursive search path. For example, if you copied Unity’s
Librariesfolder into your project, add:
Replace$(PROJECT_DIR)/[YourUnityFolderPath]/Libraries/Unity/Headers/**[YourUnityFolderPath]with the actual relative path where you placed Unity’s files.
Copying files directly often leads to duplicates or conflicting entry points:
- Main Entry Point: Unity uses a custom
main.mmto initialize its runtime. If your native project has its ownmain.m/main.mm, you’ll get a duplicate symbol error. You need to merge the logic: keep Unity’smain.mmand modify it to call your native app’s initialization code after Unity’s setup, or vice versa (depending on which part is the primary entry). - Info.plist Conflicts: Unity adds several required privacy permissions and settings to its
Info.plist(like camera, microphone, photo library access if your app uses those features). Don’t overwrite your native project’sInfo.plist—instead, copy all the Unity-specific keys (look for entries starting withPrivacy -,UnityorARKit) into your existingInfo.plist. - Duplicate Resource Files: If both projects have assets with the same name (like images, audio files), you’ll get duplicate resource errors. Rename one set of files or remove duplicates that aren’t needed.
If your native project uses classes, functions, or macros with the same names as Unity’s, you’ll get compilation errors:
- Class Name Conflicts: Search for errors like
duplicate symbol _OBJC_CLASS_$_[ClassName]. If the conflicting class is from your native code, rename it or wrap it in a custom namespace. If it’s from Unity, you’ll need to adjust your native code to avoid the clash. - Preprocessor Macros: Unity defines several macros (like
UNITY_IOS,UNITY_2023_1) in its build settings. Go to your target’s Build Settings > Apple Clang - Preprocessing > Preprocessor Macros and add any Unity-specific macros that are missing (you can find these in the exported Unity project’s build settings).
Unity adds custom build phases to handle resource copying and post-export scripts. These often break when you copy the project:
- Copy Unity’s custom build phases (like Copy Unity Resources, Run Unity Post Export Scripts) from the exported project to your native target’s Build Phases tab.
- For any script phases, check the paths in the script—if you moved Unity’s files, the absolute paths will be wrong. Update them to use relative paths (e.g., replace
/Users/YourName/UnityProject/...with$(PROJECT_DIR)/YourUnityFolder/...). - Make sure the order of build phases is correct: Unity’s resource copy phases should run before the Compile Sources phase, and post-export scripts should run after.
- Always start with the first error in the Xcode console—most subsequent errors are just cascading issues from that one root problem.
- Use the Report Navigator (the bell icon on the left) to filter errors by type (e.g., duplicate symbols, missing frameworks) to narrow down the issue.
- Clean your project (
Cmd+Shift+K) and delete the derived data (Cmd+Option+Shift+K) before rebuilding—sometimes cached files cause weird errors.
内容的提问来源于stack exchange,提问作者Liu Silong

