Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- abstract class SmoothChanging<T>
- {
- protected T from; // Стартовое значение любого типа
- protected T to; // Финальное значение этого же типа
- protected Lerp lerp; // Моя отдельная утилита, которая плавно меняет 0 до 1 за указанное время
- public SmoothChanging()
- {
- lerp = new Lerp();
- }
- public void SetDuration(float duration)
- {
- // За какое время 0 поднимется до 1,
- // а значит, за какое время from станет to.
- lerp.SetDuration(duration);
- }
- public void StartChange()
- {
- // 0 начинает увеличиваться при запуске этой функции
- lerp.Play();
- }
- public void StopChange()
- {
- // Можно делать паузу
- lerp.Stop();
- }
- public void SetFromAndTO(T from, T to)
- {
- // Любой тип, Vector2/3, float, и т.п.
- this.from = from;
- this.to = to;
- }
- public abstract T GetValue();
- }
- // Вот так все просто получается, внизу
- // float, Vector3, Vector2, но потом
- // можно добавить и другое, кватернионы, int-ы, и т.п.
- class SmoothFloatChanging : SmoothChanging<float>
- {
- public override float GetValue()
- {
- return Mathf.Lerp(from, to, lerp.GetLerp());
- }
- }
- class SmoothVector3Changing : SmoothChanging<Vector3>
- {
- public override Vector3 GetValue()
- {
- return Vector3.Lerp(from, to, lerp.GetLerp());
- }
- }
- class SmoothVector2Changing : SmoothChanging<Vector2>
- {
- public override Vector2 GetValue()
- {
- return Vector2.Lerp(from, to, lerp.GetLerp());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment