- Home /
Help finding the right scale for an icon inside a backgorund sprite
In my inventory system, an item could take more than one slot, 2x2, 2x3, 3x3, etc. Each item has a texture - I don't care about the texture size (for a 2x2 item, the texture size doesn't have to be like, 256x256 or 128x128, but it would be better if it was)
Since I'm using NGUI, the item slot is actually a button, with a background, an icon (both sprites) and a label. I instantiate the slots, by using a Slot prefab, which has a script that stores the Slot size, for now I'm using 50. There's also a spaceBetweenSlots variable, which is 5.
So if I had a 2x2 item, its background would be of size 105x105, I scale the background to that size.
But the problem is, the item icon. I can't seem to know how to scale the icon, for it to fit perfectly inside the background. Take a look at this example:
Now the RedHerb fits, because its size is 594x601 and I'm scaling it by 100x100x1, but the gun is 640x453, if I scale it by the same scale as the Herb, you would get the results above.
Now the question is, how can I scale the icon to perfectly sit inside the background?
I came up with an idea of finding the difference between the icon's and the background's size, I keep scaling down the icon till there's a low dif between the two sizes. But NGUI is giving me a hard time of doing anything -_-
For my idea to work I just need the icon's and the background's size. I searched and found NGUIMath.CalculateAbsoluteWidgetsBounds, I'm not sure how to use it, it asks for a transform, and returns a Bounds, so I gave my background's, it got me back a bound with size of 0.2x0.2, did the same for the icon it gave me back a size of 0.0x0.0 lol O_O what is that?!
I tried background.relativeSize, I got back a V3 of (50,50,1) - NGUI forums says to Scale that by background.cachedTransform.relativeSize, to get the size in pixles, so I did:
background.relativeSize.Scale(background.cachedTransform.localScale);
Is that correct? is that how to scale two vectors? - I got back a V2 of (1.0, 1.0) :D
Any help would be much appreciated, thanks a lot.
I'd post a link of this question on the NGUI forum ^^ They should be most suited to respond to how you should do this. I don't know NGUI, but it seems a pretty basic functionality you should have, so I'm sure they'll be able to help you along.
Unfortunately NGUI = one guy. He answers most the Qs, pretty overloaded/overworked guy. So the answer might come really slow... :/ Anyway, I did ask there.
Something I'm not getting about your scaling... are you trying to apply the same scaling modifier to the gun, than you do to the leaves? Even though the original texture/object do not have the same original size?
No, not the same scale. I'm having the problem because I'm using the same scale for textures of dif sizes. The background is scaled 105,105,1 the gun is 100,100,1 - that gives the result ^. However, if manually change the gun scale down to 85,75,1 it fits perfectly. $$anonymous$$y whole journey is to figure out a formula to give me back that new scale I have to apply to the gun.
Since I don't know NGUI, I'm probably not getting all of this. But when you say scale, you're talking about the transform scaling right? (And not talking about the size in pixels) And if you know the scaling of the background and its original size, and know the original size of the gun, you should be able to compute the scale of the gun, no?
logicaly, scaleGun/scaleBG = sizeGun/sizeBG so scaleGun = scaleBG*sizeGun/sizeBG;
But again, might be missing something ^^