Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 oliver-jones · Nov 23, 2010 at 03:59 AM · guiselectiondrag-and-droponscenegui

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

Comment
Add comment
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

2 Replies

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

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;

}

Comment
Add comment · Show 7 · 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 oliver-jones · Nov 23, 2010 at 01:43 PM 0
Share

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

avatar image denewbie · Nov 23, 2010 at 03:33 PM 0
Share

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. =)

avatar image oliver-jones · Nov 23, 2010 at 05:12 PM 0
Share

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

avatar image denewbie · Nov 24, 2010 at 10:23 AM 0
Share

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

avatar image denewbie · Nov 24, 2010 at 10:27 AM 0
Share

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.

Show more comments
avatar image
0

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 :)

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

2 People are following this question.

avatar image avatar image

Related Questions

Custom Editor: How to get a reference to an existing object in the scene at the mouse position after drag-and-dropping a prefab into the scene? 1 Answer

C# converting Input.Mouseposition.y to to "top" argument of GUI.Box 1 Answer

Always show Custom Editor GUI 1 Answer

Working with the editor Selection 0 Answers

Mass Effect Select Menu 1 Answer


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