Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jixi · Jul 15, 2012 at 02:33 PM · guimouseclickfocusgui.window

Clickable GUI.Window

Alright so what i'm trying to do is make GUI.Window components that do something when clicked, but i have no idea how to make it work.

Here's what i got so far:

 GUI.WindowFunction winFunction;
 Rect r = new Rect(30, 30, 300, 300);
 
 void Start(){
     winFunction = windowFunction;
 }
 
 void OnGUI(){
     GUI.Window(0, r, winFunction, "test");
 }
 
 void windowFunction(int windowID){
     //One of the things i've tried, doesn't work for some reason.
     if(Input.GetMouseButtonDown(0) && r.Contains(Input.mousePosition))
     {
         print("doesnt work");
     }
 }


Any ideas how to make it work? besides perhaps using reflection.

Comment
Add comment · Show 2
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 Bunny83 · Jul 15, 2012 at 04:24 PM 0
Share

btw, is there a reason why you use an explicit delegate variable? Usually you just pass the callback directly to the Window function.

avatar image Jixi · Jul 15, 2012 at 05:58 PM 0
Share

No reason, i just threw this together and well it's kind of a habit that stuck with me over the years.

1 Reply

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

Answer by Bunny83 · Jul 15, 2012 at 04:18 PM

The input class is ment for gameinput and shouldn't be used in OnGUI. Use the Event class in OnGUI:

 void windowFunction(int windowID)
 {
     Event e = Event.current;
     if(e.type = EventType.MouseDown && e.button == 0 && r.Contains(e.mousePosition))
     {
         print("should work");
     }
 }

OnGUI is called for many different events. The GUI functions like Button or Label handle the event internally. When you do anything "manually" in OnGUI, keep in mind to pick the right event or you would execute your code for all events.

edit
Keep in mind that all events processed inside a window callback have mouse coordinates relative to the window origin.

Comment
Add comment · Show 7 · 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 Jixi · Jul 15, 2012 at 05:53 PM 0
Share

I see, thanks that works fine!

avatar image Triglav · Sep 08, 2013 at 01:17 PM 0
Share

No, it doesn't.

Working on Unity 4.2.0f4, more than a year later :), apologies for necro. I tried what you both suggested above:

$$anonymous$$entioned option 1) Insert the EventType... condition inside window specific function when window is created (wrapped in OnGUI()).

Not mentioned option 2) Insert the EventType... condition inside OnGUI() function but outside window creation function and read them for mouseDown/up and for buttons.

Well neither of them works. Clicking inside any created windows and debugging Events shows the only types of Events are "used", "repaint" and "layout" and those are known. "Contains()" works in all cases fine and mouse buttons register correctly (0,1,2,3,..), but what they DO (so down or up) does not register. However if I click outside created windows, the types register correctly for all mouse clicks.

So if there's a way to get mouseDown/up/click or other eventtypes(keyboard?) while inside a window, please help.

avatar image Triglav · Sep 08, 2013 at 02:21 PM 0
Share

Sry for spam, unsure whether I should create new comment or edit last one. I'm sure some1 will tell me :)

Anyway, I just noticed that in the above code, when checking Event.current.type from inside window creation function and starting mouse drag from outside window and finishing inside window, you get $$anonymous$$ouseDrag Events and $$anonymous$$ouseUp events correctly. However, just to repeat the problem, when starting inside the window, these events don't register correctly.

avatar image Bunny83 · Sep 08, 2013 at 03:54 PM 1
Share

An event is only valid until a control uses it. If a control (like a button) handles a $$anonymous$$ouseDown and the event happened in the area of the button, the Button will call Event.current.Use() which will "eat" the event by overriding it with the "used" event.

The GUI is an immediate mode GUI system. The first control in OnGUI is the first who has the change to process the event.

Windows are handled a little bit differently. The layout events are passed in the usual order (which will happen before any other event) but $$anonymous$$ouse and keyboard events are sent to the active window first, even before the OnGUI function that created the window.

Once an event has been eaten it can't be processed again. All data in the Event will be "erased".

From your comment we can't deter$$anonymous$$e what you actually want to do. I suggest you post your own question since your problem is clearly not related to this question. You either have "active" gui elements in your window or you do something else wrong. Without seeing your code this is a dead-end.

avatar image Bunny83 · Sep 08, 2013 at 09:46 PM 1
Share

As i said if an even is processed and eated by a control the event will be set to "used". You have a GUI.DragWindow without Drag area so it will use the whole window area. DragWindow will eat the $$anonymous$$ouseDown event since it will process it (it starts dragging the window). Same for $$anonymous$$ouseDrag events.

If you want your print statement to show something useful, swap the two lines:

     print(Event.current.type.ToString()); 
     GUI.DragWindow();
Show more comments

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I focus on mouse control without having to click? 0 Answers

Detecting a GUI element under the mouse? 1 Answer

Clicks from a track pad works, but not from a mouse 0 Answers

First UI click in a scene doesnt register 1 Answer

Disable Selection on Left button Mouse Click 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