View difference between Paste ID: Rbe6zcUf and 4psnkJJL
SHOW: | | - or go back to the newest paste.
1
using UnityEngine;
2
3
abstract class SmoothChanging<T>
4
{
5
    protected T from; // Стартовое значение любого типа
6
    protected T to; // Финальное значение этого же типа
7-
    protected Lerp lerp; // Моя отдельная утилита, которая плавно меняет 0 до 1 за указанное времени
7+
    protected Lerp lerp; // Моя отдельная утилита, которая плавно меняет 0 до 1 за указанное время
8
9
    public SmoothChanging()
10
    {
11
        lerp = new Lerp();
12
    }
13
14
    public void SetDuration(float duration)
15
    {
16
        // За какое время 0 поднимется до 1,
17
        // а значит, за какое время from станет to.
18
        lerp.SetDuration(duration);
19
    }
20
21
    public void StartChange()
22
    {
23
    	// 0 начинает увеличиваться при запуске этой функции
24
        lerp.Play();
25
    }
26
27
    public void StopChange()
28
    {
29
        // Можно делать паузу
30
        lerp.Stop();
31
    }
32
33
    public void SetFromAndTO(T from, T to)
34
    {
35
        // Любой тип, Vector2/3, float, и т.п.
36
        this.from = from;
37
        this.to = to;
38
    }
39
40
    public abstract T GetValue();
41
}
42
43
// Вот так все просто получается, внизу
44
// float, Vector3, Vector2, но потом 
45
// можно добавить и другое, кватернионы, int-ы, и т.п.
46
47
class SmoothFloatChanging : SmoothChanging<float>
48
{
49
    public override float GetValue()
50
    {
51
        return Mathf.Lerp(from, to, lerp.GetLerp());
52
    }
53
}
54
55
class SmoothVector3Changing : SmoothChanging<Vector3>
56
{
57
    public override Vector3 GetValue()
58
    {
59
        return Vector3.Lerp(from, to, lerp.GetLerp());
60
    }
61
}
62
63
class SmoothVector2Changing : SmoothChanging<Vector2>
64
{
65
    public override Vector2 GetValue()
66
    {
67
        return Vector2.Lerp(from, to, lerp.GetLerp());
68
    }
69
}
70