2
votes

So I'm doing this little game for university and I've encountered a problem with the resizing of the sprites. My game's like a graphic adventure (monkey island, grim fandango and stuff) and it has an inventory where items can be saved. The problem is, although the world object sprites are good and have different sizes, I want their sprites to be resized when and save them in the inventory so they can fit.

I thought this were done using the Bounds class, but I have some issues with it. Also: the tutorial I was following did something that in it's time might work, but now it gives me a compile error. Showing below my function to save an item (and resize its sprite in the process):

void SaveInventoryItem()
{
    string name = transform.parent.gameObject.name;
    GameObject temporaryObject = Instantiate(inventoryItemPrefab);
    temporaryObject.name = name;
    temporaryObject.gameObject.GetComponent<SpriteRenderer>().sprite = transform.parent.GetComponent<SpriteRenderer>().sprite;
    temporaryObject.transform.parent = inventoryCanvas.transform;

    temporaryObject.transform.position = Vector3.forward * -1;
    temporaryObject.gameObject.GetComponentInChildren<InteractZone>().interactText = "Usar " + name;

    Bounds bounds = temporaryObject.gameObject.GetComponent<SpriteRenderer>().sprite.bounds.size ;
    float factor = General.inventoryItemSize / bounds.size.y;
}

The compile error

The error says cannot convert from UnityEngine.Vector3 to UnityEngine.Bounds

If someone knows a way to solve this error or another way to resize sprites in a default size, please let me know.

3
"Bounds bounds = temporaryObject.gameObject.GetComponent<SpriteRenderer>().sprite.bounds;" should work. That being said, you shouldn't need to modify the sprite size at all if you're using Unity's UI Canvas capabilities for your inventory.Erik Overflow
Exactly. If you have an Image component on your inventory items in the UI, just assign the sprite to them and that's it.ximera

3 Answers

1
votes

Look at @Erik Overflows comment. Use a canvas class in your inventory system, which will contain a bunch of images with properly set sizes. Then you can just assign the sprite to the image, and it should size automatically.

1
votes

As Erik said in his comment, you can just set the image for the UI canvas element in the inventory to the sprite, it will automatically fit the sprite to the ui element. Then all you need to do is use the system figbar suggested to assign sprites to your images as inventory items are added or removed.

0
votes

If you know the size of the sprite, under the Inspector, change the Pixels Per Unit to that size. You could play around with the numbers until it fills, or you can change your Sprite Mode to multiple, click the Sprite Editor button, and go to slice. Select grid by cell size type and it may populate the pixel size, but you can play with it and figure it out if it doesn't.