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

查看:45
本文介绍了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())")

为了解决比较问题,我使用description属性比较两个纹理是否相等.但是要使此工作正常,您不能使用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天全站免登陆