-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHorizontalScrollBar.cs
More file actions
54 lines (45 loc) · 1.53 KB
/
HorizontalScrollBar.cs
File metadata and controls
54 lines (45 loc) · 1.53 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
using UnityEngine;
namespace Agricosmic.Utilities
{
/// <summary>
/// DEPRECATED - Moves its parent based on its vertical position
/// <br/>
/// TODO: @xavier needs rework :)
/// </summary>
public class HorizontalScrollBar : MonoBehaviour
{
public float _maxX;
public float _minX;
private bool _isClicked = false;
private float _percentOnX = 0;
public float GetBarPercent() => _percentOnX;
public bool IsClicked() => _isClicked;
private void Update()
{
Vector3 localMousePosition = transform.parent.InverseTransformPoint(Input.mousePosition);
if (localMousePosition.x > _minX && localMousePosition.x < _maxX && _isClicked)
{
transform.localPosition = new Vector2(localMousePosition.x, transform.localPosition.y);
_percentOnX = (_maxX - localMousePosition.x) / (_maxX - _minX);
}
if (localMousePosition.x < _minX && _isClicked)
{
transform.localPosition = new Vector2(_minX, transform.localPosition.y);
_percentOnX = 1;
}
if (localMousePosition.x > _maxX && _isClicked)
{
transform.localPosition = new Vector2(_maxX, transform.localPosition.y);
_percentOnX = 0;
}
}
private void OnMouseUp()
{
_isClicked = false;
}
private void OnMouseDown()
{
_isClicked = true;
}
}
};