如何为使用 imageFromFile 创建的 SKTexture 着色?

How to colorize an SKTexture that is created with imageFromFile?

我创建了几种形状的png图像,例如六边形和星形,它们都是白色的。

我想给这些 SKSpriteNodes 或 SKTextures 着色,这样我就可以分配任何颜色,而不是为我需要的每种颜色创建形状图像并将这些纹理加载到内存中。我需要高分辨率的图像,所以拥有多个图像并将它们加载到内存中不是一个好的选择,因为我收到来自 XCode 的内存警告。在需要时将它们加载到内存中也不起作用,因为它们会导致我的游戏延迟大约 0.25 秒。

那么,有什么方法可以创建 1 个 SKTexture 并在需要更改颜色时为 SKSpriteNode 的图像纹理着色?

你可以像这样改变白色纹理的颜色(假设你使用的是纯白色纹理):

//Colorize to red

 let yourNode = SKSpriteNode(imageNamed: "textureName")
 yourNode.colorBlendFactor = 1
 yourNode.color = UIColor.redColor()

关于colorBlendFactor

The value must be a number between 0.0 and 1.0, inclusive. The default value (0.0) indicates the color property is ignored and that the texture’s values should be used unmodified. For values greater than 0.0, the texture is blended with the color before being drawn to the scene.

More about this topic on Whosebug.

希望对您有所帮助。