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
1
Question by DaiMangouDev · Mar 19, 2015 at 04:27 PM · c#guibutton

How do I register or read multiple MouseDown events ?

hey , I just creating a double click class which allows me to make anything clickable...

the code is written below . this is done just by checking i a mouse down event is triggered twice in under 0.25 seconds.

then i realised that while i am not able to "click" anything... it is not really clicking the way buttons handle clicks .

I am checking for a mouse up event over the GUI element , setting a float to be realtimeSinceStartup then checking for a mouse down event , check if realtimeSinceStartup - the previously saved realtimeSinceStartup is less than 0.25 thus completing the double click.

but no buttons in unities gui even registers a mouse up event .

this code does check for double clicks on GUI elements.. but not on GUI elements that already registers or read a MouseDown event somewhere in the unity API

  public class Click
     {
         static float time2;
         static bool result ;
         public static bool DoubleClick(Rect rect)
         {
             bool result = false;
             float time = Time.realtimeSinceStartup / 10.0f;
             if (rect.Contains(Event.current.mousePosition))
             {
                 if (Event.current.rawType == EventType.MouseUp)
                 {
                     time2 = time;
 
                 }
                 if (Event.current.rawType == EventType.MouseDown && time - time2 < 0.025f)
                 {
                     time2 = 0;
                     result = true;
                 }
             }
             return result;
         }
 
 
     }


Implementation example

    private Rect someRect =(new Rect(100,100,100,50));
 private Rect someOtherRect = new Rect(250,100,100,50)
 private Texture2D = (Texture2D) Resources.Load("someimage",typeof(Texture2D)); 
      void OnGUI()
      
      {
      GUI.Button(someRect,"double click me") ;
      if(Click.DoubleClick(someRect)) Debug.Log("double clicked")
 
 
 GUI.DrawTexture(someOtherRect ,image);
       if(Click.DoubleClick(someOtherRect )) Debug.Log("texture clicked")
      
      }

Comment
Add comment
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

2 Replies

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

Answer by Bunny83 · Mar 19, 2015 at 05:44 PM

The "old" GUI system handles events with the Event class. In your code you use some strange class called CurrentRawMouseEvent. If a control actually reacts to an event it "eats" that event so no other element can see it or react to it. Since you use GUI.Button, it will eat the MouseDown / MouseUp events. When an even got "eaten" Event.current.type will return "EventType.Ignore" instead of the actual event. You can use "Event.current.rawType" to get the actual event type.

ps: using hardcoded 0.025 seconds is not a good ides. First of all it's pretty short. Usually the delay is longer. Also 0.025 will physically only work with at least 40 frames per second, though you would need more (80+) to guarantee that it's working.

Comment
Add comment · Show 5 · 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 Bunny83 · Mar 19, 2015 at 05:47 PM 0
Share

Ahh, just saw that you divided your time by 10. So it's actually 0.25 and not 0.025 ^^

avatar image DaiMangouDev · Mar 19, 2015 at 05:51 PM 0
Share

oooh i understand what you mean in regards to the 0.025 seconds

I replaced CurrentRaw$$anonymous$$ouseEvent is what it was

     public static class Current$$anonymous$$ouseEvent
     {
         public static Event evt()
         {
             return (Event.current);
 
         }
 
 
     }
 
     public static class CurrentRaw$$anonymous$$ouseEvent
     {
         public static EventType evt()
         {
               return (Current$$anonymous$$ouseEvent.evt().rawType);
         }
     }
avatar image DaiMangouDev · Mar 19, 2015 at 06:02 PM 0
Share

umm does that mean that i no longer need to adjust the time dynamically to compensate for fps ?

avatar image DaiMangouDev · Mar 19, 2015 at 06:29 PM 0
Share

thumbs up for the help and info :)

i'm still not getting the double click to work on things like DrawTexture if the DrawTexture rect if some other function using $$anonymous$$ouseDown is accessing the Rect of the DrawTexture .

i'm using rawType but i'm still having trouble .

avatar image DaiMangouDev · Mar 25, 2015 at 08:37 PM 0
Share

after trying for a while , I guess it cant be done ... at least not easily .

avatar image
1

Answer by hav_ngs_ru · Mar 19, 2015 at 05:01 PM

why dont you just use OnMouseUp/OnMouseDown event to register click and check time since last click?

speaking about your code... the question is WHERE and WHEN do you call your DoubleClick.DoubleClick() function?

your register first (and all others) click ONLY if you called DoubleClick from OnGUI when current event is MouseUp/MouseDown... do you really call it every OnGUI? in other words - its better to see a caller code.

Comment
Add comment · Show 4 · 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 DaiMangouDev · Mar 19, 2015 at 05:17 PM 0
Share

this is a n implementation example

 private Rect someRect =(new Rect(100,100,100,50));
 void OnGUI()
 
 {
 GUI.Button(someRect,"double click me") ;
 if(Click.DoubleClick(someRect)) Debug.Log("double clicked")
 
 
 }
avatar image DaiMangouDev · Mar 19, 2015 at 05:26 PM 0
Share

and I had to rename the class in the example . the class name is now Click and not DoubleClick

avatar image DaiMangouDev · Mar 19, 2015 at 05:35 PM 0
Share

oh my I just kep messing up . sorry "CurrentRaw$$anonymous$$ouseEvent" is just Event.current.rawType

avatar image DaiMangouDev · Mar 19, 2015 at 06:18 PM 0
Share

thumbs up for the help :)

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

22 People are following this question.

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

Related Questions

How to repeat the same list of buttons? 1 Answer

Is there a way to use ScaleMode.Scaletofit on buttons? 0 Answers

C# Random Number on GUIButton click without updating every frame 0 Answers

C# GUI.Button highlighting and pressed effect 0 Answers

Key for GUI.Button 2 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