• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by paulaceccon · Jan 09, 2013 at 06:09 PM · guisizeguitexturetexure

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 = '*'.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfram · Jan 10, 2013 at 12:09 PM 0
Share

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?

avatar image paulaceccon · Jan 10, 2013 at 12:35 PM 0
Share

Edited. (:

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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).

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image paulaceccon · Jan 10, 2013 at 04:15 PM 0
Share

Hey, I don't need to adjust the position. At least not now... (:

Thank you a lot!

avatar image paulaceccon · Jan 10, 2013 at 04:17 PM 0
Share

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!

avatar image Wolfram · Jan 10, 2013 at 04:20 PM 0
Share

Exactly that: "rule of three", or "Dreisatz" in German.

avatar image paulaceccon · Jan 11, 2013 at 12:00 PM 0
Share

(: Thks!!

avatar image Wolfram · Jan 12, 2013 at 08:25 PM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

how big is my app size 2 Answers

Skinned/unskinned GUI controls size differences 3 Answers

dynamic GUI Texture.. clipping and rotation HUD 1 Answer

script to create gui when detection collision between cube and first controler person ?? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges