我正在开发 SWIFT3/SpriteKit 屏幕保护程序,我的屏幕保护程序可以正常工作,但我无法加载我的纹理,因为当我执行以下操作时:
// load texture atlas
let textureAtlas = SKTextureAtlas(named: "Flyers")
SKTextureAtlas 在 screensaverengine.app
中运行时在主包中寻找“传单”,这不是纹理所在的包。
在我旧的基于 obj-c 的屏幕保护程序中,我必须执行以下操作才能从包中加载图像:
thisBundle = [NSBundle bundleForClass:[self class]];
fileName=[[NSString alloc ] initWithFormat:@"flyer%d", flyNum];
picturePath = [thisBundle pathForResource:fileName ofType:@"png"];
flyerImage= [[NSImage alloc ] initWithContentsOfFile:picturePath];
在我开始在运行时加载图像和创建纹理之前,我想利用 Stackoverflow 家族寻求帮助……有什么想法吗?
最佳答案
我从未使用过 SpriteKit。
但是查看文档,从 iOS 8 开始,您可以使用 UIImage(named: imageName, in: bundle, compatibleWith: nil)
从任何捆绑,然后使用这些图像的字典轻松实例化您的图集:
let bundle = Bundle(for: type(of: self))
var dictionary = [String:Any]()
["textureName": "imageName"].forEach { textureName, imageName in
dictionary[textureName] = UIImage(named: imageName, in: bundle, compatibleWith: nil)
}
let atlas = SKTextureAtlas(dictionary: dictionary)
关于swift3 - SKTextureAtlas 可以从 "my"包而不是 "host"包加载包吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41174487/