Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by oliver-jones · Nov 25, 2010 at 10:34 AM · guiprefabbuttondrag-and-dropmousedrag

How To Make GUI/Dragable Object - Att: Video

Hello,

Basically, Right now I have a very simple GUI with a button. What I would like to do is to have that button to be able to be dragged to the scene, in where it will turn into a game object that follows the mouse.

Watch This To See What I Mean: http://olliejones.com/GUIHelp.mov

Like an inventory, but the reversal. So instead of picking up things from scene. I want to be able to drop onto scene from GUI. Preferably with a grid snap feature such as the one shown in the video.

Please help, or point me in the right direction.

Thanks

Comment
Add comment · Show 4
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 · Dec 02, 2010 at 01:54 AM 0
Share
  • It doesn't have to be a button in the GUI - Later on, it will soon be a texture of some sort *

avatar image Peter G · Dec 02, 2010 at 02:06 AM 0
Share

Please stop reposting the same question.

avatar image oliver-jones · Dec 02, 2010 at 02:13 AM 1
Share

I haven't - I mealy edited it slightly and placed a bounty on it.

avatar image Peter G · Dec 02, 2010 at 03:29 AM 3
Share

I mean not this one, these: http://answers.unity3d.com/questions/28557/how-to-drag-something-from-gui-to-scene-help, http://answers.unity3d.com/questions/28591/drag-object-from-gui-into-scene,

3 Replies

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

Answer by Kourosh · Dec 06, 2010 at 06:28 PM

Oliver, instead of pasting code, I thought it would be more helpful to send you the scene I've created so that you would understand it better. So I've sent it to your email.

Comment
Add comment · Show 1 · 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 · Dec 06, 2010 at 06:51 PM 0
Share

Thanks $$anonymous$$ourosh - Works fine!

avatar image
5

Answer by _Petroz · Dec 02, 2010 at 09:05 AM

Break it down:

  1. Drag and store which item was dragged
  2. Detect where it is dropped
  3. Convert drop location from screen to world coordinates
  4. Snap the 3d position
  5. Create an object based on which item was dragged

Here are some tips:

setup: Use a GUITexture so you can use OnDrag, store a reference to the prefab you want to create inside that object.

  1. store the Prefab reference associated with the button inside OnDrag in a variable visible from (5.)
  2. Use Input.GetMouseButtonUp
  3. Use Camera.ScreenToWorldPoint
  4. Use the modulus operator for x and z coordinates
  5. Use Instantiate if your object is a prefab
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 · Dec 02, 2010 at 12:35 PM 0
Share

This sounds like a good approach - I've only been scripting for about a week and I'm poor at making up code. So far I have tried receiving the mouses position according to the scene and having a prefab follow it but no luck there. Also tried initiating a prefab where I click - but that didn't even work too

avatar image _Petroz · Dec 02, 2010 at 08:50 PM 0
Share

You can use print statements to check that code is reachable and also to test values. Have a look at the parts which are broken, and ask yourself 'is this code getting executed?' and 'is this value what i expect?', then use print statements to confirm.

avatar image _Petroz · Dec 03, 2010 at 07:44 AM 0
Share

3 up votes, surely that deserves the bounty. :)

avatar image oliver-jones · Dec 04, 2010 at 01:17 AM 0
Share

Yes, but it does not explain the scripts - I'm really bad at scripts - please help me!!

avatar image _Petroz · Dec 04, 2010 at 06:04 AM 0
Share

What I have given you should get you started. As requested I have "pointed you in the right direction". I don't mean any disrespect but we won't write your code for you. $$anonymous$$ake a start and ask specific questions if you get stuck on a certain step. Coding is not so hard, just give it a go and you'll be surprised how quickly you become quite good. :)

Show more comments
avatar image
0

Answer by Uriel_96 · Dec 02, 2010 at 02:19 AM

Im not very sure but this is experimentation that maybe will work

if ( Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100.0)){
hit.collider.transform.tag = "drag";
//here you put when you clic to the GUI
}
}else{
if ( Input.GetMouseButtonUp(0)){
if (Physics.Raycast (ray, hit, 100.0)){
hit.collider.transform.tag = "drop";
//Here you put when you clic and that construction its build
}
}
}

Recomendation: For this its not sure to put a GUI in the script, it will be better if you do it in the scene.

Comment
Add comment · Show 1 · 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 BerggreenDK · Dec 04, 2010 at 02:49 AM 2
Share

why has people voted this one down? please remember to explain when you dont agree.

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

No one has followed this question yet.

Related Questions

On Click paramaters disappear from button prefab? 5 Answers

Loading scene with a prefab GUI button? 1 Answer

GUI Creates a prefab? 1 Answer

GUI and buttons 3 Answers

Another GUI question. enable and disable 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