/// <summary>
        /// Deserializes the options from a serialized string
        /// </summary>
        /// <returns>The serializable string.</returns>
        /// <param name="serializedOptions">String that represents serialized SlicingOptions.</param>
        public static SpriteSlicingOptions FromSerializableString(string serializedOptions)
        {
            var options = new SpriteSlicingOptions();

            string[] entries = serializedOptions.Split(DelimeterChar);

            // Default ImportMode to Multiple for versioned options
            options.ImportMode = UnityEditor.SpriteImportMode.Multiple;

            options.GridSlicing = GridSlicingMethod.SliceAll;

            options.CellSize = new Vector2(int.Parse(entries[0]), int.Parse(entries[1]));
            if (entries.Length >= 3)
            {
                int.TryParse(entries[2], out options.Frames);
                if (entries.Length >= 9)
                {
                    options.ImportMode    = (SpriteImportMode)int.Parse(entries[3]);
                    options.GridSlicing   = (GridSlicingMethod)int.Parse(entries[4]);
                    options.OverridePivot = bool.Parse(entries[5]);
                    options.Pivot         = (SpriteAlignment)int.Parse(entries[6]);
                    options.CustomPivot   = new Vector2(float.Parse(entries[7]), float.Parse(entries[8]));
                }
            }

            return(options);
        }
        /// <summary>
        /// Deserializes the options from a serialized string
        /// </summary>
        /// <returns>The serializable string.</returns>
        /// <param name="serializedOptions">String that represents serialized SlicingOptions.</param>
        public static SpriteSlicingOptions FromSerializableString(string serializedOptions)
        {
            var options = new SpriteSlicingOptions();
            string[] entries = serializedOptions.Split(DelimeterChar);

            // Default ImportMode to Multiple for versioned options
            options.ImportMode = UnityEditor.SpriteImportMode.Multiple;

            options.GridSlicing = GridSlicingMethod.SliceAll;

            options.CellSize = new Vector2(int.Parse(entries[0]), int.Parse(entries[1]));
            if (entries.Length >= 3)
            {
                int.TryParse(entries[2], out options.Frames);
                if (entries.Length >= 9)
                {
                    options.ImportMode = (SpriteImportMode)int.Parse(entries[3]);
                    options.GridSlicing = (GridSlicingMethod)int.Parse(entries[4]);
                    options.OverridePivot = bool.Parse(entries[5]);
                    options.Pivot = (SpriteAlignment)int.Parse(entries[6]);
                    options.CustomPivot = new Vector2(float.Parse(entries[7]), float.Parse(entries[8]));
                }
            }

            return options;
        }
        /// <summary>
        /// Creates a spritesheet for a texture based on the slicing options
        /// </summary>
        /// <returns>The sprite sheet metadata for the texture.</returns>
        /// <param name="texture">Texture to slice.</param>
        /// <param name="slicingOptions">Slicing options.</param>
        public static SpriteMetaData[] CreateSpriteSheetForTexture(Texture2D texture, SpriteSlicingOptions slicingOptions)
        {
            List<SpriteMetaData> sprites = new List<SpriteMetaData>();
            Rect[] gridRects = GetAllSliceRectsForTexture(texture, slicingOptions.CellSize);
            for (int i = 0; i < gridRects.Length; i++)
            {
                SpriteMetaData spriteMetaData = new SpriteMetaData();
                spriteMetaData.rect = gridRects[i];
                spriteMetaData.alignment = (int)slicingOptions.Pivot;
                spriteMetaData.pivot = slicingOptions.CustomPivot;
                spriteMetaData.name = texture.name + "_" + i;
                sprites.Add(spriteMetaData);
            }

            return sprites.ToArray();
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a spritesheet for a texture based on the slicing options
        /// </summary>
        /// <returns>The sprite sheet metadata for the texture.</returns>
        /// <param name="texture">Texture to slice.</param>
        /// <param name="slicingOptions">Slicing options.</param>
        public static SpriteMetaData[] CreateSpriteSheetForTexture(Texture2D texture, SpriteSlicingOptions slicingOptions)
        {
            List <SpriteMetaData> sprites = new List <SpriteMetaData>();

            Rect[] gridRects = GetAllSliceRectsForTexture(texture, slicingOptions.CellSize);
            for (int i = 0; i < gridRects.Length; i++)
            {
                SpriteMetaData spriteMetaData = new SpriteMetaData();
                spriteMetaData.rect      = gridRects[i];
                spriteMetaData.alignment = (int)slicingOptions.Pivot;
                spriteMetaData.pivot     = slicingOptions.CustomPivot;
                spriteMetaData.name      = texture.name + "_" + i;
                sprites.Add(spriteMetaData);
            }

            return(sprites.ToArray());
        }