Draggable components

Hello,

How can I make draggable components ? How can I drag a button (for example) with the mouse ?

You can use GUI.DragWindow to move GUI components around.

Yes, but I’d like to drag other components (GUIButton or GUITexture).

Not to long ago i used a draggable window, then colored it clear so you cant see it. Then make a button or label box ect where the window edges are just outside the component so it can be dragged. Worked like a charm.

Just incase you dont know how to color a GUI element
Here’s an example :smile:

GUI.backgroundColor = Color.clear;
mW000=GUI.Window(window00_ID,mW000,DI000,pageT);
backgroundColor = null;

To move something around while mouse down i guess you could use something like this.

Input.mousePosition.x
Input.mousePosition.y

Placed in the proper parameters of an element. :smile:

Good idea, but the button inside the window is not displayed (due to the instruction GUI.backgroundColor = Color.clear; )

you can set the color to something new again later on to change the state for following draw ops

OK, by using this code, it works nice :

private var _rect : Rect;

private function Awake( )
{
  _rect = new Rect(0, 0, 100, 100);
}

private function OnGUI( )
{
  GUI.backgroundColor = Color.clear;
  _rect = GUI.Window( 0, _rect , WindowFunction, "" );
}

private function WindowFunction( id : int ) 
{
  GUI.backgroundColor = Color.white;
  GUI.DragWindow( Rect( 0,0, 100, 100 ) );
  
  GUI.Button( Rect( 0, 0, 100, 100 ), "drag me");
}

Thanks :slight_smile:

Unfortunately, I can’t use these components (GUI.Window) in a scrollView. I have to find another solution. :frowning:

You are going to have to jack into the event functions of the upper level GUI objects themselves. It’s as simple as detecting a mouse event, and then Use()ing it so that it doesn’t translate to the actual GUI itself.

You are going to need to detect the location of the mouse, and determine if it is inside of the button you want to drag, and then change the location of the button based on where the mouse is pointed after a mouseDown event has been initiated.

Where can I find a sample using the events of GUI elements ? I never used them.

Actually, you aren’t going to be using GUIElements’ events at all, since GUI elements are dynamically drawn each frame, instead of being actual elements within a heirarchy themselves.

Events can be tested for within OnGUI(). Look into the Event object in the reference. It’s all really self-explanatory.