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.
65 lines
1.2 KiB
65 lines
1.2 KiB
3 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
|
||
|
namespace UIWidgets
|
||
|
{
|
||
|
[RequireComponent(typeof(RectTransform))]
|
||
|
/// <summary>
|
||
|
/// Slide up. Helper component for Notify.
|
||
|
/// </summary>
|
||
|
public class SlideUp : MonoBehaviour
|
||
|
{
|
||
|
public bool UnscaledTime;
|
||
|
|
||
|
RectTransform rect;
|
||
|
void Awake()
|
||
|
{
|
||
|
rect = transform as RectTransform;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Start animation.
|
||
|
/// </summary>
|
||
|
public void Run()
|
||
|
{
|
||
|
StartCoroutine(StartAnimation());
|
||
|
}
|
||
|
|
||
|
IEnumerator StartAnimation()
|
||
|
{
|
||
|
yield return StartCoroutine(AnimationCollapse());
|
||
|
|
||
|
gameObject.SetActive(false);
|
||
|
}
|
||
|
|
||
|
void OnDisable()
|
||
|
{
|
||
|
Notify.FreeSlide(rect);
|
||
|
}
|
||
|
|
||
|
IEnumerator AnimationCollapse()
|
||
|
{
|
||
|
var max_height = rect.rect.height;
|
||
|
var speed = 200f;//pixels per second
|
||
|
|
||
|
var time = max_height / speed;
|
||
|
var end_time = GetTime() + time;
|
||
|
|
||
|
while (GetTime() <= end_time)
|
||
|
{
|
||
|
var height = Mathf.Lerp(max_height, 0, 1 - (end_time - GetTime()) / time);
|
||
|
|
||
|
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
|
||
|
yield return null;
|
||
|
}
|
||
|
|
||
|
//return height back for future use
|
||
|
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, max_height);
|
||
|
}
|
||
|
|
||
|
float GetTime()
|
||
|
{
|
||
|
return UnscaledTime ? Time.unscaledTime : Time.time;
|
||
|
}
|
||
|
}
|
||
|
}
|