- Home /
Texture height based on it width.
Hi, I have a GUITexture which content changes time by time. All texture defined have the same width, but the height may change. Until now, I have something like this:
void Start (){
messageSize = new Vector2(Screen.width/3, Screen.height/5);
messageGO = new GameObject();
messageGO.transform.position = Vector3.zero;
messageGO.transform.localScale = Vector3.zero;
messageGUI = messageGO.AddComponent<GUITexture>();
messageGUI.name = "Message";
}
void Update(){
if(displayMessage){
timer += Time.deltaTime;
if(timer <= time){
messageGUI.enabled = true;
messageGUI.texture = message;
messageGUI.pixelInset = new Rect(position.x,position.y,messageSize.x,
messageSize.y);
}
else{
messageGUI.enabled = false;
displayMessage = false;
timer = 0f;
currentMessage ++;
}
}
}
So, I was wondering if there is a way to make this messageSize.y adjusts dynamically. Something like we do in HTML, passing width = messageSize.x and messageSize.y = '*'.
What I have so far are fix messageSize. I would like that the messageSize.y change to have the necessary size, like we can do in HTML with the parameter height = '*'.
Either I don't understand your problem, or simply call the two lines you posted every time your texture changes (plus updating messageSize).
And please give more information. For example, who computes/updates messageSize? Are the textures "message" created via script, or referenced, or where do they come from?
Answer by Wolfram · Jan 10, 2013 at 01:41 PM
If you want to keep the width constant, and at the same time preserve the texture's aspect ratio, you can do this:
float messageNewHeight=messageSize.x*message.height/message.width;
messageGUI.pixelInset = new Rect(position.x,position.y,messageSize.x,
messageNewHeight);
If you also need to adjust position.y, it gets a bit more complicated. Just tell me if you need that, and if so, how/where do you want to align your texture (bottom/center/top of texture).
Hey, I don't need to adjust the position. At least not now... (:
Thank you a lot!
Oh man, it was not hard! It was just... I don't know how you guys call these in english, but in portuguese is "Regra de Três". Thaaaanks!
Ah, O$$anonymous$$, then the problem was power of twos. Per default, all imported textures are fitted into a power of two width and height, because the graphics card is optimized for that, and mipmapping won't work in Unity otherwise. Setting it to "GUI" disables this scaling. You can also disable it by using the "Advanced" option, but GUI is fine - since you are using it as such.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090650im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
Move GUI elements. 0 Answers
GUITexture Button? 1 Answer
GUI Transparency Changing automatically 2 Answers
Write a Shader for GUI (GUITexture or scripted GUI) 0 Answers