Burnner

Untitled

Apr 9th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. void DoIT()
  2. {
  3. // determine your size from sprites
  4. width = 0;
  5. height = 0;
  6. foreach (Sprite t in textures)
  7. {
  8. width += t.texture.width;
  9. // determine the height
  10. if (t.texture.height > height) height = t.texture.height;
  11. }
  12.  
  13. // make your new texture
  14. atlas = new Texture2D(width, height, TextureFormat.RGBA32, false);
  15. // loop through your textures
  16. for (int i = 0; i < textures.Length; i++)
  17. {
  18. int y = 0;
  19. while (y < atlas.height)
  20. {
  21. int x = 0;
  22.  
  23. while (x < textures[i].texture.width)
  24. {
  25.  
  26. if (y < textures[i].texture.height)
  27. {
  28. // fill your texture
  29. atlas.SetPixel(x + textureWidthCounter, y, textures[i].texture.GetPixel(x, y));
  30. }
  31. else
  32. {
  33. // add transparency
  34. atlas.SetPixel(x + textureWidthCounter, y, new Color(0f, 0f, 0f, 0f));
  35. }
  36. ++x;
  37. }
  38. ++y;
  39.  
  40. }
  41. atlas.Apply();
  42. textureWidthCounter += textures[i].texture.width;
  43. }
  44. // for normal renderers
  45. if (testMaterial != null) testMaterial.mainTexture = atlas;
  46. // for sprite renderer just make a sprite from texture
  47. Sprite s = Sprite.Create(atlas, new Rect(0f, 0f, atlas.width, atlas.height), new Vector2(0.5f, 0.5f));
  48. testSpriteRenderer.sprite = s;
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment