using System;
using UnityEngine;
using UnityEditor.U2D.Sprites;
namespace Editor
{
static public partial class SpriteEditorUtility
{
///
/// Slices a texture into a regular grid of sprite rectangles based on specified cell size, offset, and padding.
/// Optionally keeps or discards empty rectangles based on pixel alpha values.
///
/// The sprite data provider for the texture.
/// The texture data provider.
/// The offset from the top-left corner to start the grid.
/// The size of each grid cell (width x height).
/// The padding between grid cells.
/// Method for handling existing sprites (DeleteAll, Smart, Safe).
/// Function to generate sprite names based on index.
/// Whether to keep sprites with no visible pixels.
/// Tolerance for detecting overlapping sprites.
/// Tolerance for best-fit matching.
/// Whether to use best-fit algorithm for overlap detection.
/// True if slicing succeeded, false if texture is not readable.
static public bool GridSliceTexture(ISpriteEditorDataProvider spriteDataProvider, ITextureDataProvider textureProvider, Vector2 offset, Vector2 size, Vector2 padding,
AddNewSpriteMethod addNewSpriteMethod, Func nameGenerator,
bool keepEmptyRects =false, float kOverlapTolerance= 0.00001f, float kBestFitTolerance = 0.5f, bool bestFit = false)
{
var textureToUse = GetTextureToSlice(textureProvider);
if (textureToUse == null)
{
return false;
}
var rects = UnityEditorInternal.InternalSpriteUtility.GenerateGridSpriteRectangles(textureToUse, offset, size, padding, keepEmptyRects);
var newRects = GenerateNewSpriteRects(spriteDataProvider, rects, addNewSpriteMethod, nameGenerator, kOverlapTolerance, kBestFitTolerance, bestFit);
spriteDataProvider.SetSpriteRects(newRects.ToArray());
return true;
}
}
}