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 Brizz104 · Mar 07, 2013 at 11:48 PM · buttonclickmousedragrepeatbutton

RepeatButton AND Button at the same time

What I need to be able to do is to click-drag using a repeat button, but also just click to initiate movement. Think of the status bar on android phones (the bar at the very top). You drag it down to move it. But i also want to just tap it to have it open/drag down on its own. I have the dragging code all working fine, and the clicking code working fine, I just don't know a way to have both be active at the same time. I believe a Button Up function would fix this, because if you're dragging you aren't Up, but if you click you are. I haven't found anything about GUI.Button Up and even if I did, there's still the problem of only triggering the button that's on "top", the button under it still wouldn't trigger an up function. Any help is appreciated. If there is a snippet of code that just tells Unity to push all buttons the mouse is on or to ignore button priority that would rock. Thanks for the help!

The reason I'm using a repeat button is so that you can't just drag anywhere on the screen to use it. You have to be on the graphic to pull it, and since a GUI.Button only triggers when "clicked", and dragging keeps the mouse held down, I can't just lump the two into one button. Obviously if there is alternative to using RepeatButton this would clear up my issue, and would be appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class Drawer : MonoBehaviour
 {
     public GUISkin skin;
     
     private float drawerPosX = 1232;
     private float drawerBGPosX = 1280;
     private float drawerDragZone = 100;
     private float mousePosX;
     private bool drawerRight = false;
     private bool drawerDockRight = false;
     private bool drawerDockLeft = false;
     public bool drawerDrag = false;
         
     Vector2 StartPos;
     int SwipeID = -1;
     float minMovement = 20.0f;
     
     void Start ()
     {
     
     }
     
     void OnGUI()
     {
         GUI.skin = skin;
         
         GUI.Box(new Rect(drawerPosX, 0, 48, 800), "", "Other");    
         GUI.Box(new Rect(drawerBGPosX, 0, 1280, 800), "", "DrawerBG");
         
         if(GUI.RepeatButton(new Rect((drawerPosX - 50), 0, drawerDragZone, 800), "", "ArrowUPHead"))
         {
             drawerDrag = true;            
             
             
         }
         
         if(GUI.Button(new Rect((drawerPosX - 50), 0, drawerDragZone, 800), "", "ArrowUPHead"))
         {
             if(drawerPosX <= 640)
             {
                 drawerDockRight = true;    
                 drawerDockLeft = false;
             }
             
             else if(drawerPosX >= 1232)
             {
                 drawerDockLeft = true;    
                 drawerDockRight = false;
             }
         }
     }
     
     void Update ()
     {
         if(drawerPosX <= 640)
         {
             if(Input.GetMouseButtonUp(0))
             {
                 drawerRight = true;
             }
             
             if(drawerRight == true)
             {                
                 drawerPosX += 20;
                 drawerBGPosX += 20;
                 
                 if(drawerPosX >= 640)
                 {
                     drawerPosX = 640;
                     drawerBGPosX = 688;
                 }    
             }
             else
             {
                 
             }
         }
         
         if(drawerDrag == true)
         {
             mousePosX = Input.mousePosition.x;
             drawerPosX = mousePosX;
             drawerBGPosX = mousePosX + 48;    
         }
         
         if(Input.GetMouseButtonUp(0) && drawerDrag == true)
         {
             drawerDrag = false;    
         }
         
         if(drawerDockRight == true)
         {
             drawerPosX += 20;
             drawerBGPosX += 20;
             
             if(drawerPosX >= 1232)
             {
                 drawerPosX = 1232;
                 drawerBGPosX = 1280;
             }
         }
         
         if(drawerDockLeft == true)
         {
             drawerPosX -= 20;
             drawerBGPosX -= 20;
             
             if(drawerPosX <= 640)
             {
                 drawerPosX = 640;
                 drawerBGPosX = 688;
             }
         }
     }
 }
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

1 Reply

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

Answer by robertbu · Mar 08, 2013 at 07:39 AM

GUI.Button() calls execute on the mouse up, so you can set a timer when the mouse goes down and check the duration to see if it is a click or something longer. Simple test script:

 using UnityEngine;
 using System.Collections;
 
 public class GUIClick : MonoBehaviour {
     
     float clickThreshold = 0.25f;
     float clickTime = 0.0f;
     
     void Update() {
         if (Input.GetMouseButtonDown(0))
             clickTime = Time.time;
     }
 
     void OnGUI() {
         if (GUI.Button(new Rect(200,200,200,100),"Click Here")) {
             if (clickTime + clickThreshold >= Time.time)
                 Debug.Log ("I was clicked");
         }
 
     }
 }
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

10 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

Related Questions

GUI Repeatbutton up? 1 Answer

Unity UI Button not working 1 Answer

OnMouseDown() Doesn't work 17 Answers

Stop clicking through a GUI window 2 Answers

Detect Swipe and GUI Button Press 1 Answer


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