- Home /
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
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 :)
lol i always thought the built in GUI in unity lacked flexibility. I will try NGUI in my next project .
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;
}
}
}
}
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.
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?
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.
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*.
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().
Ahh, you're right! I always missed the first sentence in the Event script reference: "A UnityGUI event." Learned something new today :D
Thanks for your answer. This should be marked as more correct. :)
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.
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.
Your answer
Follow this Question
Related Questions
How to change the game-window style? 0 Answers
Touch on GUI.Button HELP!!!! 3 Answers
Change GUILayout.Button fontsize 0 Answers