Beispiel #1
0
        private static bool PackTextures(Texture2D texture, List <SpriteEntry> sprites, int padding, bool unityPacking, bool forceSquare)
        {
            #if UNITY_3_5 || UNITY_4_0
            var maxSize = 4096;
            #else
            var maxSize = SystemInfo.maxTextureSize;
            #endif

            maxSize = Mathf.Min(maxSize, 2048);

            sprites.Sort(Compare);

            var textures = sprites.Select(x => x.texture).ToArray();

            var rects = new Rect[0];

            if (unityPacking)
            {
                rects = texture.PackTextures(textures, padding, maxSize);
            }
            else
            {
                rects = TexturePacker.PackTextures(texture, textures, 4, 4, padding, maxSize, forceSquare);
            }

            for (var i = 0; i < sprites.Count; ++i)
            {
                var rect = TextureUtility.ConvertToPixels(rects[i], texture.width, texture.height, true);

                if (textures[i] == null)
                {
                    return(false);
                }

                if (Mathf.RoundToInt(rect.width) != textures[i].width)
                {
                    return(false);
                }

                var spriteEntry = sprites[i];

                spriteEntry.x      = Mathf.RoundToInt(rect.x);
                spriteEntry.y      = Mathf.RoundToInt(rect.y);
                spriteEntry.width  = Mathf.RoundToInt(rect.width);
                spriteEntry.height = Mathf.RoundToInt(rect.height);
            }

            return(true);
        }