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 Alanimator · Nov 18, 2012 at 04:13 PM · draggui.button

How do i drag a GUI.Button ?

how do i drag a GUI button ?

yeah its a noob question but seeing as i have never used any drag function....lol

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 Meltdown · Nov 18, 2012 at 04:19 PM 1
Share

Don't even bother with Unity's built in GUI. Download NGUI or another 3rd party GUI from the asset store.. you'll save yourself heaps of headaches. Trust me. After 5 game projects, I know :)

avatar image Alanimator · Nov 18, 2012 at 04:41 PM 1
Share

lol i always thought the built in GUI in unity lacked flexibility. I will try NGUI in my next project .

4 Replies

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

Answer by dpk · Nov 18, 2012 at 10:36 PM

I won't pretend this is the cleanest way to do this, but it works. Add some clamping to the values and you'll have something solid.

 public class UserInterface : MonoBehaviour {
     public Rect playerPositionRect = new Rect(0, 151, 200, 50);
     private Vector2 currentDrag = new Vector2();
 
     void Start () {
     
     }
     
     void Update () {
     
     }
     
     void OnGUI(){
         GUI.Box (playerPositionRect, "Pos: " + playerPosition);
         
         Vector2 screenMousePosition = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
         if (currentDrag.sqrMagnitude != 0 || playerPositionRect.Contains(screenMousePosition)) {
             if (Input.GetMouseButtonDown(0)) {
                 currentDrag = screenMousePosition;
             } else if (Input.GetMouseButton(0)) {
                 playerPositionRect.x += (screenMousePosition.x - currentDrag.x);
                 playerPositionRect.y += (screenMousePosition.y - currentDrag.y);
                 
                 currentDrag = screenMousePosition;
             } else {
                 currentDrag.x = 0;
                 currentDrag.y = 0;
             }
         }
     }
 }
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 whydoidoit · Nov 19, 2012 at 12:07 AM 0
Share

You shouldn't be using Input in OnGUI - you should be handling events for mouse movements. OnGUI is called multiple times per frame and who knows what it's doing to the Input - but it isn't always right :)

Event.current contains the current mouse position when it is a mouse event.

avatar image dpk · Nov 19, 2012 at 01:04 AM 0
Share

Thanks for the tip. I've moved the code to Update (in my local copy) and it works fine there. I'm not sure what you mean by Event.current, where would that go?

avatar image
7

Answer by SONB · Nov 19, 2012 at 02:03 AM

That's what whydoidoit meant:

public class TestClass : MonoBehaviour { Rect buttonRect = new Rect(10, 10, 100, 20); bool buttonPressed = false;

 void OnGUI()
 {
     if(buttonRect.Contains(Event.current.mousePosition))
     {
         if(Event.current.type == EventType.MouseDown)
         {
             buttonPressed = true;
         }

         if(Event.current.type == EventType.MouseUp)
         {
             buttonPressed = false;
         }
     }

     if(buttonPressed && Event.current.type == EventType.MouseDrag)
     {
         buttonRect.x += Event.current.delta.x;
         buttonRect.y += Event.current.delta.y;
     }

     GUI.Button(buttonRect, "Draggable Button");
 }

}

It checks if you pressed the mouse button while the cursor is over the GUI button, and then, if it's pressed and dragged, it applies movement delta of the mouse to the button's Rect. Here you can only drag the button if you previously pressed it. This way you can create your own GUI controls and their behaviour. Hope it helps.

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 dpk · Nov 21, 2012 at 02:09 AM 0
Share

So, if I understand correctly, this means that it's O$$anonymous$$ to use Event.current.mousePosition inside OnGUI, but not O$$anonymous$$ to use Input.Get$$anonymous$$ouse*.

avatar image SONB · Nov 21, 2012 at 02:21 AM 0
Share

Exactly. Input for Update(), Event for OnGUI(). Ok, Events can be used in any other situation as well, but it's better to use Input only in Update().

avatar image Eric5h5 · Nov 21, 2012 at 02:25 AM 0
Share

Events can only be used in OnGUI.

avatar image SONB · Nov 21, 2012 at 02:32 AM 0
Share

Ahh, you're right! I always missed the first sentence in the Event script reference: "A UnityGUI event." Learned something new today :D

avatar image browne11 · Feb 25, 2016 at 12:11 AM 0
Share

Thanks for your answer. This should be marked as more correct. :)

avatar image
0

Answer by nicloay · Nov 21, 2012 at 06:46 AM

AngryAnt created a very nice tutorial about drag'n'drop, http://angryant.com/2009/09/18/gui-drag-drop/ He has in that example how to make dragging in ediotor as well as in the scene.

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
0

Answer by Geo.Ego · Feb 11, 2014 at 03:22 PM

If you're willing to use NGUI, I made a blog post outlining how to do that here. It can be done in a matter of moments with no coding required.

Basically, you add a Collider to the element you want the user to click to drag, add the UIDragObject script on to that object, and drag the object you want to move around to the UIDragoObject's "Target" parameter.

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

17 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

Related Questions

I want to set mechanics like in this game 'Sand Ball' where you swipe on sand and sand is removed from swiping area? 1 Answer

How can i implement a scroll speed for a scroll view/rect so that my content travels slower when I drag? 1 Answer

How to change the game-window style? 0 Answers

Touch on GUI.Button HELP!!!! 3 Answers

Change GUILayout.Button fontsize 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