Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by michael 4 · Sep 09, 2011 at 04:25 PM · mouseevent

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image japanitrat · Sep 09, 2011 at 08:59 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Adam-Buckner · Sep 09, 2011 at 09:59 PM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image michael 4 · Sep 13, 2011 at 02:19 PM 0
Share

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.

avatar image Adam-Buckner ♦♦ · Sep 13, 2011 at 02:49 PM 0
Share

$$anonymous$$ichael: Are you thinking about Layer $$anonymous$$asks? 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: layer$$anonymous$$ask A Layer mask that is used to selectively ignore colliders when casting a ray.

avatar image michael 4 · Sep 13, 2011 at 05:38 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Get button under mouse 1 Answer

GUILayout Window consume mouse events 1 Answer

Main window mouse event 2 Answers

OnMouseEnter() over environment GameObject during the game 1 Answer

Difference between OnPointerEnter and OnMouseEnter 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges