Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by Ozer_Jigme · Nov 11, 2017 at 02:04 PM · unity 5uidrag-and-dropright click

Convert UI Drag script for right mouse click?

Hey, I'm using a great script I found here to drag and drop UI elements and restrict their positions on screen. My question is: how can I make it so this responds to the right mouse click only?

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class DragableUI : UIBehaviour, IBeginDragHandler, IDragHandler
 {
     /// <summary>
     /// The RectTransform that we are able to drag around.
     /// if null: the transform this Component is attatched to is used.
     /// </summary>
     public RectTransform dragObject;
 
     /// <summary>
     /// The area in which we are able to move the dragObject around.
     /// if null: canvas is used
     /// </summary>
     public RectTransform dragArea;
 
     private Vector2 originalLocalPointerPosition;
     private Vector3 originalPanelLocalPosition;
 
     private RectTransform dragObjectInternal
     {
         get
         {
             if (dragObject == null)
                 return (transform as RectTransform);
             else
                 return dragObject;
         }
     }
 
     private RectTransform dragAreaInternal
     {
         get
         {
             if (dragArea == null)
             {
                 RectTransform canvas = transform as RectTransform;
                 while (canvas.parent != null && canvas.parent is RectTransform)
                 {
                     canvas = canvas.parent as RectTransform;
                 }
                 return canvas;
             }
             else
                 return dragArea;
         }
     }
 
     public void OnBeginDrag(PointerEventData data)
     {
         originalPanelLocalPosition = dragObjectInternal.localPosition;
         RectTransformUtility.ScreenPointToLocalPointInRectangle(dragAreaInternal, data.position, data.pressEventCamera, out originalLocalPointerPosition);
     }
 
     public void OnDrag(PointerEventData data)
     {
         Vector2 localPointerPosition;
         if (RectTransformUtility.ScreenPointToLocalPointInRectangle(dragAreaInternal, data.position, data.pressEventCamera, out localPointerPosition))
         {
             Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
             dragObjectInternal.localPosition = originalPanelLocalPosition + offsetToOriginal;
         }
 
         ClampToArea();
     }
 
     // Clamp panel to dragArea
     private void ClampToArea()
     {
         Vector3 pos = dragObjectInternal.localPosition;
 
         Vector3 minPosition = dragAreaInternal.rect.min - dragObjectInternal.rect.min;
         Vector3 maxPosition = dragAreaInternal.rect.max - dragObjectInternal.rect.max;
 
         pos.x = Mathf.Clamp(dragObjectInternal.localPosition.x, minPosition.x, maxPosition.x);
         pos.y = Mathf.Clamp(dragObjectInternal.localPosition.y, minPosition.y, maxPosition.y);
 
         dragObjectInternal.localPosition = pos;
     }
 }

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
1

Answer by troien · Nov 24, 2017 at 10:03 AM

Noticed you comment in my other answer (a little late). Sinse this is probably the better place for an answer to this I'll place it here. You can use PointerEventData.button for that.

Unity already gives you the PointerEventData in the OnBeginDrag and OnDrag events, so you can simply check those.

The 2 methods I'm talking about should then be changed to something like this:

     public void OnBeginDrag(PointerEventData data)
     {
         if (data.button == PointerEventData.InputButton.Right)
         {
             originalPanelLocalPosition = dragObjectInternal.localPosition;
             RectTransformUtility.ScreenPointToLocalPointInRectangle(dragAreaInternal, data.position, data.pressEventCamera, out originalLocalPointerPosition);
         }
     }
 
     public void OnDrag(PointerEventData data)
     {
         if (data.button == PointerEventData.InputButton.Right)
         {
             Vector2 localPointerPosition;
             if (RectTransformUtility.ScreenPointToLocalPointInRectangle(dragAreaInternal, data.position, data.pressEventCamera, out localPointerPosition))
             {
                 Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
                 dragObjectInternal.localPosition = originalPanelLocalPosition + offsetToOriginal;
             }
 
             ClampToArea();
         }
     }


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

232 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does the Unity Scroll View cannot be scrolled horizontally using the finger instead of using the scroll bar 0 Answers

How to create a back button? 0 Answers

Changing Music On Icon To Off and saving the preference 2 Answers

IPointerExitHandler while holding click 0 Answers

Level Selector - Level Images for Brick Breaker 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