简体   繁体   English

SpriteKit - 如何处理多种屏幕尺寸?

[英]SpriteKit - How do I handle multiple screen sizes?

My new game is finished and now I'm just testing on multiple real devices.我的新游戏已经完成,现在我只是在多个真实设备上进行测试。 I came across multiple issues after testing.测试后我遇到了多个问题。 The big issue is how to handle screen sizes.最大的问题是如何处理屏幕尺寸。 The game is how I want it to look on 6 Plus/6s Plus, but not on 6s, 6, 5s, 5, 4s, 4, or iPad.游戏是我希望它在 6 Plus/6s Plus 上的样子,但不是在 6s、6、5s、5、4s、4 或 iPad 上的样子。

I found these two answers but I don't know how to implement them: How to support multiple screen sizes in SpriteKit?我找到了这两个答案,但我不知道如何实现它们:如何在 SpriteKit 中支持多种屏幕尺寸? and SpriteKit how to get correct screen sizeSpriteKit 如何获得正确的屏幕尺寸

I really would like any type of help, because this is irritating me.我真的很想得到任何类型的帮助,因为这让我很恼火。

I was reminded of checking the screen size and changing the node sizes, I found this as an answer: how to check screen size of iphone 4 and iphone 5 programmatically in swift我被提醒检查屏幕大小和更改节点大小,我发现这是一个答案: how to check screen size of iphone 4 and iphone 5 in swift programmatically

All I had to add was this in my GameScene and it was called in every .swift:我只需要在我的 GameScene 中添加这个,并且在每个 .swift 中调用它:

extension UIScreen {

enum SizeType: CGFloat {
    case Unknown = 0.0
    case iPhone4 = 960.0
    case iPhone5 = 1136.0
    case iPhone6 = 1334.0
    case iPhone6Plus = 1920.0
}

var sizeType: SizeType {
    let height = nativeBounds.height
    guard let sizeType = SizeType(rawValue: height) else { return .Unknown }
    return sizeType
}
}

And this和这个

if UIScreen.mainScreen().sizeType == .iPhone4 {
// Make specific layout for small devices.
}

Its quite a commonly asked question.这是一个很常见的问题。

What we usually do in SpriteKt is to give the SKScene a fixed size and let SpriteKit do the scaling for you on different devices.我们通常在 SpriteKt 中做的是给 SKScene 一个固定的大小,让 SpriteKit 在不同的设备上为你做缩放。

So basically we have 2 ways to do it correctly所以基本上我们有两种方法可以正确地做到这一点

1) Set scene size to iPad (eg 1024x768 -landscape, 768x1024 - portrait). 1) 将场景尺寸设置为 iPad(例如 1024x768 - 横向,768x1024 - 纵向)。 This was the default setting in Xcode 7.这是 Xcode 7 中的默认设置。

You than usually just have/show some extra background at the top/bottom (landscape) or left/right (portrait) on iPads which gets cropped on iPhones.您通常只在 iPad 上的顶部/底部(横向)或左侧/右侧(纵向)显示/显示一些额外的背景,而这些背景会在 iPhone 上被裁剪。

Examples of games that show more on iPads / crop on iPhones:在 iPad 上显示更多/在 iPhone 上裁剪的游戏示例:

Altos Adventure, Leos Fortune, Limbo, The Line Zen, Modern Combat 5. Altos Adventure、Leos Fortune、Limbo、The Line Zen、Modern Combat 5。

2) Apple changed the default scene size in xCode 8 to iPhone 6/7 (750*1334-Portait, 1337*750-Landscape). 2)Apple 将 xCode 8 中的默认场景尺寸更改为 iPhone 6/7(750*1334-Portait,1337*750-Landscape)。 This setting will crop your game on iPads.此设置将在 iPad 上裁剪您的游戏。

Examples of games that show less on iPads:在 iPad 上显示较少的游戏示例:

Lumino City, Robot Unicorn Attack鲁米诺城,机器人独角兽来袭

Choosing between those 2 options is up to you and depends what game you are making.在这两个选项之间进行选择取决于您,并取决于您正在制作的游戏。 I usually prefer to use option 1 and show more background on iPads.我通常更喜欢使用选项 1 并在 iPad 上显示更多背景。

For scale mode it is usually best to either use .aspectFill or .aspectFit .对于缩放模式,通常最好使用.aspectFill.aspectFit

You would use the Universal asset slot and/or device specific images.您将使用通用资产插槽和/或设备特定图像。 This way you will have a consistent experience on all devices这样您就可以在所有设备上获得一致的体验

Spritekit scale full game to iPad Spritekit 将完整游戏扩展到 iPad

How to make SKScene have fixed width? 如何使 SKScene 具有固定宽度?

Hope this helps希望这可以帮助

In my case I have a portrait game, and I want to keep the height fixed while showing more or less content left/right depending on the device's screen.在我的情况下,我有一个纵向游戏,我想保持height固定,同时根据设备的屏幕向左/向右显示更多或更少的内容。

For that in Xcode's Scene Editor I added and set an SKCameraNode for my scene, and set the scene size to the "widest" aspect ratio device (iPhone X ), that is 2436x1125 pixels.为此,我在 Xcode 的场景编辑器中为我的场景添加并设置了SKCameraNode ,并将场景大小设置为“最宽”纵横比设备(iPhone X),即2436x1125像素。

在此处输入图片说明

Then set a custom class for the scene and override sceneDidLoad :然后为场景设置一个自定义类并覆盖sceneDidLoad

override func sceneDidLoad()
{
    super.sceneDidLoad()

    self.size.width = self.size.height * (UIScreen.main.bounds.size.width / UIScreen.main.bounds.size.height)
    self.scaleMode = .aspectFit
}

If I want to preview how my game will look in the "narrowest" device (any iPad has a 1.5:1 ratio) I just temporarily change my scene's width to around 1500 pixels.如果我想预览我的游戏在“最窄”设备(任何 iPad 的比例为 1.5:1)中的外观,我只是暂时将场景的width更改为1500像素左右。

在此处输入图片说明

Note: SKView 's contentMode changes nothing.注意: SKViewcontentMode没有任何改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM