- Home /
Uncompressed Texture for GUI Doesn't Display
I am using a texture to display text in a specific font and font size on the screen as a part of the GUI. The texture is too big because it covers the main character during game play. I've been trying to resize the texture in the start function, but the texture doesn't display when I do. (The texture does display when I do not try to resize the texture) Here's my code for resizing the texture:
var xGUITexture : Texture2D;
function Start (){
xGUITexture.Resize(Screen.width/2,Mathf.RoundToInt(Screen.width * 0.3),xGUITexture.format,true);
xGUITexture.Apply();
}
//[...]
Am I using the wrong texture format? What would cause the texture not to appear? (I'm not showing the calls for showing the texture because they are working with the texture when it is not resized).
GUITexture
is a Unity class, you shouldn't name your variables identically to these. Do you have #pragma strict
enabled?
I changed the name of the texture, since I am using the name only as an example. And yes, I do have #pragma strict enabled
Well, the calls you use to display your texture are important, since we have no way of knowing whether you are using UnityGUI, the actual GUITexture object, or a 3D plane that's textured with a Texture2D to display your GUI.
This may also deter$$anonymous$$e whether resizing the texture is even the right approach (e.g., it will not change the displayed size when using textured 3D objects).
do you have to change the texture size, wouldnt be easier to change the scale of the gui
AFAI$$anonymous$$, it only affects everything that comes after it, and only in that particular call to OnGUI().
Answer by Wolfram · Jun 18, 2012 at 10:14 PM
Oh, just noticed this myself.
Reading documentation does help: "After resizing, texture pixels will be undefined."
So texture.Resize() does not rescale your texture, it simply changes the dimensions and provides the memory adaption, but deletes the content.
And since non-uniform scales of a GUI.Label's Rect() are apparently (and unfortunately) ignored, your best option is to leave the texture alone, and scale your GUI:
function OnGUI(){
// store original matrix
var guiMatrix:Matrix4x4=GUI.matrix;
// where the label should be placed
var labelRect:Rect=Rect(xPos,yPos,xGUItexture.width,xGUItexture.height);
// scale GUI relative to current label size
var scaleFactor:Vector2=Vector2(
Screen.width/2 / labelRect.width,
Mathf.RoundToInt(Screen.width * 0.3) / labelRect.height);
// scale GUI around center of label
GUIUtility.ScaleAroundPivot(scaleFactor, labelRect.center);
// place label
GUI.Label(labelRect,xGUItexture);
// reset GUI scaling
GUI.matrix=guiMatrix;
// other GUI stuff, un-scaled
// [...]
}
As I said, I don't really use UnityGUI, so no guarantees ;-)
Still nothing... I don't know how my code compares, but it looks similar:
funciton OnGUI(){
var labelRect : Rect = Rect(Screen.width - question.width,0,question.width,question.height);
var scaleFactor : Vector2 = Vector2($$anonymous$$athf.RoundToInt(Screen.width/2/labelRect.width),$$anonymous$$athf.RoundToInt(Screen.width * 0.3 / labelRect.height));
var pivot : Vector2 = Vector2(Screen.width,0);
GUIUtility.ScaleAroundPivot(scaleFactor,pivot);
GUI.Label(labelRect,question);
}
I assume you removed the code in Start() messing with the texture?
What exactly happens, don't you see the texture at all, is it black, is it unchanged in size? Please elaborate. Your code seems fine, from what I can see.
One thing I see, change the Screen.width/2
to Screen.width/2.0
, so it will be a float. Otherwise the result is 0 if labelRect.width is > Screen.width/2.
First, to the questions, the texture does not display at all, and the Start function and all of its contents have been eli$$anonymous$$ated.
Second, I tried the suggestion,but nothing changed (but I'm definitely keeping the change, though).
Then experiment around a bit with scaleFactor and pivot (make them modifyable in the inspector), starting from neutral values ((1,1) for scale, screen center for pivot), or comment out that line completely, to figure out where the problem lies.