SwiftUI and Unity as Library

Hi Guys,

I am new to native iOS development. Just starting with Swift UI. After lots of failed attempts to create beautiful UI in Unity I stumbled upon Swift UI and Unity as a library.

From Unity talk and some online resources it looks straight forward. Unfortunately all example are very old from Unity 5 times.

Does anyone has some up to date information how to integrate Swift UI and Unity 2019/2020?
If someone can give me an example how to integrate a simple spinning cube it will be more than enough to get me started.

Thank you for help.

Hi, I’m looking for the same thing, did you find a way to do it ?
Please let me know if you succeed, and I will do the same if I find something working.

No unfortunately no. There are lots of information, but it is outdated or not working.

I have not tried doing this myself, all the following information are deducted through a typical iOS framework integration workflow, but I hope the information could give you some lead.

Since UaaL is a new thing since 2019.3 (I assume you know this), it has to be one of the versions newer in order to first build a unity framework for embedding into your main app.

Once you exported the Xcode project from Unity, build the framework by selecting the UnityFramework scheme in Xcode (top left corner) and select Any iOS Device to make sure building all the supported archs.

Once you have the thing built, find Products > UnityFramework.framework in project navigator, right click and Show in Finder. It will open Finder on that build folder and you can copy the UnityFramework.framework out to somewhere else. This is the executable for your Unity project.

As for the assets, copy the entire Data folder in the root of the exported Unity project.

Be sure to build on release mode after you are done with testing. See the following screenshots on how to change the build type.

Then, include and embed the framework into your main app as you normally would with any other iOS frameworks. Include the copied Data folder in the Copy Bundle Resources build phase of your app.

Take a look at function UnityFrameworkLoad() in MainApp/main.mm of the Unity Xcode project to see a reference on how to load the Unity library. After that, since I have not done this myself, you probably need to call method -runEmbeddedWithArgc:argv:appLaunchOpts: and then -showUnityWindow. The header file <UnityFramework/UnityFramework.h> should give you an idea what the interface looks like.

The forum only allows 5 attachments per post. Here the image of what the build folder looks like.

Here is another tip. Before you integrate the framework into your main app or update it, you should test your Unity project standalone (with the app target that the Unity Xcode project comes with) to help isolating issues.

1 Like

Thank you @Neonlyte . It is very helpful. I will definitely try it…

Hi Neonlyte, thanks you so much for helping us !
@Artpen : Did you tried Neonlyte’s method ? Did it work ?

Hey @Neonlyte thanks for this. One question, any reason I can’t build/use multiple UnityFramework’s in a single XCode project? I need to compile for each SDK (arm64 for iOS and x86 for simulator) as SwiftUI’s previews use the iOS simulator. I thought I could associate each UnityFramework with each build target in XCode but haven’t been able to get it working yet. TIA!

Sorry, I have no further knowledge in this use case. I tried to integrate frameworks to work with SwiftUI in a non-Unity project, and it was pain.

Edit: See if this matches what you were trying to do:
https://developer.apple.com/forums/thread/86759

Thanks, much appreciated

Hey @Neonlyte sorry to ping you again, but still trying to get this working.

Any idea if Uaal/UnitFramework is possibly stripping compiled architectures from custom plugins included in Unity? For the life of me I can’t get this running in the simulator w/ a simple bridge plugin, lipo shows UnityFramework w/ x86_64, but compiling the Swift project using Uaal fails w/ an x86_64 linker error for the plugin…

More details → here .

Thanks in advance, really appreciate it.

Yeah, it is pretty hard to integrate SwiftUI and Unity but if you ask me it works especially good when combining SwiftUI with UIKit. Though some may think it may kill the last one :slight_smile: