【问题标题】:Can SKTextureAtlas load bundle from "my" bundle instead of "host" bundle?SKTextureAtlas 可以从“我的”包而不是“主机”包加载包吗?
【发布时间】:2016-12-15 22:38:13
【问题描述】:

我正在开发 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 系列寻求帮助......想法?

【问题讨论】:

    标签: swift3 texturepacker sktexture texture-atlas sktextureatlas


    【解决方案1】:

    我从未使用过 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 2019-01-30
      • 2019-11-29
      • 2021-02-08
      相关资源
      最近更新 更多