-
Notifications
You must be signed in to change notification settings - Fork 4
/
DragController.cs
52 lines (45 loc) · 1.88 KB
/
DragController.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DragController : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
public RectTransform currentTransform;
private GameObject mainContent;
private Vector3 currentPossition;
private int totalChild;
public void OnPointerDown(PointerEventData eventData)
{
currentPossition = currentTransform.position;
mainContent = currentTransform.parent.gameObject;
totalChild = mainContent.transform.childCount;
}
public void OnDrag(PointerEventData eventData)
{
currentTransform.position =
new Vector3(currentTransform.position.x, eventData.position.y, currentTransform.position.z);
for (int i = 0; i < totalChild; i++)
{
if (i != currentTransform.GetSiblingIndex())
{
Transform otherTransform = mainContent.transform.GetChild(i);
int distance = (int) Vector3.Distance(currentTransform.position,
otherTransform.position);
if (distance <= 10)
{
Vector3 otherTransformOldPosition = otherTransform.position;
otherTransform.position = new Vector3(otherTransform.position.x, currentPossition.y,
otherTransform.position.z);
currentTransform.position = new Vector3(currentTransform.position.x, otherTransformOldPosition.y,
currentTransform.position.z);
currentTransform.SetSiblingIndex(otherTransform.GetSiblingIndex());
currentPossition = currentTransform.position;
}
}
}
}
public void OnPointerUp(PointerEventData eventData)
{
currentTransform.position = currentPossition;
}
}