- Home /
How to have a sprite with fixed size and position relative to screen resolution?
I've been going through this for quite a while now. Say I have a sprite. And it looks just right in 1366x768 resolution. When I change the resolution - the sprite changes shape and is in a slightly different position. I've worked with OnGUI and already know how to keep the position, scale, etc. just right, but this is not the same deal. So, on to the main question - what would be the code that, once attached to the sprites, locks it's location, size and scale, and looks good on every resolution, plus - the sprite itself still looks good and not pixelated. I'm using C# and I know this is quite a big and complex code, but if anyone would at least go as far as lead me on the right track - I'd be grateful.
I cannot think of a reason why the Sprite would be changing shape. In Unity, the camera see the same number of units vertically no matter what the resolution, and a Sprite is a world object. Unity scales the horizontal of what the camera sees to fit the aspect ratio. So the what the camera sees will be larger or smaller than what you originally authored for depending on the aspect ratio of the display. So your Sprite doesn't really move around in world space. If you are trying anchor the sprite relative to the camera view (i.e. bottom left corner for example), you will find many posts that tackle this problem. Usually they involve mapping either screen coordinates or viewport coordinates to world coordinates (see Camera.ScreenToWorldPoint() and Camera.ViewportToWorldPoint()). As for looking good, you can only author 'pixel perfect' for one resolution. That is in only one resolution will the size be right and the pixels of the image match the pixels of the display perfectly. Generally reducing the size of image has less visual problems than increasing the size of the image.
How do I then have a fixed size of it? I tackled this again and the shape is not stretched and remains the same, but on lower resolution the sprite does appear bigger. It should remain constant in all resolutions.
World objects should remain vertical size as a fraction of the screen size. That is, the vertical size seen by the camera should remain the same (barring any scripts that are making changes). Smaller screen will be smaller size of all world objects, but the vertical view should remain the same. So if you put a world object centered at the top and centered at the bottom of the screen, they should stay in the same place as you move from resolution to resolution. If you are not seeing this, then I'm at a loss. $$anonymous$$aybe you have some code making changes, or there is something special about sprites vs other world game objects that I'm not aware of.
P.S. There may be an issue with pixel density horizontal vs vertical across devices. That is, if you had two screens that were both 1366 x 768 but one was physically longer horizontally than the other, then you would see a bit of stretching. Just a theory, but if true, Screen.dpi might be something to look at.
I do work with dual monitors, but I checked on both - the resolution change means that my sprite either gets stretched or it changes size. I'm making a main menu for the game, which is to be resolution independent. I made a HUD with OnGUI and it works just fine - all the textures in the same place, looking crisp as ever. But for the main menu, which might include even 3D objects, I tried using sprites - as I said, the height and position eventually remained the same, but they would start stretching. If the stretching would stop (random playing around with values), then the size would change. Now I've tried the NGUI, it seemed promising, yet with it I encountered pretty much the same problems... I hope this makes sense. In any case - I really value your help, thank you.
$$anonymous$$aybe there isn't any real way to go around this? I mean, many games have rather bad looking menus if you play them on very low resolution, and then again - who would play a game on 600x400 or whatever these days. But maybe I'm just making excuses, it's been hell of a long day.
Answer by PhoenixBlackReal · Jul 29, 2014 at 12:59 PM
To those who still look for an answer and this might help - I solved the problem. Add this line to the script on the sprite:
sprite.transform.localScale = new Vector3(Screen.width/1366f, Screen.height/768f, 1);
The sprite should give pixels to units: 1.
The orthographic camera should have this script attached:
camera.orthographicSize = Screen.height / 2;
Once played the sprite stays the same size and looks quite good even in the lowest resolution. Hope this helps those who need to have a fixed size sprite looking crisp and resolution independent!