优化SpriteKit

时间:2014-09-22 21:51:45

标签: ios performance optimization sprite-kit

这里我正在优化我的sprite-kit代码,试图测试我的游戏引擎的界限。

使用仪器时间分析我修复了我自己的大多数问题,现在唯一的瓶颈是

37.0ms   66.0%  0,0       -[SKTextureAtlas textureNamed:]

(我的游戏每半秒从TextureAtlases加载很多精灵) 奇怪的是,似乎在我的大部分测试中,似乎纯粹的NSString比较名称导致上述方法中的三分之一以上的工作。 这是子树:

15.0ms   26.7%  0,0         -[NSString caseInsensitiveCompare:]
15.0ms   26.7%  0,0          -[NSString compare:options:range:]
15.0ms   26.7%  0,0           -[NSString compare:options:range:locale:]
15.0ms   26.7%  6,0            CFStringCompareWithOptionsAndLocale

对于性能如此重要的东西,苹果提供的字符串比较真的很糟糕吗?为什么它们不能提供更简单的方法来访问地图册中的纹理,我们这里不需要花哨的字符串,它可以由我们所关心的int迭代。

有任何比较经验吗?

2 个答案:

答案 0 :(得分:10)

是的,避免在游戏运行时{经常)使用textureNamed:。即使使用预加载的纹理,也需要花费时间累积。

而是在各个ivars或NSArray或NSDictionary ivars中本地(在使用它们的类中)缓存所需的纹理。

答案 1 :(得分:1)

我做了一个Swift实现@ LearnCocos2D的答案,这是为了避免重复加载相同的纹理。我创建了一个带有静态纹理变量的枚举和一个访问它们的getter:

enum GameTextures {
   case Hero
   case Foe

   private static let heroTexture = SKTexture(image: UIImage(named: "hero-texture") ?? UIImage())
   private static let foeTexture = SKTexture(image: UIImage(named: "foe-texture") ?? UIImage())

   var texture: UIImage {
      get {
         switch self {
         case .Hero: return GameTextures.heroTexture
         case .Foe: return GameTextures.foeTexture
      }
   }
}

因此,当我需要纹理时,我可以简单地执行此操作:

heroSpriteNode.texture = GameTextures.Hero.texture
相关问题