-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressBarFiller.cs
More file actions
48 lines (38 loc) · 1.42 KB
/
ProgressBarFiller.cs
File metadata and controls
48 lines (38 loc) · 1.42 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
using Sirenix.OdinInspector;
using UnityEngine;
namespace Demos.SOArchApproach.CodeBase.ScriptableObjectsFramework
{
public class ProgressBarFiller : MonoBehaviour
{
[SerializeField] private RectTransform.Axis movingAxis;
private float startSize;
private RectTransform tr;
private float currentValue;
private float targetValue;
[SerializeField] private float increaseSpeed;
[SerializeField] private float decreaseSpeed;
// Start is called before the first frame update
void Start()
{
tr = transform as RectTransform;
startSize = movingAxis == RectTransform.Axis.Horizontal ? tr.rect.width : tr.rect.height;
currentValue = 0;
targetValue = 0;
}
// Update is called once per frame
void Update()
{
if (targetValue > currentValue)
currentValue = Mathf.MoveTowards(currentValue, targetValue, increaseSpeed * Time.deltaTime);
else
currentValue = Mathf.MoveTowards(currentValue, targetValue, decreaseSpeed * Time.deltaTime);
float newWidth = currentValue * startSize;
tr.SetSizeWithCurrentAnchors(movingAxis, newWidth);
}
[Button]
public void SetTargetValue(float value)
{
targetValue = value;
}
}
}