@@ -11,31 +11,31 @@ struct ContentView: View {
11
11
@State private var loading = false
12
12
@State private var showState = false
13
13
@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
16
16
17
17
@ObservedObject private var unity = Unity . shared
18
18
19
19
var body : some View {
20
20
ZStack ( alignment: . bottomLeading, content: {
21
- if loading { // Unity is starting up
21
+ if loading {
22
+ // Unity is starting up or shutting down
22
23
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 {
27
27
case . fullscreen:
28
28
UnityContainer . ignoresSafeArea ( )
29
29
case . safearea:
30
30
UnityContainer
31
31
case . aspect, . square:
32
- let isAspect = playerDisplay == . aspect
32
+ let isAspect = display == . aspect
33
33
GeometryReader ( content: { geometry in
34
34
let aspect = geometry. size. applying ( CGAffineTransform ( scaleX: 0.5 , y: 0.5 ) )
35
35
let square = min ( aspect. width, aspect. height)
36
36
let width = isAspect ? aspect. width : square
37
37
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 )
39
39
} )
40
40
}
41
41
VStack ( alignment: . leading, content: {
@@ -62,14 +62,14 @@ struct ContentView: View {
62
62
} )
63
63
}
64
64
if showLayout {
65
- Picker ( " Player display " , selection: $playerDisplay , content: {
65
+ Picker ( " Display " , selection: $display , content: {
66
66
Text ( " Square " ) . tag ( Display . square)
67
67
Text ( " Aspect " ) . tag ( Display . aspect)
68
68
Text ( " Safe area " ) . tag ( Display . safearea)
69
69
Text ( " Fullscreen " ) . tag ( Display . fullscreen)
70
70
} )
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: {
73
73
Text ( " Top " ) . tag ( Alignment . top)
74
74
Text ( " Center " ) . tag ( Alignment . center)
75
75
Text ( " Bottom " ) . tag ( Alignment . bottom)
@@ -100,7 +100,8 @@ struct ContentView: View {
100
100
} )
101
101
} )
102
102
} )
103
- } else { // Unity is not running
103
+ } else {
104
+ // Unity is not running
104
105
Button ( " Start Unity " , systemImage: " play " , action: {
105
106
/* Unity startup is slow and must must occur on the
106
107
main thread. Use async dispatch so we can re-render
@@ -132,8 +133,10 @@ fileprivate struct CustomButtonStyle: PrimitiveButtonStyle {
132
133
}
133
134
134
135
/* 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 {
137
140
public func hash( into hasher: inout Hasher ) {
138
141
switch self {
139
142
case . top: hasher. combine ( 0 )
0 commit comments