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;
}
}
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();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612142710im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
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