public Texture2D[] textures; // this is set up in the Inspector for now. Will be replaced later by downloaded images.
public UIPanel panel;
void CreateAtlas()
{
GameObject go
= new GameObject
("Atlas"); go.layer = LayerMask.NameToLayer( "UI" );
go.AddComponent("UIAtlas");
UIAtlas atlas = go.GetComponent<UIAtlas>();
Rect[] rects;
Texture2D tex
= new Texture2D
(1,
1, TextureFormat
.ARGB32,
false); int maxSize = 2048;
rects = tex.PackTextures(textures, 1, maxSize);
atlas
.spriteMaterial = new Material
(Shader
.Find ("Unlit/Transparent Colored")); atlas.spriteMaterial.mainTexture = tex;
List
<UISpriteData
> list
= new List
<UISpriteData
>(); for( int i=0; i<textures.Length; i++)
{
UISpriteData data
= new UISpriteData
(); Rect rect = NGUIMath.ConvertToPixels (rects[i], tex.width, tex.height, true );
data.x = Mathf.RoundToInt(rect.x);
data.y = Mathf.RoundToInt(rect.y);
data.width = Mathf.RoundToInt(rect.width);
data.height = Mathf.RoundToInt(rect.height);
data.name = textures[i].name;
list.Add(data);
}
atlas.spriteList = list;
UISprite sprite = NGUITools.AddWidget<UISprite>(panel.gameObject);
sprite.atlas = atlas;
sprite.spriteName = "auburn";
sprite.MakePixelPerfect();
}