x


How To Drag Something From GUI - To Scene - Help

Hello,

I was wondering if there is a way to place an image on a GUI of, say a sphere. And when the user clicks on the 'GUI Sphere' and then drags it onto the scene, an object sphere will follow the mouse until the user places it where ever?

Is this possible? Is it a lot more harder than it sounds?

About My Scene:

I have a camera looking downwards to the scene, with a little GUI window to the left (where the objects/images will be placed.

Would appreciate any help. Thanks

VIDEO:: http://olliejones.com/GUIHelp.mov

more ▼

asked Nov 23 '10 at 03:59 AM

oliver-jones gravatar image

oliver-jones
2.5k 206 226 254

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Heres the full script:

var myLeft:float = 0;
var myTop:float = 0;
var myWidth:float = 200;
var myHeight:float = 50;

var isDragging:boolean = false;

function OnGUI()
{
    if (     GUI.RepeatButton( Rect(myLeft, myTop, myWidth, myHeight)  , "Moving Button" )   )
        isDragging = true;

    if (isDragging)
    {
        myLeft = Input.mousePosition.x - myWidth*0.5;
        myTop = Screen.height - (Input.mousePosition.y + myHeight*0.5);
    }
}

function Update(){

    if (    Input.GetMouseButtonUp(0)   )
        isDragging = false;

}

Just replace the words in the button with a texture of your object so it looks like you're dragging stuff.

var myLeft:float = 0;
var myTop:float = 0;
var myWidth:float = 200;
var myHeight:float = 50;

var isDragging:boolean = false;

// Add this line
var myInventoryImg:Texture2D;

function OnGUI()
{
    // if (     GUI.RepeatButton( Rect(myLeft, myTop, myWidth, myHeight)  , "Moving Button" )   )

      // Replace with this
      if (     GUI.RepeatButton( Rect(myLeft, myTop, myWidth, myHeight)  , myInventoryImg )   )
        isDragging = true;

    if (isDragging)
    {
        myLeft = Input.mousePosition.x - myWidth*0.5;
        myTop = Screen.height - (Input.mousePosition.y + myHeight*0.5);
    }
}

function Update(){

    if (    Input.GetMouseButtonUp(0)   )
        isDragging = false;

}
more ▼

answered Nov 23 '10 at 07:27 AM

denewbie gravatar image

denewbie
717 3 3 17

Okay, I gave this ago and all the button does is jump around the screen every time I click on it.

Nov 23 '10 at 01:43 PM oliver-jones

Sorry bout that there was an error on the y coordinates. I've re written and tested this full script. It'll work fine now. =)

Nov 23 '10 at 03:33 PM denewbie

Okay, its looking good - but how would I transform that button into an object/model? - As if I'm dragging something from my inventory?

Nov 23 '10 at 05:12 PM oliver-jones

I've added a second portion to show how you can get the effect.

Nov 24 '10 at 10:23 AM denewbie

Texture2D can hold any image in psd, png, gif, jpg etc. But if you want to have transparency you'll need to use psd, gif or png only.

Nov 24 '10 at 10:27 AM denewbie
(comments are locked)
10|3000 characters needed characters left

Thx for denewbie...it really works :)

BTW 4 oliver jones if you haven't get it done ( I guess you'd already figure it ou), maybe you'll want to use custom skin to change the button images :)

more ▼

answered Jun 24 '11 at 05:02 AM

beco13 gravatar image

beco13
26 11 12 14

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3696
x130
x55
x15

asked: Nov 23 '10 at 03:59 AM

Seen: 1751 times

Last Updated: Jun 24 '11 at 05:02 AM