SpriteView 和 SwiftUI 的场景不会填满整个屏幕
Scene with SpriteView and SwiftUI does not fill whole screen
我正在尝试处理项目 1 并使用 SpriteKit 和 SwiftUI,但由于某种原因,场景从未在我的 iPad 上填满整个屏幕。
import SwiftUI
import SpriteKit
struct ContentView: View {
var scene: SKScene {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
let scene = GameScene()
scene.size = CGSize(width: screenHeight, height: screenWidth)
scene.scaleMode = .fill
scene.backgroundColor = .green
return scene
}
var body: some View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
print(screenWidth)
return SpriteView(scene: scene)
.frame(width: screenWidth, height: screenHeight)
.ignoresSafeArea()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
现在看起来像这样:
知道我在这里遗漏了什么吗?
最大
您在场景 属性 中有宽度和宽度参数。 (@aheze在评论中是怎么提到的)
最后通过将背景放在 ContentView 的 ZStack() 中解决了这个问题
var body: some View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
ZStack{
EmptyView()
Image("road")
.resizable()
.aspectRatio(contentMode: .fill)
.ignoresSafeArea()
SpriteView(scene: scene, options: [.allowsTransparency])
.frame(width: screenWidth, height: screenHeight)
.ignoresSafeArea()
}
}
}
出于某种原因,当我将它添加为 GameScene 的一部分时,它不会将纹理放大到全屏大小 class,无论我尝试什么比例:
let background = SKSpriteNode(imageNamed: "road")
background.zPosition = -1
// background.scale(to: CGSize(width: 1180, height: 700))
// background.scale(to: CGSize(width: CGFloat(1180), height: CGFloat(820)))
addChild(background)
我正在尝试处理项目 1 并使用 SpriteKit 和 SwiftUI,但由于某种原因,场景从未在我的 iPad 上填满整个屏幕。
import SwiftUI
import SpriteKit
struct ContentView: View {
var scene: SKScene {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
let scene = GameScene()
scene.size = CGSize(width: screenHeight, height: screenWidth)
scene.scaleMode = .fill
scene.backgroundColor = .green
return scene
}
var body: some View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
print(screenWidth)
return SpriteView(scene: scene)
.frame(width: screenWidth, height: screenHeight)
.ignoresSafeArea()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
现在看起来像这样:
知道我在这里遗漏了什么吗?
最大
您在场景 属性 中有宽度和宽度参数。 (@aheze在评论中是怎么提到的)
最后通过将背景放在 ContentView 的 ZStack() 中解决了这个问题
var body: some View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
ZStack{
EmptyView()
Image("road")
.resizable()
.aspectRatio(contentMode: .fill)
.ignoresSafeArea()
SpriteView(scene: scene, options: [.allowsTransparency])
.frame(width: screenWidth, height: screenHeight)
.ignoresSafeArea()
}
}
}
出于某种原因,当我将它添加为 GameScene 的一部分时,它不会将纹理放大到全屏大小 class,无论我尝试什么比例:
let background = SKSpriteNode(imageNamed: "road")
background.zPosition = -1
// background.scale(to: CGSize(width: 1180, height: 700))
// background.scale(to: CGSize(width: CGFloat(1180), height: CGFloat(820)))
addChild(background)