DavidPeicho / unity-swiftui-example

Integration example of the Unity engine in a SwiftUI-based application
MIT License
70 stars 15 forks source link

unity-swiftui-example

This repository demonstrates how to integrate Unity 2020.2.1f1 into a Swift iOS app based on SwiftUI and built with XCode 12.

Example of Unity running in a native SwiftUI app

This repository is heavily based on the example published by Unity Technologies available here.

Features

The last point will allow you to transfer heavy data without the need to serialize it to a string beforehand.

Build

1, Unity Project

Screenshot showing where I build the Unity package

2. XCode Project

Screenshot showing where to add the UnityFramework entry

How

For a complete explanation of the architecture of this example, please have a look at my two blog posts:

The second link is an improvement of the first one. The main branch of this repository is in the same state as the second blog post.

FAQ

Unity doesn't render to my app

Between the moment you create the UnityFramework instance, and the moment you can display it, it seems like there is an issue. Some people try to add a delay, but this is definitely a hack.

In this example, it is assumed that whenever the API script starts, the Unity window should be ready to be shown. This is achieved by sending a first message from Unity to the native code. Whether this will work in every cases or not is questionable, but for now it has done a good job for me.