- Home /
Problem is outdated. This question has become irrelevant due to the introduction of UI in Unity 4.6. Leaving on sight for historical reasons.
GUI Texture scale and position according to the actual screen resolution..
Hi Everyone, how can I scale and position the GUITexture according to the current screen resolution. I don't want to use the built in GUI function which already include the scaling parameter.
omg would be so much easier if unity had an left right anchor seeting on gui textures
Answer by MrPhil · Nov 06, 2010 at 05:35 PM
This will center the texture based on the screen resolution and the image's aspect ratio (won't stretch it.)
private GUITexture myGUITexture;
void Awake() { myGUITexture = this.gameObject.GetComponent("GUITexture") as GUITexture; }
// Use this for initialization void Start() { // Position the billboard in the center, // but respect the picture aspect ratio int textureHeight = guiTexture.texture.height; int textureWidth = guiTexture.texture.width; int screenHeight = Screen.height; int screenWidth = Screen.width;
int screenAspectRatio = (screenWidth / screenHeight);
int textureAspectRatio = (textureWidth / textureHeight);
int scaledHeight;
int scaledWidth;
if (textureAspectRatio <= screenAspectRatio)
{
// The scaled size is based on the height
scaledHeight = screenHeight;
scaledWidth = (screenHeight * textureAspectRatio);
}
else
{
// The scaled size is based on the width
scaledWidth = screenWidth;
scaledHeight = (scaledWidth / textureAspectRatio);
}
float xPosition = screenWidth / 2 - (scaledWidth / 2);
myGUITexture.pixelInset =
new Rect(xPosition, scaledHeight - scaledHeight,
scaledWidth, scaledHeight);
}
Your last line should be: myGUITexture.pixelInset = new Rect( xPosition, (float)( screenHeight - scaledHeight ) / 2.0f, scaledWidth, scaledHeight );
$$anonymous$$y values seem to work properly if I set the position of the GUITexture to 0,0,0 and scale to 0,0,0 in the editor...
how do i manipulate this for other places in the screen other than the center?
Answer by IJM · Oct 14, 2010 at 09:19 AM
You can do this:
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), aTexture);
I believe for position, you would want Screen.width/2, Screen.height/2.
This is for fullscreen texture. If you want to show a logo or something similar.It will work as long as you have the same aspect ratio for texture and for screen. (4:3,16:9,16:10...)
This is the function that has to be called from OnGUI()... is there any calculation for the GUITexture Transform to make it exactly looks like.. so I don't have to calculate this everytime as OnGUI functions are expensive..
Thank you so much, IJ$$anonymous$$. This helped me a lot. I was using GUI.Label, but that always kept the aspect ratio of the image. GUI.DrawTexture stretched just how I wanted it too.
Answer by yoyo · Oct 14, 2010 at 06:03 PM
Attach a GUITexture to a game object and set the game object's transform to (0,0,0) for position, rotation, and scale. Now the pixel inset properties of the GUITexture control the screen position of the texture in pixels. (0,0) is the lower left, and (Screen.width, Screen.height) is the upper right.
Answer by Jameslewood · Feb 10, 2014 at 07:51 AM
Why don't you want to use the built-in scaling function?
For anyone else searching for the most simple solution, you can create a script with the following:
//
public Texture m_texture;
//
void OnGUI()
{
//
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), m_texture, ScaleMode.ScaleToFit, true);
}
Then in the editor, just drag and drop the fullscreen texture into the script. It will scale to fit and won't be cut off anywhere.
I tried it in both C# and in Javascript. I either get parsing error or I get an expected character ;. PLEASE HElP $$anonymous$$E
$$anonymous$$agicman, I think maybe you are miss an "}" at the end of your script to end the '$$anonymous$$onoBehaviour {'
Answer by psycocrusher · Mar 30, 2013 at 01:48 AM
This is what i use, replace the 570 and 380s with the resolution you're currently working on, somtimes it causes a little streching on the textures.
function Start () {
var DivideX= 570/guiTexture.pixelInset.x;
var DivideY= 380/guiTexture.pixelInset.y;
var DivideWidth=570/guiTexture.pixelInset.width;
var DivideHeight=380/guiTexture.pixelInset.height;
guiTexture.pixelInset = Rect(Screen.width/DivideX,Screen.height/DivideY,Screen.width/DivideWidth,Screen.height/DivideHeight);
}