Unity 3.4 - How to detect touch on a guiTexture

I know this subject has been discussed with earlier versions of Unity. I read and tried the examples in:

http://forum.unity3d.com/threads/30562-Detecting-touch-on-a-guiTexture

They do not work within my game and I’m wondering what else might be wrong. Any feedback anyone has to offer will be appreciated.

Attached to my main camera I have four guiTextures, main_menu_1, main_menu_2, main_menu_3, main_menu_4. When the game loads on the iPod these are displayed as menu buttons on the screen. Attached to main_menu_1 is a script called button_press_1 which was taken from the above example and is:

var level : String;
var recoveryTime = 10;

private var delay = 0;

function Update () {
if (delay>0){delay -=1;}

if (delay==0){
if(Input.touchCount == 1){
var currentTouch:Touch = Input.touches[0];
Application.LoadLevel(level);
delay = recoveryTime;
}
}
}

I had hoped that touching this button would take the player to the next level of that game as it does in the MacOS player but instead nothing happens regardless where I touch on the screen.

Can someone provide a working version of detecting a touch on the guiTexture with Unity3.4 or at least explain what I am doing wrong in the above example?

Thanks

Thanks.

That script should load “level” when you touch anywhere on the screen. Is level assigned to a variable? Have you put all the levels in the build?

do a hittest

http://unity3d.com/support/documentation/ScriptReference/GUILayer.HitTest.html

HI,

Yes we call Application.LoadLevel(level); and we define the variable level via the inspector. We also assign this to a guiTexture so we really want to know when a hit is registered against a specific area on the screen.

Hi,

We did try the hit test in a different version of the script. That version contained the following and was taken from a message in the forums that purportedly worked with the previous version of Unity.

function Update () {

if (delay>0){delay -=1;}

if (delay == 0){

if (iPhoneInput.touchCount ==1){

var currentTouch:iPhoneTouch = iPhoneInput.touches[0];

if(currentTouch.phase == iPhoneTouchPhase.Began guiTexture.HitTest(currentTouch.position)){

guiTexture.texture = button_down;
audio.PlayOneShot (mouse_click);
(gui_prize_level.prize_level)=0;
(power_meter_bullseye.pingpong_speed)=2;
Application.LoadLevel(level);

}

You can just use the OnMouseDown handler that’s in MonoBehaviour.

void OnMouseDown()
{
//do stuff
}

On mouse up and down doesn’t work on iPhone does it? And even if it does all that would let you know is if you clicked a certain object.

I use this crude method to pickup single screen presses on iphone/ipad:

//check buttons here!						
					if (Input.GetMouseButton (0)  ){
			            var         mx = Input.mousePosition.x;
			            var         my = Input.mousePosition.y;
							
						print(" MOUSE.... mousex=" + mx + "  mousey=" + my); //print mouse coords for debug
						
						//Button 0 (top left)
						if ( mx>68  mx<313   my>387  my<585)
						{
do what ever in reaction to button...

its not multi-touch but it works, both on the Mac when coding and on the device.

hope this helps

Thanks. We actually have a touch script working. It was not the script but the number of images unloaded when moving from the main menu to the game menu that caused so much delay it seemed the touch was not working. Adding some debug output to the script and monitoring the xcode console clearly showed the cause of the delay.