-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFollowDragger.cs
More file actions
127 lines (94 loc) · 3.25 KB
/
Copy pathFollowDragger.cs
File metadata and controls
127 lines (94 loc) · 3.25 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class FollowDragger : MonoBehaviour, IDragHandler, IInitializePotentialDragHandler, IEndDragHandler
{
public float normalizedSliderSpeed = 0.7f;
public bool easing = false;
public float smoothing = 0.2f;
private Vector2 m_currentPosition;
private float currentVelocity;
[SerializeField]private Slider m_slider;
public Slider Slider
{
get
{
if (!this.m_slider)
this.m_slider = this.GetComponent<Slider>();
if (!this.m_slider) return null;
return this.m_slider;
}
set { this.m_slider = value; }
}
private RectTransform m_sliderTransform;
private RectTransform SliderTransform
{
get
{
if (!this.Slider)
return null;
if (!this.m_sliderTransform)
this.m_sliderTransform = this.Slider.GetComponent<RectTransform>();
return this.m_sliderTransform;
}
}
private void Start()
{
SetTargetToCurrentValue();
}
public void OnInitializePotentialDrag(PointerEventData eventData)
{
if (!this.SliderTransform) return;
Vector2 localPoint;
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(this.SliderTransform, eventData.position, eventData.pressEventCamera, out localPoint))
return;
this.m_currentPosition = localPoint;
}
public void OnDrag(PointerEventData eventData)
{
if (!this.SliderTransform) return;
Vector2 localPoint;
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(this.SliderTransform, eventData.position, eventData.pressEventCamera, out localPoint))
return;
this.m_currentPosition = localPoint;
}
public void OnEndDrag(PointerEventData eventData)
{
SetTargetToCurrentValue();
}
private Vector2 NormalizeLocalPosition(Vector2 value)
{
if (!this.SliderTransform) return Vector2.zero;
var size = this.SliderTransform.rect.size;
var pivot = this.SliderTransform.pivot;
value = new Vector2(Mathf.Clamp01(value.x / size.x + pivot.x), Mathf.Clamp01(value.y / size.y + pivot.y));
return value;
}
private void SetTargetToCurrentValue()
{
if ((bool)this.SliderTransform)
{
m_currentPosition = new Vector2((this.Slider.normalizedValue - this.SliderTransform.pivot.x) * this.SliderTransform.rect.size.x, 0);
}
}
private void OnValidate()
{
currentVelocity = 0;
}
private void Update()
{
if (!this.Slider) return;
var current = this.Slider.normalizedValue;
var target = NormalizeLocalPosition(this.m_currentPosition);
if (Mathf.Abs(target.x - current) <= float.Epsilon)
return;
if (easing)
{
this.Slider.normalizedValue = Mathf.SmoothDamp(current, target.x, ref currentVelocity, smoothing, this.normalizedSliderSpeed);
}
else
{
this.Slider.normalizedValue = Mathf.MoveTowards(current, target.x, Time.deltaTime * this.normalizedSliderSpeed);
}
}
}