x


Detect when a touch has been captured by the GUI

I'm making a game for the iPhone with GUI buttons. And in Update, I look for touches for another purpose, but I don't wan't to process touches if they are on a GUI element.

The problem is that iPhoneInput.touchCount and iPhoneInput.touches contain all the touch information whether or not it has been processed by GUI, and those variables are actually available in Update and FixedUpdate before GUI gets to do anything with them.

Now I can come up with a hacked solution, no problem (validate the touch data by my GUI function and only use touch data when it has been validated). But I wondered if there is a clean, supported solution.

more ▼

asked Nov 19 '09 at 06:59 AM

Sam Bauwens gravatar image

Sam Bauwens
145 3 3 12

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

We originally tried OnGUI() for iPhone, didn't really seem to work well. (probably because of the reasons you defined above)

What we ended up doing was just implementing rectangles for various areas of interest on the screen, and then checking the touches to see if they were within one of those rectangles and at the correct phase. From there we just handled the touch appropriately.

Of course at this point it is no longer immediate-mode because we just had predefined GUITextures in the scene.

Hope that helps.

more ▼

answered Nov 19 '09 at 08:38 PM

equalsequals gravatar image

equalsequals
4.5k 16 28 64

Not using GUI is might indeed be the way to go since they probably won't support address this problem as it would augment latency.

Nov 20 '09 at 06:42 AM Sam Bauwens
(comments are locked)
10|3000 characters needed characters left

Use layers, create custom layer for your touchable objects and then ignore the rest, or limit your raycasting by creating a layermask, and ignore the objects or areas that you don't want.

Check this out, he uses a layermask ignoring a custom layer named GUI and the ignore Raycast layer, and get touch events from the rest. Apply the same concept, so only when a ray reaches your object(under your custom layer) you will trigger your touch code.

http://www.youtube.com/watch?v=oOfPMKdJdKk

source code is here:

http://www.revelopment.co.uk/tutorials/unitytutorials/73-howtorotateanobjectbytouch

Hope it helps.

more ▼

answered Jun 21 '11 at 01:01 AM

revelopment gravatar image

revelopment
61 1 1 1

(comments are locked)
10|3000 characters needed characters left

In my game the GUI does not normally overlap the gameplay area. The only time it does is when a "modal" dialog box is showing. So before reacting to a click or tap, the gameplay code asks the dialog box manager class if there's a dialog box showing. If there is, the click is ignored.

This works but is a little weak. It requires cooperation between my classes. If I were to write a new class that put up it's own masking visual element, the gameplay code would have to check for that as well.

A more elaborate system might be the following: classes interested in events would register with an event manager class. Before taking any action on a click they would first check to see if they were the "top-most" event handler. If not, they would ignore the click.

This would handle modal dialog boxes, but if you wanted to allow the current event handler to "pass" and let the next handler take an event (a non-modal dialog box for example, when the user clicks outside) then you have a new set of problems to solve.

more ▼

answered Nov 20 '09 at 03:14 AM

Bampf gravatar image

Bampf
5k 8 19 49

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3695
x2000

asked: Nov 19 '09 at 06:59 AM

Seen: 4731 times

Last Updated: Jun 21 '11 at 01:01 AM