• 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
4
Question by unityprogrammer008 · Aug 06, 2010 at 06:24 PM · double-click

Gui Buttons Single and Double click

Hey guys im new to unity and programming, and am having difficulty making a gui button that will respond to single and double clicks with different functions for each type. Example: In an rts game when u single click on a units UI button it selects that unit, when u double click that UI button it moves the camera to the players position. If anyone can lead me to how to go about doing this it would b greatly appreciated!

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
7
Best Answer

Answer by spinaljack · Aug 06, 2010 at 06:58 PM

save a time stamp for the last click and then check the current click to see if it is less than a short amount of time. e.g.

var lastClick : float = 0;

if(GUI.Button(Rect(someRect),"double click button")){ if(Time.time-lastClick<0.3){ // do something }else{ // do something else } lastClick = Time.time; print(lastClick); }

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 unityprogrammer008 · Aug 06, 2010 at 08:50 PM 0
Share

thanks for the reply spinal ill give it a shot!

avatar image unityprogrammer008 · Aug 06, 2010 at 09:13 PM 0
Share

The code worked great spinal, however there is a problem, i have more than one button and if i click on one then the other it will call the second clicks gui.button script. This will b a problem for this game ><, also I am not aware of how many buttons there will b that need to use this feature so i tryed makin seperate lastclick variables which worked but with the number of buttons that will b used unknown i cant use that method, keep in $$anonymous$$d i am completely new to program$$anonymous$$g so im still learnin how to do things efficently. Thanks for your time and help!

avatar image spinaljack · Aug 07, 2010 at 01:07 AM 0
Share

Just store more variables. You can store them in a variable array if you don't know how many buttons there are at run time.

avatar image unityprogrammer008 · Aug 07, 2010 at 07:15 PM 0
Share

thanks again spinal for ur help works great now =D

avatar image castor · Jan 17, 2013 at 11:58 PM 0
Share

But this means that if you double click you get a single + double click. Is there a way to just get either the double or single click?

avatar image
-1

Answer by cncguy · Aug 07, 2010 at 09:12 AM

Do just what spinal Jack said but also save a reference to the rectangle used as the Rect() in drawButton. Then on the second click check the time and also check to see if the saved rect contains the mouseposition. If it does then it's a double click, if it's not then it's a different button click.

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
avatar image
-1

Answer by problogger · Feb 03, 2011 at 06:46 AM

heya.....i want a code such that i when i click a button.it automatically clicks all other specified buttons...................regards http://aweeklyriddle.blogspot.com/

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Issue with in-game double clicking? 2 Answers

Double-click MonoDevelop 2.4 , it does not Run 4 Answers

Double click detection 2 Answers

Double click too fast 0 Answers

How to detect double-clicked button in editor window 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