You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.5 KiB
85 lines
2.5 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public enum AnimationType
|
|
{
|
|
EaseInOut,
|
|
Ease,
|
|
Lerp,
|
|
SlowThenFast
|
|
}
|
|
|
|
[RequireComponent(typeof(RectTransform))]
|
|
public class Resizable : MonoBehaviour
|
|
{
|
|
RectTransform RectTransform;
|
|
[SerializeField] private Vector2 Minimum;
|
|
[SerializeField] private Vector2 Maximum;
|
|
private bool IsStretched;
|
|
|
|
private void Awake()
|
|
{
|
|
RectTransform = GetComponent<RectTransform>();
|
|
IsStretched = RectTransform.rect.width > Minimum.x;
|
|
}
|
|
|
|
IEnumerator Lerp(bool toMaximum, AnimationType animation, float time)
|
|
{
|
|
float t = 0;
|
|
float originalWidth = RectTransform.rect.width;
|
|
float originalHeight = RectTransform.rect.height;
|
|
while (t <= time)
|
|
{
|
|
switch(animation)
|
|
{
|
|
case AnimationType.Lerp:
|
|
{
|
|
if(toMaximum)
|
|
{
|
|
RectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Mathf.Lerp(originalWidth, Maximum.x, t));
|
|
if(Maximum.y != -1)
|
|
{
|
|
RectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Mathf.Lerp(originalHeight, Maximum.y, t));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
RectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Mathf.Lerp(originalWidth, Minimum.x, t));
|
|
if (Minimum.y != -1)
|
|
{
|
|
RectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Mathf.Lerp(originalHeight, Minimum.y, t));
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
t += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
StopCoroutine(Lerp(toMaximum, animation, time));
|
|
}
|
|
|
|
public void Stretch(AnimationType animation = AnimationType.Lerp, float time = 5)
|
|
=> StartCoroutine(Lerp(true, animation, time));
|
|
|
|
public void Shrink(AnimationType animation = AnimationType.Lerp, float time = 5)
|
|
=> StartCoroutine(Lerp(false, animation, time));
|
|
|
|
public void Toggle()
|
|
{
|
|
if(IsStretched)
|
|
{
|
|
Shrink();
|
|
|
|
IsStretched = false;
|
|
}
|
|
else
|
|
{
|
|
Stretch();
|
|
|
|
IsStretched = true;
|
|
}
|
|
}
|
|
}
|
|
|