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.
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.