Last active
November 12, 2018 02:19
-
-
Save HassakuTb/ac338893fd71fd86c7c0e60dc2bda9dd to your computer and use it in GitHub Desktop.
UGUI Window that open with fade in animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using UnityEngine; | |
| namespace Citrus.UI{ | |
| /// <summary> | |
| /// 開くときにフェードインするウインドウ | |
| /// </summary> | |
| [RequireComponent(typeof(CanvasGroup))] | |
| public class FadeInWindow : MonoBehaviour { | |
| // ログインウィンドウウィンドウ部分 | |
| [SerializeField] private CanvasGroup window; | |
| // アニメーション開始時のスケール | |
| [SerializeField] private float startScale = 0.8f; | |
| // スケールのカーブ | |
| [SerializeField] private AnimationCurve scaleCurve; | |
| // アニメーション時間 | |
| [SerializeField] private float animationTime = 0.2f; | |
| // 自分自身のCanvasGroup | |
| private CanvasGroup seal; | |
| // 自分自身は画面全体にしてraycastをブロックする | |
| private void Start() | |
| { | |
| seal = GetComponent<CanvasGroup>(); | |
| } | |
| /// <summary> | |
| /// ログインウィンドウを開く | |
| /// </summary> | |
| public void Open() | |
| { | |
| StartCoroutine(OpenCoroutine()); | |
| } | |
| // アニメーションしながら開く | |
| private IEnumerator OpenCoroutine() | |
| { | |
| seal.SetToEnable(); | |
| float time = 0f; | |
| window.alpha = 0f; | |
| while(time < animationTime) | |
| { | |
| SetScale(time / animationTime); | |
| window.alpha = time / animationTime; | |
| time += Time.deltaTime; | |
| yield return null; | |
| } | |
| window.transform.localScale = new Vector2(1f, 1f); | |
| window.alpha = 1f; | |
| // アニメーションが完了したら利用可能 | |
| foreach(Transform t in window.transform) | |
| { | |
| t.gameObject.SetActive(true); | |
| } | |
| } | |
| /// <summary> | |
| /// ログインウィンドウを閉じる。 | |
| /// </summary> | |
| public void Dismiss() | |
| { | |
| StartCoroutine(DismissCoroutine()); | |
| } | |
| // アニメーションしながら閉じる | |
| private IEnumerator DismissCoroutine() | |
| { | |
| // アニメーション開始時に内容を非アクティブ | |
| foreach (Transform t in window.transform) | |
| { | |
| t.gameObject.SetActive(false); | |
| } | |
| float time = animationTime; | |
| window.alpha = 1f; | |
| while (time > 0f) | |
| { | |
| SetScale(time / animationTime); | |
| window.alpha = time / animationTime; | |
| time -= Time.deltaTime; | |
| yield return null; | |
| } | |
| window.transform.localScale = new Vector2(startScale, startScale); | |
| window.alpha = 0f; | |
| seal.SetToInvisible(); | |
| } | |
| private void SetScale(float ratio) | |
| { | |
| float scale = startScale + (1f - startScale) * scaleCurve.Evaluate(ratio); | |
| window.transform.localScale = new Vector2(scale, scale); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment