Displaying single sprite from spritesheet on GUI

I’ve got a sprite sheet of weapons and items that I’ve set to “multiple” sprite mode, so I have a bunch of “sub-sprites” from the main sheet. I’ve then created “icon” parameters for, say, a weapon, and dragged the corresponding sub-sprite from the sheet into the inspector slot - now the object has a reference to that sub-sprite.

I want to display this sprite on a GUI when it’s selected (e.g. in an item shop screen), however when I try and use GUI.DrawTexture() on that object’s sub-sprite, instead it draws the entire spritesheet it came from. I then tried using GUI.DrawTextureWithTexCoords(), but this made the sprite seemingly disappear, even though as far as I’m aware, the coordinates were correct. I also tried creating a prefab of the individual sprite and passing it to the UI to display, but this didn’t work either (and seems a bit long-winded to do for every single one when I already have them separated as sub-sprites).

Is there a way to display the single sub-sprite from the main sheet on the GUI?

EDIT: I got DrawTextureWithTexCoords() to work (I forgot to set the last param to true meaning there was no alpha?), but even so, it was a complete pain to go through and find the coords of every single sprite in the main sheet, and displaying it was also annoying.

Ultimately I followed what @lunoland said, but instead of using a prefab with lots of child prefabs for each icon, I gave each item a Sprite attribute, then when the GUI needed to display that item, it just accessed its attached sprite attribute and updated the on-screen item prefab’s sprite to be that. I don’t know if this is the best way to do it, but it works for now and I’ll play around with it some more.

@Nephtis I would strongly recommend using the newer Canvas paradigm over OnGUI.

Create a single prefab for one of your icon sprites and get that set up and looking the way you want on the Canvas. Then just iterate over all your sprite icons, instantiating a new instance of the prefab and changing its sprite to the current sprite. You’ll also want to parent each new object to the canvas and change the object’s name to match the sprite or icon name.

I can’t test this right now, but assuming you put your sprite in the folder Resources/Sprites, it would look something like:

public GameObject iconPrefab; // Assign these in the inspector
public Transform canvas;

void GenerateIcons() {
    Sprite[] sprites = Resources.LoadAll<Sprite>("Sprites/YourSpriteName");

    foreach (Sprite sprite in sprites) {
        GameObject newIcon = Instantiate<GameObject>(iconPrefab);
        newIcon.name = sprite.name;
        
        newIcon.GetComponent<Image>().sprite = sprite;
        newIcon.transform.SetParent(canvas, false);
    }
}

Once you run the script and the new UI objects are created in the scene, you can select them and drag into your assets folder to create individual prefabs. This will make it super easy to adjust individual icons later on.