-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowDragger.cs
More file actions
58 lines (48 loc) · 1.96 KB
/
WindowDragger.cs
File metadata and controls
58 lines (48 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using UnityEngine;
using UnityEngine.EventSystems;
namespace Michsky.DreamOS
{
public class WindowDragger : UIBehaviour, IBeginDragHandler, IDragHandler
{
[Header("Resources")]
public RectTransform dragArea;
public RectTransform dragObject;
private Vector2 originalLocalPointerPosition;
private Vector3 originalPanelLocalPosition;
public new void Start()
{
if (dragArea == null)
{
try
{
dragArea = GetComponent<RectTransform>();
}
catch { Debug.LogWarning("<b>[Window Dragger]</b> Drag Area is missing.", this); }
}
}
public void OnBeginDrag(PointerEventData data)
{
originalPanelLocalPosition = dragObject.localPosition;
RectTransformUtility.ScreenPointToLocalPointInRectangle(dragArea, data.position, data.pressEventCamera, out originalLocalPointerPosition);
}
public void OnDrag(PointerEventData data)
{
Vector2 localPointerPosition;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(dragArea, data.position, data.pressEventCamera, out localPointerPosition))
{
Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
dragObject.localPosition = originalPanelLocalPosition + offsetToOriginal;
}
ClampToArea();
}
public void ClampToArea()
{
Vector3 pos = dragObject.localPosition;
Vector3 minPosition = dragArea.rect.min - dragObject.rect.min;
Vector3 maxPosition = dragArea.rect.max - dragObject.rect.max;
pos.x = Mathf.Clamp(dragObject.localPosition.x, minPosition.x, maxPosition.x);
pos.y = Mathf.Clamp(dragObject.localPosition.y, minPosition.y, maxPosition.y);
dragObject.localPosition = pos;
}
}
}