How to embed a Unity game into an iOS native Swift App

IronEqual
7 min readAug 8, 2018

--

Hi! For one or our future B2B contracts we have to integrate our Unity game into an iOS native Swift app. We have struggled for many days to achieve it efficiently and without errors.

We want to share our knowledge and process through this simple tutorial.

Resources:

Here’s the link to all the resources: Click here

And the links to specific parts of it:

  • Extra: The folder containing the essential files to follow the tutorial :
    Click here
  • TutorialImages: All the images used in the tutorial : Click here
  • UnityExport: The Unity iOS build export folder : Click here
  • UnityProject: The Unity project used in the tutorial : Click here
  • XcodeProject: The whole Xcode project that you can use as a template for your own project : Click here
    (If you encounter errors when building Delete and “Remove references” of the “Classes”, “Libraries” and “Data” folders and proceed the 11. 12. and 13. steps.)

Notes:

  • This tutorial was greatly inspired by the work of Jiulong Wang and you can check his tutorial and his awesome automation scripts here https://github.com/jiulongw/swift-unity
  • This tutorial was made with Unity 2017.4.0f1 but, it also works with the lastest 2018.2.2f1 version. However, you have to setMismatched return type” option to “Yes” in the “Build Settings” of the project
  • Notice that Xcode doesn’t like spaces in your paths and project name, so avoid it
  • Beware that embedding Unity can cause problems withing the game, especially graphics ones
  • When you make changes to your Unity project and export it again be sure to replace the “Classes”, “Libraries” and “Data” folders and check if the “Configurations” in the project settings is set to “Unity”
  • When you have errors in Xcode the commands
    “Clean” (Product/Clean) Command+Shift+K
    and “Clean Build Folder” Command+Option+Shift+K
    can help you get rid of it before building again
  • If you get a “Could not locate device support files” error in Xcode when building, download this repository https://github.com/iGhibli/iOS-DeviceSupport and add the missing Device Support folder in “/Applications/Xcode.app/Contents/Developer/Platforms/
    iPhoneOS.platform/DeviceSupport”

Tutorial:

1. Open Xcode and create a new Swift project or open an existing one

2. Select “Single View App” then click on “Next”

3. Name your project whatever you want and set the language to Swift

4. Save your project wherever you want

5. Open your Unity project and switch to iOS platform

6. Create a folder “Editor” if you don’t already have one and copy the “XcodePostBuild.cs” script that you can find in the “Extra” folder at the beginning of the tutorial. This is the script provided by Jiulong Wang

7. Open the script and replace the “XcodeProjectRoot” with the path to your Xcode project path that you just created. You have to put the path to the folder containing the “.xcodeproj” file. Then, replace the “XcodeProjectName” with the name of your Xcode project name, obviously

8. Build your game and save it in the same folder you created your Xcode project in

9. Open the Xcode project and check in the “General” tab if a valid “Team” is selected and verify if your “Bundle Identifier” is OK

10. Download the “Extra” folder provided at the beginning of the tutorial and copy the “Unity” folder from it into the Xcode project folder in Finder

11. Now drag the folder into Xcode tree to link it with the options “Copy items if needed” and “Create groups” checked

12. Copy and paste the “Classes”, “Libraries” and “Data” folders from the Unity export folder into the Xcode project into the previous Unity folder created

13. Now drag the “Classes” and “Libraries” folders into Xcode tree to link it with the options “Copy items if needed” and “Create groups” checked

14. Then, drag the “Data” folder into Xcode but, only with the “Create folder references” option

You should obtain this

15. Go in the “General” tab of the project and set the “Unity” configuration profile

16. Now you can entirely replace the “AppDelegate.swift”, “Main.storyboard” and “ViewController.swift” files with the ones provided in the “Extra” folder or you can merge their content with your own files

17. If you want to replace them, select the “AppDelegate.swift”, “Main.storyboard” and “ViewController.swift” in the Xcode tree and delete them with the “Move to trash” option

18. Then, drag those 3 files provided in the extra folder in the Xcode project tree and copy them with the options “Copy items if needed” and “Create groups” checked.

19. If Xcode asks you to configure a bridging header select “Don’t Create”

Those two scripts were also provided by Jiulong Wang.

AppDelegate.swift

ViewController.swift

20. If you didn’t import our “Main.storyboard” file you can create it yourself and link buttons to the right functions in the “ViewController.swift”

21. Once you have your “Main.storyboard” and “ViewController.swift” done, check that the later is linked in the “Class” variable of the storyboard

22. To avoid some error with the “LaunchScreen.storyboard” file delete it and in the “General” tab of the project, set the “Main.storyboard” as the “Launch Screen File”

23. Now that everything is set up you should be able to select your iOS device and build your Xcode project on it

24. Once the app launched press “Start Unity” to start your game and press “Pause Unity” to pause and hide it. You can press “Start Unity” again to resume it.

--

--

IronEqual

Indie gaming studio. Bringing you awesome content randomly!