Unity – generate SpriteSheets at runtime!

Unity and plugins provide many great ways to build Sprite Sheets. However they’re used directly into Unity Editor or with an external software which is perfect in many case, but none provide the ability to generate SpriteSheets at runtime. So we made our own library.

The goal of a sprite sheet is to pack as many sub-textures as possible in one big texture. So the first thing to do was a packing algorithm. Fortunately we remembered the one made and open-sourced by Ville Koskela in AS3, so we started with a Unity C# port.
Click here to view the Unity rectangle packing example running in your browser in WebGL!

Once the packing algorithm done, we worked on a cache system so the generated sprite sheets are written and saved on disk for a future loading. The generator process won’t be needed anymore unless you increase your cache version!

Give a try to UnityRuntimeSpriteSheetsGenerator!

Unity – sprite packing

Best practices with Unity have always been an hidden gem. Most precisely concerning the Resources folder. Hopefully things are changing with Unity’s best practices guide! And things are simple concerning the Resources folder: just don’t use it!

Managing 2D Sprites in Unity is simple if you have a static scene: just put every sprite needed on your screen and you’re almost done. Obviously you should package them in Spritesheets. The easy way to do it is via Unity’ Sprite Packer tool. But what to do if you need to load Sprites at runtime? How to access a Sprite if it’s not linked directly on a GameObject or a Prefab and without using the Resources folder?

Create a ScriptableObject! They are the best way to store informations within Unity. Here is an Editor Script for generating ScriptableObjects with a list of Sprites for each Packing Tag mentioned:

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

[Serializable]
public class SpritesData:ScriptableObject {

	[SerializeField]
	public Sprite[] sprites;

	static public string assetDirPath = "RuntimeSprites/SpritesData/";
	static public string assetPath = "Assets/RuntimeSprites/SpritesData/{0}.asset";

	#if UNITY_EDITOR
	[MenuItem("Data/Create Sprites Data")]
	static void CreateSpritesData() {
		string dirPath = Application.dataPath + "/" +  assetDirPath;

		if (!System.IO.Directory.Exists(dirPath))
			System.IO.Directory.CreateDirectory(@dirPath);

		SpritesData data = ScriptableObject.CreateInstance<SpritesData>();

		List<Sprite> spritesList = new List<Sprite>();

		string[] anims = AssetDatabase.FindAssets("t:Sprite", new string[] {"Assets/Sprites"});

		string currentTag = "";

		foreach (string anim in anims) {

			string path = AssetDatabase.GUIDToAssetPath(anim);

			TextureImporter ti = TextureImporter.GetAtPath(path) as TextureImporter;

			if (ti.spritePackingTag != currentTag) {

				if (data && currentTag != "") {

					data.sprites = spritesList.ToArray();
					AssetDatabase.CreateAsset(data, string.Format(assetPath, currentTag));
				}

				data = ScriptableObject.CreateInstance<SpritesData>();
				spritesList = new List<Sprite>();

				currentTag = ti.spritePackingTag;
			}

			Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);

			spritesList.Add(sprite);
		}

		data.sprites = spritesList.ToArray();
		AssetDatabase.CreateAsset(data, string.Format(assetPath, currentTag));
	}
	#endif
}

Now that you have all your SpritesData you just have to create a MonoBehaviour with a public SpritesData[] spritesDatas; property, then you will have access to all your sprites! The good things is while a sprite isn’t displayed on Screen it’s not in memory.

Cheers!

Unity – manage mailbox

For a project, unfortunately canceled, we needed to be able to manage a mailbox with Unity. Including IMAP protocol.

Unity uses C#, so we were pretty confident it wouldn’t be an issue since there are so many .NET libraries around there. But none of them worked fine with Unity: some of them worked in Unity Editor, some were also able to work on Android, but none could pass Unity’s IL2CPP compilation for Xcode & iOS. We searched for solution on Unity’s forums but we find nothing, there was no other choice than testing all the libraries around the web!

EAGetMail was the only one to fail on Xcode due to IL2CPP compilation, all the other libraries failed way before (due to Unity Mono version). So we raised an issue to Unity’s IL2CPP team.

During that time, we needed to be sure the project would be feasible. So we looked for alternative: a PHP solution for IMAP called Fetch combined with an AMF library for Unity. Not really a good replacement, but it worked.

Luckily Unity fixed the issue some weeks later! If you have to manage mailboxes with Unity, be sure to give a try to EAGetMail before banging your head against the wall! 😉

Memovox, AIR still on top

Two years ago, we coded a beautiful app named Geophysic for the famous luxary watches brand Jaeger-LeCoultre. Made with the cross-platform technology Adobe AIR, and Starling & Feathers, we finished the dev diary asking which other technologies could have done the app, but didn’t get a strong answer.
Today, with Pixelfordinner agency, we’re proud to share this new app named Wake-Up Memovox, for the same client, available for iOS and Android.
It was developed with the same technology, and if it’s obvious the game development market moves in favour of Unity, AIR is still rocking for making apps! Let’s see the development part!

Continue reading Memovox, AIR still on top