Getting Sliced Sprites programatically

Let’s say that we have a texture in multiple mode, and that gets sliced into 50 sprites.

I want to be able to have a variable of type Texture2D, set this texture (e.g. through the editor), and get all of the sprites produced by the Sprite Editor.

I can do this, by getting pixels from a texture manually, using the texture, a width, height, padding and offset, just like you enter them in the Sprite Editor’s grid slicer. Is there some better way?

for example:

Sprite GetSprite(Texture2D from, Rect rect, int atX, int atY, int paddingX, int paddingY)
{
Rect newRect = rect;
newRect.position = new Vector2((newRect.width + paddingX) * atX + newRect.position.x, (newRect.height + paddingY) * atY + newRect.position.y);
return Sprite.Create(from, newRect, Vector2.zero);
}

I don’t know if this is what you want but you can get all the sliced sprites this way:

Sprite[] sprites = Resources.LoadAll<Sprite>("filename");

Just make sure your Sprite is in your Resources folder.

Ah yes, tyvm, I forgot to write about that.

It was another solution that I tried, but the problem is that it’s not a very robust solution, as I have to hardcode the filepath (or always provide it somehow), and if I move the asset or change the folder structure, it’s going to break.

I don’t think there’s any other way… At least none that I know of… Personally I don’t have a problem with hardcoding the filepath, it’s not like I keep changing my folder structure from time to time ^^’

Ok, tyvm for the help anyway! :slight_smile: