如何确定SKTexture是否为占位符图像

Wes*_*ton 6 objective-c ios sprite-kit

在SpriteKit中,如果使用[SKTexture textureWithImageNamed:]方法加载图像,它将首先搜索您的包,然后搜索地图集,如果找不到您的纹理,它将创建一个占位符图像并加载它.

有没有办法找出(没有目测检查)加载的图像是否是占位符?

这似乎不是一种非常"可可"的方式来处理这种类型的错误.

developer.apple.com现在已经关闭了,所以我想我会向SO提出这个问题.

Kla*_*aas 1

SKTexture 有一些有用的私有属性(参见例如https://github.com/luisobo/Xcode-RuntimeHeaders/blob/master/SpriteKit/SKTexture.h),但在 Swift 和开发过程中我使用这个:

extension SKTexture {
    public var isPlaceholderTexture:Bool {
        if size() != CGSize(width: 128, height: 128) {
            return false
        }

        return String(describing: self).contains("'MissingResource.png'")
    }
}
Run Code Online (Sandbox Code Playgroud)

注意力!

仅当您使用以下命令时,此方法才有效SKTextureAtlas

let t1 = SKTextureAtlas(named: "MySprites").textureNamed("NonExistingFoo")
// t1.isPlaceholderTexture == true

let t2 = SKTexture(imageNamed: "NonExistingBar")
// t2.isPlaceholderTexture == false
Run Code Online (Sandbox Code Playgroud)