Spritesheet to Unity

I’ve made a few posts already about spritesheets, atlases, etc, as I can’t seem to make up my mind. Especially, as my sprite needs change constantly, as I don’t really have complete art and I’m trying to get away with a bit of DCSS tiles and a bit of Oryx tiles at the moment.

Originally, I used a 2D texture atlas + a JSON file with the description of what sprites are where. It was a nightmare to edit. Also, in runtime, filtering was difficult, as Unity provides only so much freedom with sampling, as with texture atlases care needs to be taken at the edges to prevent bleeding and do correct filtering. So, difficult to edit, and difficult to render. Booh.

Then I thought “Ok, let’s use texture arrays in Unity”. So atlas+JSON as source data, then conversion to a texture array in Unity for runtime. Rendering is now easy, without any filtering issues. I do have a limit of a maximum of 2048 sprites per atlas, which is not great, but my 32-bit sprite instance data has now 8 whole bits free as a result, as I need only 11 bits to represent the texture index. On the minus side, editing was still hard.

The last few weeks, I had the sudden realisation that the atlas+JSON format as source data is very, very pointless, as I’m converting to arrays in Unity anyway. So, I went back to the basic form, which is files-and-folders. One file per sprite, some special naming format for animations, folders and subfolders for grouping and … magic! Now the spritesheet is very, very easily editable. Tiles can be previewed directly in explorer, I can change sprite names at will, add/remove tiles, and do some more stuff (more next few weeks), and it’s all very, very easy. When I’m done with editing, I run some Unity script that converts that to an array (still limit of 2048 max per atlas applies), and that’s it. I feel like I’ve been making my life more difficult with the 2D texture atlas format. So, the new atlas format will be the final (barring minor mods), as there’s no problem point really.

With such a simple “loose” format, it’s quite easy to write python scripts/tools to process the spritesheets, e.g. rename sprites or mass-rename animations, create distance fields per sprite, do some autotiling work, etc.

Audio

Since this is the first post about audio (I think?), I guess I can afford not being creative in naming the post. So, Unity already has audio facilities, which while nice, still leave some bits to be desired, especially if you’re going Wild West without using gameobjects much, like I do.

AudioListener and AudioSource

The basics that I bothered to research, require 2 things: an audio listener and audio sources, both components. Nice and simple. Normally you hook the audio listener to the player/camera gameobject, and audio source components are hooked to the gameobjects in your world. Since I’m not using gameobjects much, I’ve got an AudioListener to the camera gameobject, and a number of AudioSource components in my 2nd gameobject (called Scripts). The audio sources represent audio types really, and are 7:

  • Background music x2 : self-explanatory
  • Ambient sounds primary x2 : biome sounds for overworld, e.g. forest, swamp, open sea
  • Ambient sounds secondary x2: secondary ambience for overworld, e.g. river or shore
  • Positional sounds : misc sounds like doors opening/closing, secrets found, etc

The positional sounds source always uses the .PlayOneShot() function, which as the name suggests, plays immediately a sound, and is capable of mixing sounds, so I can reuse it to play many sounds. Simple!

The others (which are all looping channels) got a bit more complicated. I wanted crossfade, you see, and the solutions I found out there were not up to snuff. Snapshots something something, mixers, more and more components and gameobjects, or coroutines that you fire and forget, but if you want more control, tough luck.

So, I’m using a ping-pong pattern for the channels, so I need 2 of each. Thanks computer graphics for teaching me tricks! When you’re in the forest and you go to the desert, the “active” forest ambience channel starts fading out, and the 2nd buffer become active and fades in the desert audio. That’s it really! The fade in/out happens at the update function. In code, when we move around tiles in the overworld, I just set desired volume level, like “for ping-pong buffer 0 of ambient sounds primary, set volume to 1 and set audio clip to forest-ambience”. then the update function will dutifully interpolate whatever volume we currently have towards the desired volume. That’s it really! I’m quite happy with the result, and especially that it took a few hours overall to set up. I might have used more time searching for biome sounds really.

Here’s a video that demonstrates the above, plus some new biome-specific tilesets. So, you enter a dungeon in the tundra, you get tundra maps, etc. The video also uses some music that I made ages ago, as I’m eager to use this game as an outlet for all my procedural, algorithmic and creative needs.

(Towards the end of the video, I’m trying hard to find the dungeon entry, but it’s a large map so I started teleporting around, gave up and turned the FoV off, bad cheater I know 🙂 )