更改所有 SKTextures 的 SKTextureFilteringMode

Change SKTextureFilteringMode for all SKTextures

有没有办法将 filteringMode 属性分配给所有 SKTexturesSKTextureFilteringMode.nearest?除了单独分配给每个纹理。以下工作正常,但我宁愿不必遍历纹理,而只需为过滤模式设置默认值。这可能吗?

func walk () -> SKAction {

    let walkTexture1 = SKTexture(imageNamed: "walk1.png")
    let walkTexture2 = SKTexture(imageNamed: "walk2.png")
    let walkTexture3 = SKTexture(imageNamed: "walk3.png")
    let walkTexture4 = SKTexture(imageNamed: "walk4.png")
    let walkTexture5 = SKTexture(imageNamed: "walk5.png")


    let animationTextures: [SKTexture] = [walkTexture1, walkTexture2, walkTexture3, walkTexture4, walkTexture5]

    for texture in animationTextures {
        texture.filteringMode = SKTextureFilteringMode.nearest
    }

    let walkAnimation = SKAction.animate(with: animationTextures, timePerFrame: 0.3/5)

    return walkAnimation

在同一个循环中创建纹理和设置属性

let textures = (1...5).map { let texture = SKTexture(imageNamed: "walk\([=10=]).png") texture.filteringMode = SKTextureFilteringMode.nearest return texture }

或仅使用 forEach

设置属性

textures.forEach { [=12=].filteringMode = .nearest }

你或许可以对 SKTexture 做一个扩展,像这样:

extension SKTexture {
    class func pixeled(imageNamed imageName: String) -> SKTexture {
        let texture = SKTexture(imageNamed: imageName)
        texture.filteringMode = .nearest
        return texture
    }
}

那么你将拥有

let walkTexture1 = SKTexture.pixeled(imageNamed: "walk1.png")

等等