Skip to content

Commit 3bedcad

Browse files
committed
add README
1 parent 05e6cee commit 3bedcad

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# unity-swiftui
2+
3+
This is a _reference project_ for those that want to use [Unity as a Library](https://unity.com/features/unity-as-a-library) in their SwiftUI iOS app. Unity's [existing iOS documentation](https://github.com/Unity-Technologies/uaal-example/blob/master/docs/ios.md) only demonstrates UAAL with UIKit/Objective-C projects and lacks illustrative examples of native-to-unity state flow, control of the Unity view, and automated integration of Unity's build output.
4+
5+
![Screen recording](recording.gif)
6+
7+
## Requirements
8+
9+
- Physical device with iOS 17.5 or later.
10+
- Unity Editor 2022.3.36f1 - latest LTS at time of this writing - with iOS build support installed in Unity Hub.
11+
12+
## Instructions
13+
14+
1. **Clone repository**
15+
```bash
16+
git clone https://github.com/bdeweygit/unity-swiftui.git
17+
cd unity-swiftui
18+
```
19+
20+
2. **Create build folder for Unity project**
21+
```bash
22+
mkdir -p UnityProject/Builds/iOS
23+
```
24+
25+
3. **Open Unity project**
26+
- Use Unity Hub to open `UnityProject` directory.
27+
- Go to `File > Build Settings`.
28+
- In platform menu, select `iOS` and click `Switch Platform`.
29+
- Click `Build` and choose `Builds/iOS` folder you created earlier.
30+
31+
4. **Open integration workspace**
32+
- Use Xcode to open `integration.xcworkspace` file.
33+
34+
5. **Configure SwiftUI project**
35+
- Edit `UnitySwiftUI` target's signing settings with valid bundle identifier and signing team ID.
36+
- Switch to `UnitySwiftUI` scheme.
37+
38+
6. **Run app on a physical device**
39+
- Connect your physical device to Xcode.
40+
- Build and run `UnitySwiftUI` scheme to your device.
41+
42+
## Key features
43+
44+
- **Control of Unity view** as a SwiftUI element that can be repositioned and resized. Unity's [documentation](https://docs.unity3d.com/2022.3/Documentation/Manual/UnityasaLibrary-iOS.html) says "Unity as a Library only supports full-screen rendering, and doesn’t support rendering on part of the screen." We have fixed this limitation.
45+
- **Touches and gestures** on Unity view are processed within Unity.
46+
- **Restarting** the Unity player.
47+
- **State data** is sent from native Swift to Unity C# as a struct with members including string, boolean, floating point, integer, and MTLTexture types.
48+
49+
## Key files
50+
51+
### SwiftUI Project
52+
53+
- [**Unity.swift**](SwiftUIProject/UnitySwiftUI/Unity.swift): Class for controlling Unity framework and native-to-unity state flow.
54+
- [**ContentView.swift**](SwiftUIProject/UnitySwiftUI/ContentView.swift): UI demonstrating Unity view as a SwiftUI element and controls for layout and state.
55+
56+
### Unity Project
57+
58+
- [**NativeStateManager.cs**](UnityProject/Assets/Scripts/NativeStateManager.cs): Class for receiving and storing native state data.
59+
- [**RootBehavior.cs**](UnityProject/Assets/Scripts/RootBehavior.cs): Script that renders content - a cube and a spotlight - reacting to state updates and touch events.
60+
- [**NativeState.h**](UnityProject/Assets/Plugins/iOS/NativeState.h), [**NativeState.m**](UnityProject/Assets/Plugins/iOS/NativeState.m): Plugin enabling flow of state data from native Swift to Unity C#.
61+
- [**UnityFramework.modulemap**](UnityProject/Assets/Plugins/iOS/UnityFramework.modulemap): Custom modulemap for plugin interoperability with Swift.
62+
- [**PostProcessBuild.cs**](UnityProject/Assets/Editor/PostProcessBuild.cs): Script automating integration of Unity generated Xcode project.
63+
64+
## Known issues
65+
- After building Unity project, Xcode workspace may not always reflect latest files, be unable to build. Close and reopen workspace to refresh it and fix this bad state.
66+
- When running app while attached to Xcode, there may be some noticeable delay between device orientation change and UI layout. There should be no such delay when running detached.

0 commit comments

Comments
 (0)