- Home /
Scale GUI Texture to current screen size?
I'm making a 2D game for Android. Currently the game looks fine when the Game View is set to 800 x 480. But I am trying to get my GUI Textures to scale up or down according to the screen size.
My GUI textures are all 800 x 480 in size.
In the Unity Inspector, the Pixel Insets for my GUI Textures are set to X = -400, Y = -240.
Here's what I have as far as code:
var player1WinsGUI : GUITexture;
function Update(){
//Scaling textures
player1WinsGUI.transform.localScale = Vector3(0, 0, 0);
}
I am trying to come up with a formula that will scale the texture to the current screen resolution, which I know I can get using Screen.width and Screen.height. My guess was:
Vector3(Screen.height/800, Screen.width/480, 0);
But that results in positive values, which make the texture way too large.
1) Would this have anything to do with the way I have the Pixel Inset properties set in the inspector?
2) Does anyone know which units the Vector3 parameters are in? Is it pixels, percentage or something else?
3) Any ideas on how to do this the way I've set it up? Or is there a simpler way?
Thank you in advance!
Answer by Eric5h5 · Jul 29, 2012 at 08:44 PM
Set all pixel inset values to 0, and just set the scale to x=1, y=1 (and position to .5, .5). Then it will always use the entire screen regardless of resolution. GUIText and GUITextures use viewport space.
Aahh thank you so much, Eric5h5, that works perfectly! I'm trying to give you an up vote, but Answers is saying I don't have permission even though I'm logged in. :( I will keep trying.
In case anyone wants to reference what my Inspector now looks like:
Under "Transform": Position: (0.5, 0.5, 0) Rotation: (0, 0, 0) Scale: (1, 1, 1)
Under "GUI Texture": All number values set to 0. (I also had set the "Width" and "Height" under Pixel Inset to 800 x 480, which was adding to the problem)
It works great!! Thank you again!
You need a certain amount of karma before you can upvote, which you should have enough of now.
Cant make it work to auto fit atleast in Editor Player. Will it work on device? When I set scale to 1 for x and y then image became outside the screen.
Answer by ddkclaudio · May 31, 2014 at 10:13 PM
hello this solved almost all my problems, however I have an image with 88x179px, but when I do that and care she is cool but when it fits a screen unless the image is very pixelated.
appreciate the help