- Home /
Automatic GUI resizing
I everybody,
I've got a GUI texture used as background for some GUI elements. I made a textre in 1280*800 to fit with the biggest smartphones screens, but this texture doesn&'t be resized when I use a smaller screen.
How could I do that ?
Tks
Tooo, I hoped there is something automatic :( But tks anyway, I'll try to find a way !
Answer by SomeGuy22 · Sep 04, 2012 at 01:37 PM
This is a common problem with most games, and every single Unity Game I've seen that uses GUI elements FAIL to resize the buttons for multiple resolutions. What if (On a standalone build, the player does NOT want to play in full screen?)
What you're looking for is basically what I use for all my GUI code, with a few changes.
To make a re-sizable button, we look at this code here:
if (GUI.Button(Rect(.1 * Screen.width, .8 * Screen.height, .2 * Screen.width, .1 * Screen.height), "My Button"))
What you're looking for is a texture that fills the whole screen, so we do a similar statement:
GUI.DrawTexture(Rect(0 * Screen.width, 0 * Screen.height, 1 * Screen.width, 1 * Screen.height), myTexture, ScaleMode.ScaleToFit, false, 0);
Which can be simplified to:
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), myTexture, ScaleMode.ScaleToFit, false, 0);
Tks SomeGuy22,
I tried something like that :
var myTexture ; var GObject : GameObject;
function Start () {
GObject = GameObject.Find("gui_background");
myTexture = GObject.guiTexture;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), myTexture, Scale$$anonymous$$ode.ScaleToFit, true, 1.6);
}
And I've got that error message :
InvalidCastException: Cannot cast from source type to destination type.
As the noob as I am, I don't understand ...
You need to define myTexture as a Texture2D:
var myTexture : Texture2D ; var GObject : GameObject;
I've already tried this and Unity tell me this :
BCE0022: Cannot convert 'UnityEngine.GUITexture' to 'UnityEngine.Texture2D'.
and if I defined myTexture in the start function like this: myTexture = GObject.GetComponent(Texture2D);
I've got a null reference execption: NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, Scale$$anonymous$$ode scale$$anonymous$$ode, Boolean alphaBlend, Single imageAspect)
I thought it was because I defined my texture as a GUI texture ins$$anonymous$$d of a texture in the inspector but it doesn't change anything...
$$anonymous$$y bad, looking at the scripting reference:
http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTexture.html
It needs to be just Texture.
var myTexture : Texture;
I don't think you should have that code in Start (). Get rid of everything in the Start function and put the GUI.DrawTexture in OnGUI() ins$$anonymous$$d. Then just drag the texture into the variable in the inspector, because you might be confusing it by grabbing it from GObject.
EDIT: Actually it might be Texture2D... try both.
I'm such an as...le :)
I found the problem, I just put the line :
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), nomTexture, Scale$$anonymous$$ode.StretchToFill, true, 0);
In the GUI function and not in the start one :
var GObject : GameObject;
var myTexture : GUITexture;
var nomTexture : Texture;
function Start () {
GObject = GameObject.Find("gui_background");
myTexture = this.GetComponent(GUITexture);
nomTexture = myTexture.texture;
}
function OnGUI () {
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), nomTexture, Scale$$anonymous$$ode.StretchToFill, true, 0);
}
But it create an other problem, I can't ajust the depht, it seems to stay on forground !
Your answer
Follow this Question
Related Questions
Overlaping GUITexture, help! 1 Answer
transparent background with GUI.DrawTexture 0 Answers
How to place/add a HD image/picture in background of my game? 0 Answers
background GUI texture 2 Answers