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

时间:2023-01-22 21:25:45

In SpriteKit, if you load an image with the [SKTexture textureWithImageNamed:] method, it will first search your bundles, then atlases, if it fails to find your texture, it will then create a placeholder image and load it.

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

Is there a way to find out (without visually checking) if the loaded image is a placeholder?

是否有一种方法可以发现(不需要视觉检查)加载的图像是否是占位符?

This doesn't seem like a very "cocoa" way to handle this type of error.

这似乎不是处理这种类型错误的非常“可可”的方法。

developer.apple.com is down now, so I figured I would pose this question to SO.

developer。apple。com现在宕机了,所以我想我可以提出这个问题。

2 个解决方案

#1


1  

SKTexture has some private properties that would be helpful (see e.g. https://github.com/luisobo/Xcode-RuntimeHeaders/blob/master/SpriteKit/SKTexture.h), but in Swift and during development I'm using this:

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'")
    }
}

Attention!:

注意!:

This only works, if you are using a SKTextureAtlas:

这只在你使用sktextextureatlas时有效:

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

let t2 = SKTexture(imageNamed: "NonExistingBar")
// t2.isPlaceholderTexture == false

#2


2  

You can check if the atlas contains the image. I created the following method in my AtlasHelper:

您可以检查地图集是否包含图像。我在我的AtlasHelper中创建了以下方法:

+(BOOL) isTextureWithName:(NSString *) name existsInAtlas:(NSString *)atlasName
{
    SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:atlasName];
    return [atlas.textureNames containsObject:name];
}

#1


1  

SKTexture has some private properties that would be helpful (see e.g. https://github.com/luisobo/Xcode-RuntimeHeaders/blob/master/SpriteKit/SKTexture.h), but in Swift and during development I'm using this:

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'")
    }
}

Attention!:

注意!:

This only works, if you are using a SKTextureAtlas:

这只在你使用sktextextureatlas时有效:

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

let t2 = SKTexture(imageNamed: "NonExistingBar")
// t2.isPlaceholderTexture == false

#2


2  

You can check if the atlas contains the image. I created the following method in my AtlasHelper:

您可以检查地图集是否包含图像。我在我的AtlasHelper中创建了以下方法:

+(BOOL) isTextureWithName:(NSString *) name existsInAtlas:(NSString *)atlasName
{
    SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:atlasName];
    return [atlas.textureNames containsObject:name];
}