- Home /
changing the sprite scale with screen resolution
ok ok i know the rule , search at first
search();
if(search == null)
{
ask();
}
so believe that its nearly an hour or maybe more that im searching but still....
im finishing my project and now i wanna change the scale of my sprites with different monitor resoluttion ive made my sprite with 1368 * 768 that is a 16/9 ratio...there are same questions in forum but whole of them are about gui ..but i havent used from gui elements in my 2d game and ive only used sprites that got transform.localscale not rect.... thanx
Answer by robertbu · Oct 16, 2013 at 05:41 PM
Given that you are talking about game object in world space, their height will remain constant across all resolutions. So you only have to adjust the width. Given you have the aspect you authored for, the math is simple:
newObjectWidth = OldObjectWidth * 9.0 / 16.0 * Screen.width / Screen.height;
but with your code my newwidth changes even in my origin res 1368* 764 for example
oldobjectwidth= 130 ;
1368/764 = 1.79
1.79 * 16 = 28.64
130 * 9 = 1170
1170 / 28.64 = 40 that isnt equal with my oldwidth..!!
ok , i guess the place of 16 and 9 should be changed , this is my code
var NewObjectWidth : float ;
var OldObjectWidth : float ;
function Start ()
{
OldObjectWidth = this.transform.localScale.x ;
//print (OldObjectWidth);
NewObjectWidth = ((OldObjectWidth * 16.0) / ((9.0 * Screen.width )/ Screen.height));
//this.transform.localScale.x = NewObjectWidth ;
print (NewObjectWidth);
}
function Update ()
{
}
but still it doesnt work,,,, maybe i should change the camera size...oah im confused , can anyone tell anything about this or i should solve this by myself
The formula is to first multiply by the inverse of the current aspect ratio and then multiply by the new aspect ratio. You can be a bit more accurate by using the actual resolution since it is not a perfect 16/9.
newObjectWidth = oldObjectWidth * 764 / 1368 * Screen.width / Screen.height;
Your parentheses change the calculation.
thanx , my background changes correctly , but the problem is , that some objects that are in corner of screen in my origin resolution , in lower res such 800 * 600 disappear from the camera , now is there a way to position them base on screen resolution , not in world system
You might take a look at viewport coordiantes. They start in the lower left at (0,0), and go to (1,1) in the upper right. So you can use a coordinate like (.15, .15) to put something in towards the lower left. You would use Camera.ViewportToWorldPoint() to convert this coordinate into a world coordinate for the placement of the object. Note you must set the 'z' component of the point to the distance in front of the camera before making the conversion to world coordinates.