GuiTexture And Touch Inputs

My app consists of lots of guiTextures but I’m having some odd problems…

I have a background guiTexture which responds to mouse input as well as other gui elements in front of it (in the z). Now a lot of the time the elements in front of the background gui dont respond to touches, however if I revert that particular guitextures box collider to its prefab it starts to work even though the prefab setting are identical!
Whats more is that when I do this other guitextures in the scene which had previously responded well to touches are no longer detected…so I revert these setting and hey presto they work…
…but of course now other elements switch off…

This is really wierd and I was wondering if anyone knew what was going on. There are about 8 buttons plus the background which are all guitextures at different z positions.

Help!
Cheers :o

Hello! :smile:

What I do to touch elements is to cast a ray from the camera to where there was a touch, with that you can know where did you touched an element, regardless the Z position :slight_smile:

Hmm okay, but i’m not quite sure how to set my objects to be detectable by a ray. They all have box collisions but my ray doesnt seem to hit them…??

…and obviously I cant actually see where my box colliders are as these are all guitexture objects…I’m presumming they naturally form a bounding box if the scale is set to 1,1,1?

Hello,

Look what I do to detect the objects :).

private	var hit : RaycastHit;
private var ray : Ray;

function FixedUpdate () {
	if(iPhoneInput.touchCount == 1){
		ray = Camera.main.ScreenPointToRay(iPhoneInput.touches[0].position);
		Debug.DrawLine(ray.origin,ray.direction * 10);
		if(Physics.Raycast(ray.origin, ray.direction * 10,hit)){
			Debug.Log(hit.transform.name);
		}
	}
}

Hope it helps you out! :smile:

Yeah I think Ive got that cheers…The big problem now is trying to get the scale of the box colliders to match with the scale of the guiTextures??? How on earth is that possible? It seems every component has a different scale system…should my collider be 480 by 320…nope this seems too big, 1 by 1…nope…then what. Any idea on how to ‘fit’ your colliders neatly around you gui objects…??
cheers

mmhmmm I get you.

You should use then a button instead of a GUITexture, and add to the button the GUI Texture :),so the button shows the GUITexture (it should look exactly)

That should solve your problems.

Use the ray cast only for meshes with colliders :).

That sounds like pretty much restructuring everything so its all 3d…I might end up doing that in the end but i think there must be a way to get the z detection working correctly for guitextures. Its such a tiny problem that I’m kinda hoping I dont have toredo lots of code because of it,

hmm :?

well :frowning: as fas as I know, the Z axis is useless on the GUITexture component :(.

The z axis sets the depth of the guiTexture. A guiTexture with a positio.z of -1 will be drawn behind a guiTexture with a position.z of 0.

It all looks fine but touch detections dont work properly.

I get you, didnt know the Z axis worked on a GUI element, a new thing I learn today :smile:

The problem is, (as far as I know) a GUITexture, is not designed to detect inputs :S, that’s why I was suggesting you to use a button, but I’m not sure if you can play with the button Z axis on a game Object where the script is attached.

GUITexture is a completely different thing than GUI.xxx and OnGUI.
The later commonly is avoided by any meanings on the iphone unless you don’t care about performance. Use SpriteUI instead in that case

So it looks like I was doing the right thing by using GUITextures then…but what is this SpriteUI thing? I cant find any documentation regarding this?

Cheers

sorry to raise a pseudo old thread, but you if you’re using GUITextures and GUIText’s, you shouldn’t have to cast rays yourself or add colliders to them.

If you check into it, GUITexture and GUIText inherit from GUIElement, they have a hit test method. The way I’ve been handling it is my HUD Script has an arraylist of all interactive elements, when you click I run the hit test method on every GUIElement in that arraylist just using Input.mousePosition to see if I clicked one or not.

Hope this helps, or maybe I missed something in this thread.