- Home /
Unity 2D targeting multiple screen sizes
Apologies to ask about this old chestnut again but I cant find a clear answer.I've set my unity editor to show the game based on aspect ratio. I've done a rough draft of the game on the 9:16 ratio. I want to scale the game to stretch horizontally to cover other ratios. I previously coded android games using libgdx and used a percentage based approach to scale and position sprites. It doesnt seem to be working here though. Im getting problems scaling. I can get the scale of the sprite in my code (C#) sr.sprite.bounds.size.x but setting the new size is problematic. I think its the relationship between the units Im confused about. I can get the screen size but that doesnt relate to the size of the sprite and then the scale of the gameobject of the sprite is different again. Can anyone pop up a few lines to just illustrate scaling a sprite between ratios please?
Thinking about it further. I could get the ratio between the width and height of the sprite. Then set the new horizontal scale of the gameobject, then apply the new scale * ratio for the height to get it to scale in proportion.....$$anonymous$$ust try it.
Answer by _Yash_ · Oct 22, 2014 at 01:04 PM
The height of the screen in world unit is simply double of camera's orthographic size.
Y_scale = Camera.OrthographicSize * 2f;
World distance b/w left and right edge i.e. maximum X scale or screen X scale is:
X_Scale = Camera.aspect * Camera.OrthographicSize * 2f;
Yup had found those code snippets thx. Problem is the scale of the gameobject the sprite attached to is normally 1,1. The sprite however will have different scale. So i cant scale the object 1 for 1. I think ill try getting the ratio of the width versus height of the sprite and then applying that ratio when resizing the game object
Your answer
Follow this Question
Related Questions
Android resolution/aspect ration issue 2 Answers
Sprite in different aspect ratio 0 Answers
Handling Multiple Resolutions in 2D? 0 Answers
2D Level Design (programmatically or not) 1 Answer
Animataion of a Sprite v2 1 Answer