简体   繁体   English

简单SpriteKit场景中的资产大小问题

[英]Issue with asset sizing in simple SpriteKit scene

I have set up my Scene as follows: 我的场景设置如下:

override func viewDidLayoutSubviews() {

    let scene = GameScene(size: CGSize(width: 1334, height: 750))
    let skView = view as! SKView
    scene.backgroundColor = UIColor.red
    scene.scaleMode = .aspectFit
    skView.presentScene(scene)
    skView.showsFPS = true
    skView.showsNodeCount = true
    print("Screen Size: \(GlobalProperties.screenSize.width) x \(GlobalProperties.screenSize.height)")
    print("Scene Size: \(scene.size.width) x \(scene.size.height)")
} 

As you can see I set the default scene size to 1334 x 750 which is the pixel resolution of iPhone 6,7,8 如您所见,我将默认场景大小设置为1334 x 750,这是iPhone 6,7,8的像素分辨率

I created a 90x50 asset as well as a background and inserted it as follows: 我创建了一个90x50的资源以及一个背景,并将其插入如下:

override func didMove(to view: SKView) {
    let bg = SKSpriteNode(texture: SKTexture(imageNamed: "DemoBackgroundVertical"))
    bg.position = CGPoint(x: frame.midX, y: frame.midY)
    bg.zPosition = -10
    bg.size = frame.size
    self.addChild(bg)

    let melon = SKSpriteNode(texture: SKTexture(imageNamed: "CuteMelon"))
    melon.position = CGPoint(x: frame.midX, y: frame.midY)
    print(melon.size)
    self.addChild(melon)
}

So now the scene size in points is 667 x 375, the scene size in pixels is 1334 x 750, and I placed the asset in the 1x Universal group 因此,现在以点为单位的场景大小为667 x 375,以像素为单位的场景大小为1334 x 750, 我将素材资源放置在1x通用组中

Am I correct that while running this on an iPhone 8 simulator the scale mode should not matter as the aspect ratio is identical to the 1334 x 750 size I set the scene to initially? 我是否正确,因为在iPhone 8模拟器上运行此模式时,缩放模式应该无关紧要,因为纵横比与将场景设置为最初的1334 x 750尺寸相同?

Here is the issue, if in Photoshop I create an identical scene of 667 x 375 and insert a 90x50 asset it looks like this : https://imgur.com/a/BuW2HLI 这是问题所在,如果在Photoshop中我创建了667 x 375的相同场景并插入一个90x50的资源,它看起来像这样: https : //imgur.com/a/BuW2HLI

But a screenshot of the iphone 8 simulator has the watermelon asset much smaller relatively as such: https://imgur.com/flMtdfG 但是iphone 8模拟器的屏幕截图显示,西瓜资产相对要小得多: https : //imgur.com/flMtdfG

Why is this the case? 为什么会这样? Any guidance would be greatly appreciated. 任何指导将不胜感激。

In your case, you want to be using the 2x slot or reorganize your project in a better way. 在您的情况下,您想使用2x插槽或以更好的方式重组项目。

Setting the default scene size to 1334x750 gets you a scene size of 1334x750, not 667x375, as in 2668 pixels by 1500 pixels . 将默认场景大小设置为1334x750,可以得到1334x750(而不是667x375)的场景大小,例如2668 x 1500像素。 This means your SpriteKit engine is actually scaling the scene down from 1334x750 to 667x375, then scaling it back up again. 这意味着您的SpriteKit引擎实际上是将场景从1334x750缩小到667x375,然后再次将其放大。 Since coordinates use CGPoint, I always find this as an unnecessary step because 0.5 exists in this space and thus the large numbers causes confusion with people trying to understand how retina display works. 由于坐标使用CGPoint,因此我总是觉得这是不必要的步骤,因为该空间中存在0.5,因此,较大的数字会与试图了解视网膜显示工作原理的人们产生混淆。

So what you have is a 90x50 asset in a 1334x750 scene, not a 667x375 scene. 因此,您拥有的是1334x750场景中的90x50资源,而不是667x375场景。

First thing I would do is change the scene size to 667x375 so that you know you are working in the 1x coordinate system, not the 2x coordinate system. 我要做的第一件事是将场景大小更改为667x375,这样您就知道您正在使用1x坐标系,而不是2x坐标系。

Then, try to ignore the whole "pixel" concept. 然后,尝试忽略整个“像素”概念。 Stick strictly to points. 严格遵守要点。 Remember that 1x means points * 1 2x means points * 2 and 3x means points * 3. If you have this in mind. 请记住,1x表示点* 1 2x表示点* 2,3x表示点* 3。 you will find asset creation a lot simpler. 您会发现资产创建要简单得多。 Anything in your 1x slot will be the actual size your game uses, and anything placed in your 2x slot and your 3x slot will be double and triple the size respectively, only showing higher detailed graphics in your game without having to do anything with the code 1x插槽中的任何内容都将是您游戏实际使用的尺寸,而2x插槽和3x插槽中放置的任何内容都将分别为尺寸的两倍和三倍,仅显示游戏中更详细的图形,而无需执行任何代码

Finally, place your low res 1x graphics into the 1x slot. 最后,将您的低分辨率1x图形放入1x插槽。 You will now see that all of your sizes should be mapping correctly. 现在,您将看到所有尺寸都应正确映射。 If the 2x hardware scaling does not look pretty, provide the assets into their separate 2x slots to provide better details to the images you need. 如果2倍的硬件缩放效果看起来不太理想,请将资产放入其单独的2倍插槽中,以提供所需图像的更好细节。

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

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