- Home /
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:: olliejones.com/GUIHelp.mov
Answer by denewbie · Nov 23, 2010 at 07:27 AM
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;
}
Okay, I gave this ago and all the button does is jump around the screen every time I click on it.
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. =)
Okay, its looking good - but how would I transform that button into an object/model? - As if I'm dragging something from my inventory?
I've added a second portion to show how you can get the effect.
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.
Answer by beco13 · Jun 24, 2011 at 05:02 AM
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 :)