Changing Sprites at Runtime

Hi!

I have a Folder with Sprite sharing the same Atlas (Packing Tag).

On Runtime I’m using this to change the Sprite :

List<Sprite> SpriteList = new List<Sprite>();
SpriteList.AddRange(Resources.LoadAll<Sprite>("Sprites/").ToArray());

I need this to change the Sprite at runtime. But Unity will use the Sprite Atlas changing it this way or will use a Single Sprite?

I tried a form of this before using a list not array and couldn’t get it to work, is this really working?

Using Resources.Load prevents sprite atlases from working.

–Eric

Yes, it works! But @Eric5h5 saids that atlas will not work this way.

Eric, how can I make this work? If I populate the list on Design Time instead of Loading it on Runtime it will work with the Atlas?

Yes, typically you make an array that shows in the inspector, and drag and drop. You can use the lock icon to drag many items at once; you don’t have to do it one at a time.

–Eric

I wonder if this is why, when I first tried late last year, it did not fail, e.g. error out, but it would not fill anything out.

Personally I think the way Unity designed that part is stupid, if I have to load in a whole sheet just to get a single sprite, is an over burden, I know you can get around it, but it would be nice to get a reference to the sheet, then return the sprite by, name, coordinates, or even an index.

You don’t need to do that, you just drag and drop the single sprite you want. You don’t need to be concerned about any of the mechanics; you treat it the same whether it’s part of an atlas or not. That seems a lot less error prone than specifying coordinates and whatnot.

–Eric

That’s all well and good if your using prefabs, but what about lets say combining two or more image onto a sprite it’s a pain, the only way I could figure this out was have the second image(this is a shadow effect by the way) as a child of the sprite itself, it would be nice to be able to combine images easily, I think sometimes unity tires to predict what you want(tow make it easier for the developer) I say give me free rein.

You do have free reign. You can script whatever you want, write whatever shader you want. Unity can’t possibly know what every user would ever do and they can’t create every feature that could ever exist, that’s why you have the ability to create your own. Shadow effect, sure, take a texture as input, make a shadow offset, and create a combined sprite image from that. You can do it at runtime or make an editor script.

–Eric