-
Notifications
You must be signed in to change notification settings - Fork 1
/
UITween.cs
81 lines (68 loc) · 2.2 KB
/
UITween.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace AV.Animations
{
public interface ILerp {}
public interface ILerp<T> : ILerp
{
void SetLerp(T lerp);
}
public class UITween : MonoBehaviour, ILerp<float>
{
[Serializable] public struct Size
{
public Vector2 start;
public Vector2 end;
}
[Serializable] public struct Data
{
public Keyframes<Vector2> size;
}
public Graphic graphic;
public CanvasGroup group;
public Gradient gradient;
public bool invert;
public float speed = 1;
public Size scale;
public Keyframes<Vector2> sizeFrames;
public Keyframes<Vector2> scaleFrames;
RectTransform rect;
float targetLerp;
float smoothLerp;
void Awake()
{
rect = GetComponent<RectTransform>();
SetLerp(0);
}
private void FixedUpdate()
{
smoothLerp = Mathf.Lerp(smoothLerp, targetLerp, Time.unscaledDeltaTime * speed * 60);
SetLerpRaw(smoothLerp);
}
public void SetLerp(float lerp)
{
targetLerp = lerp;
}
void SetLerpRaw(float lerp)
{
if (invert)
lerp = 1 - lerp;
if (graphic)
graphic.color = gradient.Evaluate(lerp);
if (group)
group.alpha = lerp;
sizeFrames.Evaluate(lerp, out var size1, out var size2, out var sizeTime);
if (size1 != default || size2 != default)
rect.sizeDelta = Vector2.Lerp(size1, size2, sizeTime);
//if (size.start != default || size.end != default)
// rect.sizeDelta = Vector2.Lerp(size.start, size.end, lerp);
scaleFrames.Evaluate(lerp, out var scale1, out var scale2, out var scaleTime);
if (scale1 != default || scale2 != default)
rect.localScale = Vector2.Lerp(scale1, scale2, scaleTime);
}
}
}