Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

The New GUI - why does GUI.Box scale relatively?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Foxxis, Oct 29, 2007.

  1. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Probably user error, but I don't get this.

    Code (csharp):
    1.  
    2. function OnGUI() {
    3.     cCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position);
    4.     if (fadeCounter>1) {
    5.         mMode = 0;
    6.         fadeCounter=1;
    7.     }
    8.     if (fadeCounter<0) {
    9.         mMode = 0;
    10.         this.enabled = false;
    11.     }
    12.    
    13.     GUI.Box (Rect(cCoord.x,Screen.height-cCoord.y,(135-135*(1-fadeCounter)),66), backPlate, customBack);
    14.    
    15.    
    16.     fadeCounter += fadeSpeed*Time.deltaTime*mMode;
    17.    
    18. }
    19.  
    The above box will scale with the proportions intact, even though I am only animating the width. I've looked at the GUI Style but haven't found an option to disable the behaviour. What's going on...?

    Another small niggle is that the new GUI system uses a coordinate system where 0,0 is the top left of the screen. Other functions in Unity (in my case WorldToScreenpoint) uses a system where 0,0 is bottom left. Have I missed something here as well?

    While I'm at it...how would I selectively fade elements? Can't see how I can adjust the alpha for one component - the setting seems to be grouped for either all objects, text objects, or background objects?

    Thanks in advance for any help! :)
     
  2. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Well, found the (easy) solution to this one. It seems whatever settings are made to GUI properties only holds for the objects created until a new property value is set. Pretty cool and very useful - didn't anticipate that... ;)
     
  3. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    So, noone knows why setting the width/height of a box to something other than the original image ratio simply scales the image to fit, rather than strecthes the image with the box..?
     
  4. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    I can only deduct it seems to be by design. Drawing an image only (which is basically what I'm after) can seemingly be done by setting a GUIStyle background to the wanted texture and then drawing a box with that. It seems a bit counterproductive though, since you have to declare a new GUIStyle for each image you want to display (stretched).

    Surely there must be an easier way...?
     
  5. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    One of the params of GUI.Box is a texture. Why not just pass the texture you want to the Box call, which will override the style:

    static function Box (position : Rect, image : Texture) : void
     
  6. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Thanks for the reply! :)
    However, I did just that at first (as you can see in the code above). But that's when it acts weird - the image won't stretch - it always keeps the same, original proportions. At least in my case.

    The only way to get it to distort is to set it as a background.
     
  7. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    Sorry, I didn't look as closely at your code as I should have.

    I've accessed the background of a GUI style at runtime, but the example below shows an (untested) assignment. That change _may be permanent, though, after the OnGUI function is ended, so you still might want to play around with a single custom style instead of a built-in style. Hope that helps more.

    Code (csharp):
    1.  
    2. var myTexture : Texture2D;
    3. var guiSkin : GUISkin;
    4.  
    5. guiSkin.Box.normal.background = myTexture;
    6.  
    [/code][/i]
     
  8. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Yeah, I was thinking about taking that exact approach, but too worried that it might be permanent. For now, I've simply worked around it by using the custom settings for the GUISkin. After all, I should try to reuse the settings more than I do, and it's not *that* common to have to stretch a texture.

    The particular use I have for stretching is a status bar, but perhaps clipping it (as in the examples) might be even better.

    To conclude my ramblings, I'd still like to see a picture/image component. GUI.Image - just to simple be able to draw an image within a specified rect with stretching and without any doodads. :)

    Again, thanks for your reply! ;)
     
  9. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    That's why I think if you create a single "scrap" custom style, and then manipulate that, it doesn't matter if its stays stuck with that background when you end because you'll always be initializing it at runtime.

    Just a thought.
     
  10. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Ah, I see... good idea! As long as it's always defined whenever used, it should work. It does have the limitation of just being able to display one image/scrap style though (otherwise I would guess all visible images would change when a change call is made). Still, better than nothing. :)

    Thanks!