Example #1
0
        public static bool SetThumbnails(string name, SpriteTextureInfo info, UITextureAtlas atlas, string[] states = null)
        {
            if (atlas == null || atlas.texture == null)
                return false;

            Texture2D atlasTex = atlas.texture;
            float atlasWidth = atlasTex.width;
            float atlasHeight = atlasTex.height;
            float rectWidth = info.width / atlasWidth;
            float rectHeight = info.height / atlasHeight;
            int x = info.startX;
            int y = info.startY;

            if (states == null)
                states = new string[] { "" };

            for (int i = 0; i < states.Length; i++, x += info.width)
            {
                if (x < 0 || x + info.width > atlasWidth || y < 0 || y > atlasHeight)
                    continue;

                Texture2D spriteTex = new Texture2D(info.width, info.height);
                spriteTex.SetPixels(atlasTex.GetPixels(x, y, info.width, info.height));

                UITextureAtlas.SpriteInfo sprite = new UITextureAtlas.SpriteInfo()
                {
                    name = name + states[i],
                    region = new Rect(x / atlasWidth, y / atlasHeight, rectWidth, rectHeight),
                    texture = spriteTex
                };
                atlas.AddSprite(sprite);
            }

            return true;
        }
Example #2
0
        public static bool SetThumbnails(string name, SpriteTextureInfo info, UITextureAtlas atlas, string[] states = null)
        {
            if (atlas == null || atlas.texture == null)
            {
                return(false);
            }

            Texture2D atlasTex    = atlas.texture;
            float     atlasWidth  = atlasTex.width;
            float     atlasHeight = atlasTex.height;
            float     rectWidth   = info.width / atlasWidth;
            float     rectHeight  = info.height / atlasHeight;
            int       x           = info.startX;
            int       y           = info.startY;

            if (states == null)
            {
                states = new string[] { "" }
            }
            ;

            for (int i = 0; i < states.Length; i++, x += info.width)
            {
                if (x < 0 || x + info.width > atlasWidth || y < 0 || y > atlasHeight)
                {
                    continue;
                }

                Texture2D spriteTex = new Texture2D(info.width, info.height);
                spriteTex.SetPixels(atlasTex.GetPixels(x, y, info.width, info.height));

                UITextureAtlas.SpriteInfo sprite = new UITextureAtlas.SpriteInfo()
                {
                    name    = name + states[i],
                    region  = new Rect(x / atlasWidth, y / atlasHeight, rectWidth, rectHeight),
                    texture = spriteTex
                };
                atlas.AddSprite(sprite);
            }

            return(true);
        }