Buttons gui?

Hi,

I may have missed it, but how do I go about adding a button on unity iphone and allowing the user to tap it and bring up a window (I guess another gui texture which you draw on top) with some information.

Thanks

You could use native Unity buttons. Look into GUI.Button; file:///Applications/Unity%20iPhone/Unity%20iPhone.app/Contents/Documentation/Documentation/ScriptReference/GUI.Button.html

Here’s some sample code that will display a number that increments when you click the button, you could modify it some to get your info window like you want it:

private var counter : int;


function Start()
{
	counter = 0;
}


function OnGUI()
{
	if (GUI.Button(Rect(8, 8, 128, 64), "Click Me!"))
	{
		counter++;
	}

	GUI.Label(Rect(144, 8, 128, 22), counter.ToString());
}

like this maybe:

private var counter : int;
private var showWindow : boolean;


function Start()
{
	counter = 0;
	showWindow = false;
}


function OnGUI()
{
	if (GUI.Button(Rect(8, 8, 128, 64), "Click Me!"))
	{
		counter++;
		showWindow = true;
	}

	if (showWindow)
	{
		GUI.Label(Rect(240, 8, 256, 22), "This is info, you clicked " + counter.ToString() + " times.");

		if (GUI.Button(Rect(240, 32, 128, 64), "Close Me!"))
		{
			showWindow = false;
		}
	}
}

thanks, that’s really great!!

For the iPhone you should avoid OnGui like the plague. Instead use GUITextures using hit tests. There are many examples of doing this on the forum including a length one posted by me a while back.

Really? I didn’t know that.

[EDIT]: I see that now in file:///Applications/Unity%20iPhone/Unity%20iPhone.app/Contents/Documentation/Documentation/Manual/iphone-basic.html

Somehow I missed that before. Holy crap, I’ve been using OnGUI quite a bit. For most everything GUI related actually. Wow. This changes the equation for me.

So I’ll get a frame rate bump using GUITextures vice OnGUI calls?

This is the one you refer to I reckon:
http://forum.unity3d.com/viewtopic.php?t=29229&highlight=hanultech+guitextures

Yes, you should get a performance gain by not using OnGui, depending on how extensively you have been using it. You should be able to construct whatever GUI elements you need with GUITextures and it will be much more performant. You want to keep the number of GUITextures you use as low as possible because of the draw calls. Rather than construct static GUI parts as individual GUI textures, put them together as a single texture using Photoshop or your tool of choice.

Thanx HanulTech!

Roger that, I do that now. Currently all my buttons are two state though, and I’m re-evaluating that, at least for in game buttons. Each “touched” state = another draw call, and the buttons are often small enough that you don’t really see the second state very well anyway. I’ll probably keep two state buttons for menus though as there is so little going on that the extra overhead isn’t as much of an issue, and the buttons are larger therefore making the different states more distinguishable.