0
votes

I'm writing a game using the 2d features of unity.

I'm designing a sort of inventory for the player character, and I have a gameobject with a number of placeholder images inside it. The intention is that when I actually load this gameobject, I'll replace the sprites of the placeholder images and I'll display what I want.

My issue is that when I change the sprite using code like this

    var ren = item1.GetComponentInChildren<SpriteRenderer>();

ren.sprite = Resources.Load<Sprite>("DifferentSprite");

The image loaded is correct, however the scaling applies to the new sprite. The issue is that these sprites have all different sizes. So whilst the original placeholder image takes up a small square, the replacement might be tiny or massive enough to cover the whole screen depending on how the actual png was sized.

I basically want the sprite to replace the other and scale itself such that it has the same width and height as the placeholder image did. How can I do this?

EDIT - I've tried playing around with ratios. It's still not working perfectly, but its close.

    var ren = item1.GetComponentInChildren<SpriteRenderer>();

    Vector2 beforeSize = ren.transform.renderer.bounds.size;

    ren.sprite = Resources.Load<Sprite>("Day0/LampOn");

    Vector2 spriteSize = ren.sprite.bounds.size;

    //Get the ratio between the wanted size and the sprite's size ratios

    Vector3 scaleMultiplier = new Vector3 (beforeSize.x/spriteSize.x, beforeSize.y/spriteSize.y);

    //Multiple the scale by this multiplier
    ren.transform.localScale = new Vector3(ren.transform.localScale.x * scaleMultiplier.x,ren.transform.localScale.y * scaleMultiplier.y);
3
You get the width and height property from the placeholder image and set the real image width and height to that... Or you could create a whole new set of resized images which would be used specifically for your inventorySavlon
I don't know how to set width and height except by playing with the localScaleHaedrian
you simply use the size fitter component, it's all automaticFattie

3 Answers

1
votes
puzzle.Sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height),Vector2.one/2,256);

the last int is for the pixelperUnity, its the cause of changing size. It's fixed in my project, my default pixelPerUnit=256 and unity default = 100 so its bigger 2.56 times tell me if it helps

0
votes

How to modify width and height: use RectTransform.sizeDelta.

Example for this type of script

Vector3 originalDelta = imageToSwap.rectTransform.sizeDelta; //get the original Image's rectTransform's size delta and store it in a Vector called originalDelta
imageToSwap.sprite = newSprite; //swap out the new image
imageToSwap.rectTransform.sizeDelta = originalDelta; //size it as the old one was.

More about sizeDeltas here

-4
votes

I would expose two public variables in the script, with some default values:

public int placeholderWidth = 80;
public int placeholderHeight = 80;

Then, whenever you change sprites, set the width & height to those pre-defined values.