- Home /
"Capturing" "events" in Unity....
Very typically, you might do this somewhere
function OnGUI ()
if ( GUI.Button( Rect(20,20,50,50), "Go For It" ) ) _allez();
but somewhere else you are doing this,
function FixedUpdate()
if (Input.touchCount == 1 )
{
thisPointWorldSpace:Vector3 =
gamecam.ScreenToWorldPoint( Input.touches[0].position );
if ( something like thisPointWorldSpace.y >
fuckingButtonMarkers.position.z blah blah )
return;
_createAstoundingSpaceshipAt( thisPointWorldSpace );
}
Of course, both pieces of code get the "event", the touch.
(I use 'event' in a general sense, I have no clue if .Net ... or whatever this is .. uses "events" or if they are nomeclatured differently, or the like.)
(This is of course very counterintuitive to say a cocoa programmer, where you have to sign-up for events, explicitly pass them on if you happen to want to, and so on.)
My hard question
(1) Can you intercept such "events" at some lower level? And then decide WTF to do with them or not do with them?
Essentially, in your button code can you more or less say "Oh, and by the way, do NOT let any one else get this touch 'event'".
I love and adore the slop flexibility in unity "everyone gets all events" but in a word can you bypass this or work with it?
My easier question
(2) In fact, what idiom do most people use to handle the situation described? obviously you can use a crappy boolean global or annoying little exclusion boxes or ???????
Thanks for this...
Damn good question :) You can certainly "Use" and event in OnGUI, but whether that stops it being passed to non OnGUI code is another matter - and I don't know the answer (and am not at my computer to check!). It wouldn't fix your touchCount issue of that I'm sure.
It might be just me, but looking through my code (admittedly on my iPad) I obviously got fed up with mouse simulation of touch and something to do with phases which meant I wrote a wrapper that I added a CancelTouch method to that forces my touchCount to return 0 and clears the status of fingers on the screen (ignores them until they are lifted and replaced).
The whole thing's a big drama. I mean surely you can "capture" events and then either pass them on or not?
Someone's gotta know this !
Answer by Bunny83 · Jun 15, 2012 at 02:31 PM
No, you can't intercept events since Unity either pass the events directly to the relevant objects, like OnCollisionEnter, Start, ... or it provides state information about a certain inputs like the touches property does. This is already the lowest level. It's the direct system information of that touch. It isn't passed to anyone. You have to read it manually. If you want a special distribution, you can implement your own event dispatcher where you sould specify a certain priority for different subscriber / listener or something like that.
I see ... "If you want a special distribution, you can implement your own event dispatcher ..."
Just to be clear, you mean, you could NOT use the usual OnCollisionEnter, OnGui etc etc .. you'd have to, indeed, use your own replacements?
$$anonymous$$akes sense ...
Well in most cases you could implement your own handling / distrubution of your events. Unfortunately in case of the touch --> mouse conversion there's not much you can do against it. Unity generates "mouse events" out of the touch information behind the scenes. They wanted to make the development on mobile "simpler" but it's less flexible that way. For the OnGUI stuff it would be great when we had a CreateEvent() function that allows us to simulate mouse or keyboard events. Of course that's only helpful when you can disable the automatic mouse-event generation.
I see - thanks again.
the very bottom line here is "you can't do it" and just work with it like it is!
Suits me fine ! Thanks
Unity is really great, but it's always the same: When trying to make it comfortable and easy you propably sacrifice flexibility. If Unity provides both, very low-level-control and high-level-comfort it would be a mess ;)
Bunn, I want it both ways ! :)
This question has arisen again ...
http://answers.unity3d.com/questions/325034/blocking-click-through-on-gui-components.html
Your answer
