Skip to content

Commit d7ce092

Browse files
committed
silence retro conformance warning; refactor some vars
1 parent bcb1dd1 commit d7ce092

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

SwiftUIProject/UnitySwiftUI/ContentView.swift

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ struct ContentView: View {
1111
@State private var loading = false
1212
@State private var showState = false
1313
@State private var showLayout = false
14-
@State private var playerDisplay = Display.square
15-
@State private var playerAlignment = Alignment.top
14+
@State private var display = Display.square
15+
@State private var alignment = Alignment.top
1616

1717
@ObservedObject private var unity = Unity.shared
1818

1919
var body: some View {
2020
ZStack(alignment: .bottomLeading, content: {
21-
if loading { // Unity is starting up
21+
if loading {
22+
// Unity is starting up or shutting down
2223
ProgressView("Loading...").tint(.white).foregroundStyle(.white)
23-
} else if let unityView = unity.view { // Unity is running
24-
// Create a container for Unity's UIView.
25-
let UnityContainer = UIViewContainer(containee: unityView)
26-
switch playerDisplay {
24+
} else if let UnityContainer = unity.view.flatMap({ UIViewContainer(containee: $0) }) {
25+
// Unity is running
26+
switch display {
2727
case .fullscreen:
2828
UnityContainer.ignoresSafeArea()
2929
case .safearea:
3030
UnityContainer
3131
case .aspect, .square:
32-
let isAspect = playerDisplay == .aspect
32+
let isAspect = display == .aspect
3333
GeometryReader(content: { geometry in
3434
let aspect = geometry.size.applying(CGAffineTransform(scaleX: 0.5, y: 0.5))
3535
let square = min(aspect.width, aspect.height)
3636
let width = isAspect ? aspect.width : square
3737
let height = isAspect ? aspect.height : square
38-
UnityContainer.frame(width: width, height: height).frame(maxWidth: .infinity, maxHeight: .infinity, alignment: playerAlignment)
38+
UnityContainer.frame(width: width, height: height).frame(maxWidth: .infinity, maxHeight: .infinity, alignment: alignment)
3939
})
4040
}
4141
VStack(alignment: .leading, content: {
@@ -62,14 +62,14 @@ struct ContentView: View {
6262
})
6363
}
6464
if showLayout {
65-
Picker("Player display", selection: $playerDisplay, content: {
65+
Picker("Display", selection: $display, content: {
6666
Text("Square").tag(Display.square)
6767
Text("Aspect").tag(Display.aspect)
6868
Text("Safe area").tag(Display.safearea)
6969
Text("Fullscreen").tag(Display.fullscreen)
7070
})
71-
if playerDisplay == .aspect || playerDisplay == .square {
72-
Picker("Player alignment", selection: $playerAlignment, content: {
71+
if display == .aspect || display == .square {
72+
Picker("Alignment", selection: $alignment, content: {
7373
Text("Top").tag(Alignment.top)
7474
Text("Center").tag(Alignment.center)
7575
Text("Bottom").tag(Alignment.bottom)
@@ -100,7 +100,8 @@ struct ContentView: View {
100100
})
101101
})
102102
})
103-
} else { // Unity is not running
103+
} else {
104+
// Unity is not running
104105
Button("Start Unity", systemImage: "play", action: {
105106
/* Unity startup is slow and must must occur on the
106107
main thread. Use async dispatch so we can re-render
@@ -132,8 +133,10 @@ fileprivate struct CustomButtonStyle: PrimitiveButtonStyle {
132133
}
133134

134135
/* Make alignment hashable so it can be used as a
135-
picker selection. We only care about top, center, and bottom. */
136-
extension Alignment: Hashable {
136+
picker selection. We only care about top, center,
137+
and bottom. Retroactive conformance is a bad practice
138+
but is much more laconic than writing out a wrapper type. */
139+
extension Alignment: @retroactive Hashable {
137140
public func hash(into hasher: inout Hasher) {
138141
switch self {
139142
case .top: hasher.combine(0)

SwiftUIProject/UnitySwiftUI/Unity.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Unity: SetsNativeState, ObservableObject {
5555
})
5656
loadingGroup.wait()
5757

58-
/* The player finishes starting - runEmbedded() returns - before completing
58+
/* Unity finishes starting - runEmbedded() returns - before completing
5959
its first render. If the view is displayed immediately it often shows the
6060
content leftover from the previous run until Unity renders again and overwrites it.
6161
Clearing Unity's layer with transparent color before restart hides this brief artifact. */
@@ -79,7 +79,7 @@ class Unity: SetsNativeState, ObservableObject {
7979
}
8080
}
8181

82-
// Start the player
82+
// Start Unity
8383
framework.runEmbedded(withArgc: CommandLine.argc, argv: CommandLine.unsafeArgv, appLaunchOpts: nil)
8484

8585
// Hide Unity's UIWindow so it won't display UIView or intercept touches

0 commit comments

Comments
 (0)