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 Alex-Chouls · Nov 23, 2010 at 12:32 AM · editordouble-click

How to detect double-clicked button in editor window

Having a devil of a time coming up with a method that works on mac and windows and detects a double click only once.

Here's what I've tried:

void OnGUI()
{
    if (GUILayout.Button("DoubleClickMe"))
    {
        if (Event.current.clickCount == 2)
            Debug.Log("DoubleClick");
    }
}

This approach makes perfect sense, and works on Mac. Doesn't work on PC.

So I tried this:

void OnGUI() { if (GUILayout.Button("DoubleClickMe")) { }

 if (Event.current.type == EventType.Used &&
     Event.current.clickCount == 2)
 {
     Debug.Log("DoubleClick");
 }

}

Works great on PC, but returns 2 double clicks on Mac.

Tried a few other methods, but the Mac consistently detects double clicks twice. Except for the first method, which doesn't work on PC!

If I can't get clickCount to work consistently on Mac and PC, I guess I'll try using EditorApplication.timeSinceStartup to manually detect 2 clicks close together. But I like that clickCount uses the users system double click settings - will feel right to the user...

Both Mac and PC are running 3.1.0f (54715)

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

3 Replies

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

Answer by Alex-Chouls · Nov 24, 2010 at 08:35 AM

Ended up doing my own test for double click. Guess I'll log a bug for the inconsistent behavior of clickCount across platforms...

The code I use in an EditorWindow:

double clickTime; double doubleClickTime = 0.3;

void OnGUI() { if (GUILayout.Button("DoubleClickMe")) { if ((EditorApplication.timeSinceStartup - clickTime) < doubleClickTime) DoDoubleClick();

     clickTime = EditorApplication.timeSinceStartup;
 }

}

Theoretically this could return false positives where you quickly click on 2 buttons close to each other, but I'm not worrying about that for now... You could track a button ID or something to make sure it's the same button clicked twice...

Comment
Add comment · Show 1 · 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 denewbie · Nov 24, 2010 at 10:16 AM 0
Share

I guess you deserve credit for taking initiative on your own =)

avatar image
0

Answer by denewbie · Nov 23, 2010 at 01:39 AM

Try this:

int clickCount = 0; float timeLapse = 0;

void OnGUI() { if (GUILayout.Button("DoubleClickMe")) { if (0 == clickCount) { timeLapse = 0; clickCount++; } else if (1 == clickCount) { if (timeLapse > 0.5f) // So with this 0.5f value the double click must be carried out within half a second timeLapse = 0; else clickCount++; } if (Event.current.clickCount == 2) { Debug.Log("DoubleClick"); // If you dont want it to detect only once then uncomment the next line.
// clickCount = 0; } }

}

void Update() {

 if (1 == clickCount)
     timeLapse += Time.deltaTime;

}

Haven't really checked the script for bugs yet but the idea is thereand it should work.

Comment
Add comment · Show 2 · 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 Alex-Chouls · Nov 24, 2010 at 08:25 AM 0
Share

Thanks! I ended up doing something like this, but because it's running in the editor I can't use Time. Ins$$anonymous$$d I used EditorApplication.timeSinceStartup. I'll post code in an answer...

avatar image denewbie · Nov 24, 2010 at 10:16 AM 0
Share

If you could find an answer on your own go for it man =). Nice of you to share it too.

avatar image
0

Answer by Waz · Feb 15, 2011 at 10:35 AM

In addition to the above answers, it seems that, at least on Windows, Unity does not reset the event.clickCount when the mouse moves even a large distance, so you may need to check that too, depending on the control you are using (I'm using a SelectionGrid, so I need it):

var ev = Event.current;
var wasclick = ev.type == EventType.MouseDown;
var wasused = ev.type == EventType.Used;
selected = GUILayout.SelectionGrid(selected , ...);
if (wasclick && !wasused && ev.type == EventType.Used) {
  if ((lastClickPos - ev.mousePosition).sqrMagnitude <= 5*5 && ev.clickCount > 1)
    return true;
  lastClickPos = ev.mousePosition; // lastClickPos is class/global variable
}
Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

It seems like a Event.current.clickCount problem with Unity 4 1 Answer

Whats causing my objects transform gizmos to not stay with it in the editor? 2 Answers

Track passing time in EditorScript 1 Answer

Double click too fast 0 Answers

Some GameObjects not visible in android build but work fine in the editor? 0 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