Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use multiple asset folders in xCode

So I am working on a game in SpriteKit. I have a function that generates a level for the player. The function uses images placed in assets.xcassets to generate levels. The game has multiple themes , namely fire water and electricity. Each theme has its own set of images with the same names but each theme image set is in its separate folder/group. Is it possible for me to programmatically tell the function to use a specific folder so that I can generate levels for other themes without renaming images. Or is it possible for me to use multiple asset folders and change which one the function uses at runtime. I want to use a random theme every time the player restarts the level. assets folder

like image 781
Aman Kapoor Avatar asked Aug 17 '18 04:08

Aman Kapoor


People also ask

How do I manage assets in Xcode?

Xcode simplifies managing most types of assets with asset catalogs. Use asset catalogs to organize and manage resources such as images, colors, app icons, textures, stickers, and data. Xcode also provides interactive editors for certain types of assets, like particle effects, that let you experiment, make changes, and see the results immediately.

What is asset catalog in Xcode?

Xcode provides tools and settings to help you add, organize, and optimize the different asset types your app uses. Xcode simplifies managing most types of assets with asset catalogs. Use asset catalogs to organize and manage resources such as images, colors, app icons, textures, stickers, and data.

What can you do with Xcode?

Xcode also provides interactive editors for certain types of assets, like particle effects, that let you experiment, make changes, and see the results immediately. Add, organize, and edit sets of assets in your Xcode project using asset catalogs. Import images into your project, manage their appearances and variations, and load them at runtime.

How to add folders to Xcode project?

Besides files, you can also add folders into the Xcode project follow the below steps. Right-click the Xcode project in the project navigator. Click Add Files to ” project name “ menu item in the popup menu list. Select a folder, check Copy items if needed checkbox in Destination section.


Video Answer


1 Answers

Sprite Atlas / Sprite Sheet

You can use a Sprite Atlas (also known as Sprite Sheet).

If you don't know what I'm talking about have a look at these 2 nice videos:

  • Sprite Sheets - The Movie Part 1
  • Sprite Sheets - The Movie Pt. 2 - Performance

How to use it

If you have watched the videos now you know that the main target of a Sprite Atlas is loading into the GPU all and only (as far as possible) the images needed for the current scene or level. This way drawing each frame becomes much faster because all the resources al already on the GPU and the CPU only needs to transmit the coordinates.

Namespace

Ok but how does it solve your problem?

Xcode allows you to create a namespace for each Sprite Atlas. So you can use the same name for resources in different Sprite Atlas.

How to create a Sprite Atlas.

This is pretty simple.

  1. With Xcode open you Asset Catalog
  2. Tap on the + button you see in the image below

enter image description here

  1. Click on New Sprite Atlas
  2. You'll se a new "Folder", that's your Sprite Atlas.

enter image description here 5. Now click on the Provides Namespacecheckbox on the right.

That's it. Now just drop your images into the Sprites folder in order to add them to your sprite atlas.

In this example I'm going to rename the Sprite Atlas as Sprites0 and will add a red circle named circle.

Then

  • will create another Sprite Atlas
  • will check again the Provides Namespacecheckbox
  • will name Sprites1
  • and finally will add a green circle names circle.

Here's the final result

enter image description here

Code

Let's see now how to use it.

import SpriteKit

class GameScene: SKScene {

    override func didMove(to view: SKView) {

        let textureAtlas0 = SKTextureAtlas(named: "Sprites0")
        let texture0 = textureAtlas0.textureNamed("circle")
        let sprite0 = SKSpriteNode(texture: texture0)
        sprite0.position.y = 100
        addChild(sprite0)

        let textureAtlas1 = SKTextureAtlas(named: "Sprites1")
        let texture1 = textureAtlas1.textureNamed("circle")
        let sprite1 = SKSpriteNode(texture: texture1)
        sprite1.position.y = -100
        addChild(sprite1)

    }
}

Result

enter image description here

How does it work

Step 1: you load the Sprite Atlas

let textureAtlas0 = SKTextureAtlas(named: "Sprites0")

Step 2: you load a specific texture from the texture atlas

let texture0 = textureAtlas0.textureNamed("circle")

Step 3: you use your texture as you want

let sprite0 = SKSpriteNode(texture: texture0)
sprite0.position.y = 100
addChild(sprite0)
like image 125
Luca Angeletti Avatar answered Oct 20 '22 00:10

Luca Angeletti