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
1
Question by JPOnion · Sep 05, 2014 at 02:11 AM · canvas4.6

How to get mouse location on world canvas? [Unity 4.6]

I'm trying to build an inventory menu system using a canvas in world space. I'm trying to add drag-and-drop functionality, which requires me to know where the mouse is on the canvas so I know where to draw the item icon.

Does anybody know how to get that value?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer Wiki

Answer by JPOnion · Sep 06, 2014 at 01:10 AM

I found the answer. There's an great set of examples on the new GUI at:

http://forum.unity3d.com/threads/ui-example-project.263418/

One of the examples is on how to setup a drag-and-drop system. The canvas in the example isn't in world space, but it does work in world space.

Specific to my project, I did the following:

  1. In the class that was going to be dragged, I added:

    using UnityEngine.EventSystems;

    and inherited the following interfaces:

    IBeginDragHandler, IDragHandler, IEndDragHandler

    Each interface only has one function:

OnBeginDrag: I created an icon and set its parent to the Canvas.

OnEndDrag: I destroy the icon.

OnDrag: I call this function (this came from one of the examples linked to earlier):

 <pre><code>private void SetDraggedPosition(PointerEventData data)
     {
         if (dragOnSurfaces && data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null)
             m_DraggingPlane = data.pointerEnter.transform as RectTransform;
         var rt = m_DraggingIcon.GetComponent{RectTransform}();
         Vector3 globalMousePos;
         if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, data.position, data.pressEventCamera, out globalMousePos))
         {
             rt.position = globalMousePos;
             rt.rotation = m_DraggingPlane.rotation;
         }
     }

EDIT: I can't seem to get the code formatting correct, so replace { and } with the correct brackets...

  1. In the class that will receive the dragged item, I inherited the interface:

    IDropHandler

    This interface also only has one function:

OnDrop: The variable data.pointerDrag contains the GameObject that was being dragged. You can deal with the dropping logic how you want.

The SetDraggedPosition function from the example was what I was specifically looking for, but I hope the rest of the information is helpful, too.

Comment
Add comment · Show 2 · 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 Culzean · Jun 18, 2015 at 09:34 PM 0
Share

Thanks, answer saved me from quite a bit of grief working with world space canvas for first time.

avatar image sanjayMer · Oct 09, 2016 at 09:41 AM 0
Share

good, it's really good code and save my lost of time thanks

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

On Click paramaters disappear from button prefab? 5 Answers

[4.6 - UI] How to add 3d objects as canvas child? 4 Answers

[4.6 - UI] Accessing Text in Image inside Canvas via C# Script 3 Answers

Unity 4.6 adding a Canvas Text onto a gameobject prefab?? -1 Answers

(4.6 UI) Canvas position resetting itself to (0,0,0) when instantiating a new prefab with the canvas as child? 2 Answers


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