How do I change a sprite via an inspector?

To save myself tens of thousands of manual operations (scrolling and dragging of sprites from the project into renderers), I’m trying to create an inspector which will let me perform simple sprite-changing operations.

For instance, I want a button which will pick a sub-sprite at random from a ‘multiple’ sprite sheet. I can easily generate the name of that sub-sprite (eg “MySprite_01”), but setting the name of the sprite in the object’s sprite renderer has no effect.

Is there any way to do this?


Ok, figured this one out myself.

First off, so far as I can tell, there’s no way to set the sprite to one defined in the project other than to drag it in manually OR make the sprite a runtime-loaded resource.

However, you CAN simply generate a new sprite in code with the desired texture and texture coordinates. This isn’t much help if your sprites are custom sizes and shapes, but if they’re a simple grid you can generate what you want fairly easily.

Sprite s = Sprite.Create( old.texture,  new Rect( x,y,w,h ) , new Vector2(centreX,centreY) );