GUI Bug?

Hello all,

Not sure if this is a bug or me doing something wrong.

var customSkin : GUISkin;
var ourImage : Texture;

function OnGUI () {
	GUI.skin = customSkin;

	if(GUI.Button(Rect(Screen.width/2-100,Screen.height/2-100,200,200), ourImage))
	{
	print("test");
	}

	
	// Now create any Controls you like, and they will be displayed with the custom Skin
	//GUI.Button (Rect(Screen.width/2-100,Screen.height/2-100,200,200),ourImage);
	

	// You can change or remove the skin for some Controls but not others
	GUI.skin = null;

	// Any Controls created here will use the default Skin and not the custom Skin
	GUILayout.Button ("This Button uses the default UnityGUI Skin");
}

the code is from the docs, I made little changes to it but the prob I have is in the inspector, when I add/drag my texture to the inspector(ourImage var) I get double Image, if I leave the var ourImage blank (ref.unassigned and no errors), the gui is fine, see attached image.

Not sure if its a bug or just me not doing it right. sorry just playing with the new GUI system.

Thanks,
Ray

52933--1925--$gui2_204.jpg
52933--1926--$gui1_933.jpg

I suspect it’s due to combining GUI.Button and GUILayout.Button - I don’t think they mix well, and you’ve got both in there.

Even If I remove the GUILayout same prob.

Not assigning a texture reference in the inspector(declared as global var or public? in the script) is not giving any refence not set to instance error.

I’ll re read the manual on this GUI stuff again, maybe I just missed something as always :-).

Ray

You’re assigning your texture as GUIContent and as a GUIStyle.
I can’t figure out exactly what you want to do, but I think the solution may be to create a custom GUI Skin, set it up correctly with all the button images first, and just leave the content blank.
i.e.

GUI.skin = myCustomSkin; //Use your custom Skin
GUI.Button(new Rect(0,0,100,32), "");
GUI.skin = null; //Use the Unity Skin
GUI.Button(new Rect(0,40,100,32),"");

The point is to setup the background texture for the button in the skin at design time - at runtime when you create a button using GUI or GUILayout it will just work.

You can script custom GUIStyles at runtime, but it might be better to get the hang of things first.

@StarManta
It’s perfectly OK to mix GUILayout and GUI together - and is sometimes necessary.

hope this helps
Shaun

Shaun,

Sorry, just started playing with the GUI stuff, basically I just wanted to create a custom button(for now) that a user can press and it will activate an event. Your Script did work , I just adjusted the size of the button to make the image more readable.

Question, How do I adjust the size of the button of the GUI?
Right now when I hover the mouse over the GUI it will render the onHover image even when the mouse is not on top of the GUI. See image below GUI1 (Bottom Image) is normal mode and GUI2 (top Image) is when I hover the mouse over. The side of the GUI hovers fine(mouse at the edge and hover image activates), top and bottom only, tried playing with borders and margins set to 0 but not working.

Thanks,
Ray


The size is controlled by the Rect(X,Y,Width,Height).
It looks like your button is a fized sized graphic so you just need to set the Width and Height part of the Rect to match the image dimensions.
If you want to make resizable buttons, you need to use the border settings, which control which part of your image is stretched and which parts remain “pixel perfect”.

I think you may benefit from doing the GUI tutorial - especially the part on creating custom GUI skins. FYI a GUI Skin is just a design time collection of GUISkin settings and GUIStyles that can all be created and controlled via scripting.

Also important to note is that the GUI is immediate mode, which means that changes made inline in the code will affect whatever you GUI code you call afterwards. Therefore, if you specify GUI.skin = null (which means use the built in Unity Skin) it will continue to use that skin for all remaining controls that get rendered.

hope this helps - the GUI can be tricky at first.

Sorry not to respond right away, just got some free time today,

Yup, texture size is 1024 by 512. Why that big, I could probably make it smaller but I use this and others on a plane so I can have some mouse over feature(audio etc). Trying to use the new GUI to replace my own generic gui(buttons) that I am using on world capitals game.

Is there a specific size that a texture image should have to be used as a gui background, so it will fit nicely. sorry rambling over here :slight_smile:

Will experiment with that one and the other controls (wrap, etc)

Very tricky… would love to have some more tutorials on it though :wink:

Thanks,
Ray

A need we recognize and will hopefully get to sooner as opposed to later.