Knowing the actual dimensions of a sprite after scaling it at runtime - c#

I'm creating a 2D project and I've a particular sprite that when I import it to Unity I apply a 200 units per pixels in the importer and in runtime sometimes I scale it.
How can I know the actual dimensions of the rendered sprite?

Sprite width in pixels / pixels per unit * X scale = actual width
Do the same for height to get the size in both dimensions.
If you want to get the width of the sprite projected on the screen then you will need to modify the result depending on the camera size. I think you could just divide it by the camera size.

Related

Unity 2D import sprites larger than their original size

So, I made a sprite that is half long an HD screen, but when I import that in Unity and I add it to the scene it's larger than half the screen. I already set the camera resolution to 1920x1080. How can I solve this?
It isn't the size of image that matters, but the container in which it is to be placed. Adjust the size of your Image using Rect Tool and Rect Transform.

Resize loaded sprite from a texture with type multiple (width and height not match)

hello I'm having a problem that I don't understand why that is happening, the problem is that:
I have a texture of type multiple where I load all of my cards sprites
texture:
as you can see each card has its own Sprite properties that define the limit of the image and on this example, you will see that the width is 402 and height is 563.
but for some reason when I load the sprite using
Sprite[] sprites = Resources.LoadAll<Sprite>(sheetName) and then accessing to the specific sprite for example (sprites[0])
if I do
sprites[0].rect.width
sprites[0].rect.height
the values returned are different than the values specified before on the image and I need these values to scale the sprite width a specific width and height using this formula
var factorX = (CARD_WIDTH / sprites[0].rect.width );
var factorY = (CARD_HEIGHT / sprites[0].rect.height ) ;
transform.localScale = new Vector2(factorX, factorY);
because the values for width and height are different the factor that I'm getting is wrong.
also, I just noted that the values that I'm getting are the same that unity editor shows to me here
My question is:
why I'm getting different values for width and height?
The two ratios are about the same, could it be showing you the resolution of the thumbnail's mip map rather than the raw texture? You could try turning off Generate Mipmaps in the texture import settings if it's on

Is it safe to assume an image rotation manipulates the height and width exactly?

Say I have the following code for an image that's 200x100 or any arbitrary size really:
Image image = Bitmap.FromFile(fileName);
image.RotateFlip(RotateFlipType.Rotate90FlipNone);
image.save(fileName);
Is it safe to assume that the output size is 100x200? That the width and height exactly swapped values? My coworker is convinced it may not be guaranteed. I think the matrix math involved is exact and reliable. Who's right? If it matters, we're working with tif images.
The Image.RotateFlip Method only does rotation in exact multiples of 90 degrees (0, 90, etc.)
This means after you use it, you will always end up with one of 2 results:
1. Either the width and height will remain unchanged (for example when you flip without rotation or rotate 180 degrees).
2. Or the width and height will be exact swapped values.
Bonus info:
This type of right-angle rotation is "lossless" which means the color values are preserved and the operation can be reversed without any loss of pixel values.
On the other hand, any algorithm that rotates arbitrary angles that are not multiples of 90 degrees will be a lossy algorithm. The pixel locations and/or exact color values are not guaranteed to be restored by doing a counter-rotation.

How to scale texture2d in XNA with window resizing

I'm developing an UI for a project for school, and I've tried similar methods to scaling my texture as listed here, but here is the issue:
Our project is developed at 1440 x 900, so I've made my own images that fit that screen resolution. When we have to demo our project in class, the projector can only render up to 1024 x 768, thus, many things on the screen goes missing. I have added window resizing capabilities, and I'm doing my scaling like this. I have my own class called "button" which has a texture 2d, and a Vector2 position contruscted by Button(Texture2d img, float width, float height).
My idea is to set the position of the image to a scalable % of the window width and height, so I'm attempting to set the position of the img to a number between 0-1 and then multiply by the window width and height to keep everything scaled properly.
(this code is not the proper syntax, i'm just trying to convey the point)
Button button = new Button(texture, .01, .01 );
int height = graphicsdevice.viewport.height * button.position.Y;
int width = graphicsdevice.viewport.width * button.position.X;
Rectangle rect = new Rectangle(0,0,width, height);
sprite.being()
sprite.draw (button.img, rect, color.white);
sprite.end
it doesn't end up scaling anything when i go to draw it and resize the window by dragging the mouse around. if i hard code in a different bufferheight and bufferwidth to begin with, the image stays around the same size regardless of resolution, except that the smaller the resolution is, the more pixelated the image looks.
what is the best way to design my program to allow for dynamic texture2d scaling?
As Hannesh said, if you run it in fullscreen you won't have these problems. However, you also have a fundamental problem with the way you are doing this. Instead of using the position of the sprite, which will not change at all during window resize, you must use the size of the sprite. I often do this using a property called Scale in my Sprite class. So instead of clamping the position of the sprite between 0 and 1, you should be clamping the Size property of the sprite between 0 and 1. Then as you rescale the window it will rescale the sprites.
In my opinion, a better way to do this is to have a default resolution, in your case 1440 x 900. Then, if the window is rescaled, just multiply all sprites' scaling factors by the ratio of the new screensize to the old screensize. This takes only 1 multiplication per resize, instead of a multiplication per update (which is what your method will do, because you have to convert from the clamped 0-1 value to the real scale every update).
Also, the effects you noticed during manual rescale of the sprites is normal. Rescaling images to arbitrary sizes causes artifacts in the rendered image because the graphics device doesn't know what to do at most sizes. A good way to get around this is by using filler art during the development process and then create the final art in the correct resolution(s). Obviously this doesn't apply in your situation because you are resizing a window to arbitrary size, but in games you will usually only be able to switch to certain fixed resolutions.

Silverlight Transforms Images C#

I have an image on which I may use a ScaleTransform to increase the image size by 25%. I also have a rectangle which sits on top of the image highlighting a particular area. When I scale the image I want the rectangle to scale as well and highlight the same area as before. Scaling the rectangle itself isn't the issue, it's getting the rectangle into the correct position so that it highlights the same area. How do I do this? Is there a mathematical formula of some description that can be used to calculate its correct position?
Apply the same scaling factor to the X and Y offsets of the rectangle.

Categories