x


Using mouse events to control GUI

I have an issue when I click on gui buttons, anything under the gui gets affected to. Ive heard that using mouse events will eliminate this problem so that when a button is being pressed, it doesnt affect anything else. After reading the documentation, however, i still dont understand how to use them.

Any help? thanks

more ▼

asked Sep 09 '11 at 04:25 PM

michael 4 gravatar image

michael 4
312 45 52 56

what do you mean by "everything else is affected"? do you mean other GUI elements?

Sep 09 '11 at 08:59 PM japanitrat
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

In general, I find that one has to approach a Unity UI using OnGUI in one of two ways. Either one needs to use what I like to think of as the "built in" functionality like using GUI.Button (if (GUI.Buttton....) then {}, or one needs to use Events. I have not very successfully used the two together in any meaningful sense.

Events are under-documented and in some cases not documented at all. I believe, as of this time of posting, you can follow a trail of pointers to event documentation and find out in the end it was all a wild goose chase leading to nothing (iirc, the last step points to the UI Tutorial, which mentions it in is ToC, but has no content...).

You will have to learn Events thru the specific documentation, and by asking specific questions on the forum, IRC and here.

A typical use of Events would be something like this:

void OnGUI () {
    Event currentEvent = Event.current;

    if (isDragging && !doneDragging) {
       //    Draw the icon when dragging.
       GUI.DrawTexture (new Rect(currentEvent.mousePosition.x - (iconWidthHeight/2), currentEvent.mousePosition.y - (iconWidthHeight/2), iconWidthHeight, iconWidthHeight), dragItem.draggedItem.itemIcon);
       //    Detect non-window clicks
       switch (currentEvent.button) {
         case 0:
          switch (currentEvent.type) {
              case EventType.MouseDown:
                 DestroyDraggedItem (currentEvent);
                 currentEvent.Use ();
              break;
          }
         break;
         case 1:
          switch (currentEvent.type) {
              case EventType.MouseUp:
                 if (!openApprovalWindow) {
                   CancelDrag ();
                 }
              break;
          }
         break;
       }
    }

If you look at how this code is working out, you need to consider what EVENTS are happening, where they are happening and what they are doing. It is both more precise and more complex than relying on GUI.x ...

If you look through this code, you will also see the use of currentEvent.Use(). This does "use" the event and nothing else further down the chain can use it.

more ▼

answered Sep 09 '11 at 09:59 PM

Little Angel gravatar image

Little Angel ♦♦
453 2 4 11

I see, so mouse events isnt what I was looking for either, I was hoping using mouse events, I could have my objects under the gui not be selectable. (Raycast goes through gui). But this was very good information anyhow.

Sep 13 '11 at 02:19 PM michael 4

Michael: Are you thinking about Layer Masks? http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

If you look at the reference, you can set a layer mask to contain your ray: layerMask A Layer mask that is used to selectively ignore colliders when casting a ray.

Sep 13 '11 at 02:49 PM Little Angel ♦♦

Well my problem is that the code that selects my objects runs through the gui. So if there is a gui button for example over top of a selectable object, the gui button gets pressed and the object gets selected. A fellow programmer told me to look into mouse events, which was why i asked my original question.

Sep 13 '11 at 05:38 PM michael 4
(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:

x1001
x171

asked: Sep 09 '11 at 04:25 PM

Seen: 2351 times

Last Updated: Sep 13 '11 at 05:38 PM