Skip to content

Instantly share code, notes, and snippets.

@rafayali
Last active August 29, 2015 14:10
Show Gist options
  • Save rafayali/a74cb63f4071bd4dc2ba to your computer and use it in GitHub Desktop.
Save rafayali/a74cb63f4071bd4dc2ba to your computer and use it in GitHub Desktop.
private class TextureDownloader
{
public WWW www;
public string url;
public Texture2D tex;
public string atlasName;
}
IEnumerator _UpdateAtlas(List<TextureDownloader> texIndex)
{
Debug.Log(Helpers.TimeStamp() + "Begin updating atlases");
yield return 0; //why this?
//get the unique atlas names
TextureDownloader[] uniqueAtlasNames = texIndex.GroupBy(x => x.atlasName)
.Select(g => g.First())
.ToArray();
Debug.Log(Helpers.TimeStamp() + "There are " + uniqueAtlasNames.Count() + " atlases in the queue");
for (int i = 0; i < uniqueAtlasNames.Count(); i++)
{
string thisAtlasName = uniqueAtlasNames[i].atlasName;
//all items that are associated with this atlas
TextureDownloader[] thisAtlasurls = texIndex.Where(x => x.atlasName == thisAtlasName).ToArray();
//and within that, get the unique url lists
//http://stackoverflow.com/questions/2537823/distinct-by-property-of-class-by-linq
TextureDownloader[] atlasDownloaders =
thisAtlasurls.GroupBy(x => x.url)
.Select(g => g.First())
.ToArray();
//and get just their textures for the packtextures
Texture2D[] textures = atlasDownloaders.Select(x => x.tex).ToArray();
//pack textures and set up the material
var atlasTexture = new Texture2D(1, 1);
var coordinates = atlasTexture.PackTextures(textures, 0, SystemInfo.maxTextureSize);
var material = new Material(Shader.Find("Unlit/Transparent"));
material.SetTexture("_MainTex", atlasTexture);
//load a new atlas and set its material
var go = (GameObject)Instantiate(AtlasPrefab);
go.name = thisAtlasName;
var atlas = go.GetComponent<UIAtlas>();
atlas.spriteMaterial = material;
//add a sprite to the atlas for each unique
int j = 0;
foreach (var tex in atlasDownloaders)
{
var coordinate = coordinates[j];
var sprite = new UIAtlas.Sprite { name = tex.url, inner = coordinate, outer = coordinate };
atlas.spriteList.Add(sprite);
j++;
}
atlas.MarkAsDirty();
if (SpamLogs)
{
Debug.Log("Atlas: " + thisAtlasName + " texture count: " + textures.Count() + "coords: " + coordinates.Count());
Debug.Log(thisAtlasName + " atlas : size " + atlasTexture.width + " x " + atlasTexture.height);
}
}
Debug.Log(Helpers.TimeStamp() + "End updating atlases");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment