SKTextureAtlas 不再在 iOS 10 中共享纹理 [英] SKTextureAtlas no longer sharing textures in iOS 10

查看:34
本文介绍了SKTextureAtlas 不再在 iOS 10 中共享纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在从纹理图集中提取纹理时,我现在正在生成新纹理,而不是在 iOS 10 的不同精灵中使用相同的纹理.在 iOS 9 上,这可以按预期工作.还有其他人遇到这个问题吗?也许我错过了一个步骤,它现在是 iOS 10 的一部分.

It seems that when pulling textures from a texture atlas, I am now generating new textures instead of using the same texture across different sprites with iOS 10. On iOS 9, this works as expected. Is anyone else experiencing this issue? Perhaps there is a step I missed that is now a part of iOS 10.

注意:我创建了一个示例项目并创建了一个新图集,然后只是将宇宙飞船拖到@1x 中,我也尝试过预加载,但也没有用.

Notes: I created a sample project and created a new atlas, then just dragged spaceship in @1x, I have also tried preloading, and that did nothing as well.

代码:

  let atlas = SKTexturAtlas(named:"Sprites")
  var texture = atlas.textureNamed("Spaceship")
  print("(Unmanaged.passUnretained(texture)),(Unmanaged.passUnretained(texture).toOpaque())")

  texture = atlas.textureNamed("Spaceship")
  print("(Unmanaged.passUnretained(texture)),(Unmanaged.passUnretained(texture).toOpaque())")

为了解决比较问题,我使用描述属性来比较两个纹理是否相等.但是,要使其正常工作,您不能使用 2 个图集,每个图集都包含具有确切名称和大小的纹理.我永远不会遇到这种情况,但对于任何寻求帮助的人,请记住这一点.

To get around issues of comparison, I use the description property to compare if 2 textures are equal. For this to work though, you can't be using 2 atlases that each contain a texture with an exact name and size. I will never hit this situation, but for anybody out there looking for help, keep this in mind.

推荐答案

为了解决这个问题,我必须想出一种方法来缓存纹理,这样它就不会重复:

To get around this issue, I had to come up with a way to cache the textures so that it doesn't duplicate:

private var textureCache = [String: SKTexture]()
extension SKTextureAtlas
{
    func texturesWithNames(_ names:[String]) -> [SKTexture]
    {
        var textures = [SKTexture]()
        names.forEach({textures.append(textureNamed($0))})
        return textures
    }
    func cachedTextureWithName(_ name:String) -> SKTexture
    {
        if  textureCache[name] == nil
        {

            textureCache[name] = textureNamed(name)
        }

        return textureCache[name]!
    }
    func cachedTexturesWithNames(_ names:[String]) -> [SKTexture]
    {
        var textures = [SKTexture]()
        names.forEach({textures.append(cachedTextureWithName($0))})
        return textures
    }
    func clearCache()
    {
        textureCache = [String: SKTexture]()
    }
}


extension SKTexture
{

    var name : String
    {
        return self.description.slice(start: "'",to: "'")!
    }


}

这篇关于SKTextureAtlas 不再在 iOS 10 中共享纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆