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

Probably user error, but I don’t get this.

function OnGUI() {
	cCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position);
	if (fadeCounter>1) {
		mMode = 0;
		fadeCounter=1;
	}
	if (fadeCounter<0) {
		mMode = 0;
		this.enabled = false;
	}
	
	GUI.Box (Rect(cCoord.x,Screen.height-cCoord.y,(135-135*(1-fadeCounter)),66), backPlate, customBack);
	
	
	fadeCounter += fadeSpeed*Time.deltaTime*mMode;
	
}

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! :slight_smile:

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… :wink:

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…?

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…?

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

Thanks for the reply! :slight_smile:
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.

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.

var myTexture : Texture2D;
var guiSkin : GUISkin;

guiSkin.Box.normal.background = myTexture;

[/code][/i]

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. :slight_smile:

Again, thanks for your reply! :wink:

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.

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. :slight_smile:

Thanks!