本文介绍了使用 SKRenderer 渲染文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的项目中初始化了 SKRenderer 以将它与金属一起使用.虽然我不确定我做得是否正确,但我需要知道如何使用它渲染简单的 2D 文本?
I initialized SKRenderer in my project to use it alongside of metal. Although I'm not sure that I did it correctly, I need to know how I can render a simple 2D text with it?
例如,这里是我们如何使用 SKLabelNode
绘制文本的方法,我们是否有类似的 SKRenderer 方法?非常感谢
For example, here is how we can draw a text with SKLabelNode
, do we have anything like it for SKRenderer?thank you so much
let winner = SKLabelNode(fontNamed: "Chalkduster")
winner.text = "You Win!"
winner.fontSize = 65
winner.fontColor = SKColor.red
winner.position = CGPoint(x: 100, y: 100)
view.addChild(winner)
推荐答案
您需要做的第一件事是初始化场景.
The first thing you need to do is initialize the scene.
var renderer: SKRenderer
var skScene: SKScene
在您的 init 函数中:
in your init function:
renderer = SKRenderer(device: device)
// iphone 11 portrait
skScene = SKScene(size: CGSize(width: 1125, height: 2436))
renderer.scene = skScene
let winner = SKLabelNode(fontNamed: "Chalkduster")
winner.text = "You Win!"
winner.fontSize = 65
winner.fontColor = SKColor.red
winner.position = CGPoint(x: 1125 / 2, y: 2436 / 2)
skScene.addChild(winner)
最后在你的渲染函数中:
and finally in your render function:
renderer.render(withViewport: viewport, renderCommandEncoder: renderEncoder, renderPassDescriptor: renderPassDescriptor, commandQueue: commandQueue)
renderEncoder.endEncoding()
但请记住 ui 层 (SKRenderer) 应在最后一遍渲染.
but keep in mind ui layer (SKRenderer) should be rendered in the last pass.
这篇关于使用 SKRenderer 渲染文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!