making a GUIContent.Texture larger than its actual file size

Hi there! I’ve been running in circles all morning so I guess it’s time to get help…

I need to have a full-screen transparent texture on top of some GUI elements (buttons etc).
It appears that an old GUITexture is always behind a GUI 2.0 element, so I need my texture to be a 2.0 element too. Why not.

So I’m using GUI.depth to layer my elements, and it works fine. But now my texture cannot grow larger that its actual size, even though I checked “stretch width” and “stretch height” in my GUISkin.

So… does anyone know a way to make an GUI2 image larger than its size?

As a side note, I had a hard time finding GUI.depth because it isn’t called z-index as is common, but it was worth it because I had the overwhelming pleasure to discover it’s ordered in reverse from the old GUI system that used position.z (which was, again, the common order: bigger numbers go in front)… nice one UT :P.

Oh, second one, GUI skins have “stretch width / height” properties, while script-side it’s called GUILayout.ExpandWidth / Height()…

Are you adding the texture (i.e. like an icon) to a GUI Box? If so, I believe that the size should always be the same of the texture. The only way that I think you could use to make it bigger could be to scale the GUI.matrix (BTW, if you want to change the matrix, use GUI.matrix = Matrix4x4.TRS(…) and not GUI.matrix.SetTRS(…), since Matrix4x4 is a struct and this will not work! Took me a bit of a time to figure that one out…).

But why don’t you create a Box with a different style? Change the Box’s default texture to the texture that you want to use.

Regards,
Afonso

Obrigado nafonso!

Actually I just found out that setting style.normal.background instead of GUIContent.image does stretch the texture to the size of the element, so yay, problem fixed!
This works for any element, button, label, box, just put all borders to 0.

I’m afraid I didn’t get the Matrix thing but if someday I have another need I’ll think about it :wink: !

For the record, I used a GUI.Label (since there’s no GUI.Image… with no padding by default or something… that would have been nice), with a custom style to display ImageOnly.
GUI.Box(myImage) behaves the exact same way.

Also you say that “the size should always be the same of the texture”… nop, it’s actually scaled down automatically if the element is smaller, that’s why I found it weird that it’s not scaled up…
I’ve even seen a post saying “what if I don’t want it scaled down?”. I add: what if I want it stretched, or tiled?

Até mais cara :wink: !

The next release contains a function that has lived under the internal name of JustDrawTextureGoddammit… Guess what it does :slight_smile:

Well, that’s good to hear, every time I want to just-draw-a-texture-dammit, my finger slips and tries GUI.Image() but Intellisense stubbornly refuses to find it :wink: !

Will there be stretch/tile etc options? Tiling would be so neat… I’ve sent a request about it, case #16432.

No tiling for now, but various StretchToFillWhileMaintainingAspectRatio kinda thingies are there

JustDrawTextureGoddammit sounds like a great idea.

Do you have any tips on how to achieve the same result with the current version of Unity?

I would like to simply draw a texture on top of everything so it fills the whole screen. It’s ok if the texture gets stretched to match the screen coordinates.

I’m a Unity beginner. Isn’t there something in GUI that allows to simply cover the whole screen?

When I connect this script to the camera, it almost works.

var fadeTexture : Texture2D;

function Start () {
fadeTexture.Resize(Screen.width,Screen.height);
}

function OnGUI () {
GUI.Label (Rect (0,0,Screen.width,Screen.height), fadeTexture);
}

Except that the texture turns black and it does not fill the entire screen.

I managed to fill the entire screen by using a custom style:

var fadeTexture : Texture2D;
var totStyle : GUIStyle;

function Start () {
fadeTexture.Resize(Screen.width,Screen.height);
}

function OnGUI () {
GUI.Box (Rect (0,0,Screen.width,Screen.height), fadeTexture, totStyle);
}

It’s still black, though…

You want to assign the texture to the normal.background property of you guiStyle - and not do any rescaling.

That works perfectly. :slight_smile:
Thank you!

This does not, however, seem to work with a RenderTexture that is the size of the screen.

I get “Cannot convert ‘UnityEngine.RenderTexture’ to ‘UnityEngine.Texture2D’”.

Both Texture2D, and RenderTexture inherit from Texture, which is what the GUI calls want. So make your reference Texture instead of Texture2D.

Texture2D is a Texture, RenderTexture is a Texture, but a RenderTexture is not a Texture2D, thus the conversion error. :slight_smile:

Cheers,
-Jon

Thanks for the tip. Sounds good.

But I coded it like this:

var totStyle : GUIStyle;
private var closeUpTexture : Texture;//instead of RenderTexture

function Start () {
closeUpTexture = RenderTexture(Screen.width,Screen.height,0);
camera.targetTexture = closeUpTexture;
}

function OnGUI () {
totStyle.normal.background = closeUpTexture;
}

And it doesn’t work. Now I get “Cannot cast from source type to destination type”.

Ahh… that’s not going to work.

GUIStyleState.background is a Texture2D for whatever reason. I thought you were referring to your earlier code that uses GUI.Box(). GUI.Box takes a Texture. (As do I think all the GUI and GUILayout methods.) Could you use those?

Cheers,
-Jon

It works with a Box.

GUI.Box (Rect(0,0,closeUpTexture.width,closeUpTexture.height),closeUpTexture,totStyle);

I mean: the image displays correctly. But despite of that, I get an error message that says “Error assigning 2D rectangle texture to 2D texture property ‘_MainTex’: Dimensions must match”. Should I ignore the error message? Or is there something I need to change or add?

Well… I’d say it expects a square texture are your RenderTexture isn’t square… so changing your camera aspect ratio or something should rid you of the warning.

BTW, sorry to barge in on the discussion, I still get topic updates in my mail but I didn’t follow thoroughly ;), so I hope I’m not way off.

Thanks. This may be a good tip. So how does one change the camera’s aspect ratio?..

And what should it be set to?

Go to your cam > “normalized view port rect”.
The aspect ratio is width/height (or the contrary, I can never remember), but that’s relative to the screen.

  • You want it to be square (1:1), if I got the error message right.
  • Now most likely your screen will either be 16:10 or 4:3 (or maybe 32:10 if you’re a millionaire and you own a curved Alienware screen, but let’s say it’s 4:3 to simplify).
  • So if your cam occupies 1:1 of the screen, its aspect will be 4:3.
  • So… set height to 1 and width to .75 (= 1/4:3, or 3/4), and bam, you’ve got yourself a square cam.

Now of course you’d have to script it to cover every possible resolution/ratios. And also maybe you don’t want your texture to be square, in which case you’re boned from the start.
I could also be talking nonsense from the start, as I’m only interpreting from the error message you gave but never played with RenderTextures myself… who knows?

Thanks for the instructions. But my RenderTexture is the same size as the screen. So not square.

How do shaders do this in Unity?
Perhaps I can borrow that technique to draw a fullscreen image on screen?

It does work with a GUI Box. Despite of the error message. Is the error message wrong perhaps?