Last active
May 23, 2019 19:43
-
-
Save nekonenene/691871701d37b0cc3e9804d3678e730c to your computer and use it in GitHub Desktop.
Unity uGUI のフェードアウト処理(ブログ公開用) https://nekonenene.hatenablog.com/entry/unity-ugui-fadeout-window
This file contains 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; | |
using UnityEngine.EventSystems; | |
using UnityEngine.UI; | |
namespace Util { | |
public static class Fade { | |
public static void FadeoutObject(GameObject gameObject, float fadeoutSec = 2.0f) { | |
if (gameObject.GetComponent<Fadeout>()) return; | |
Fadeout fadeout = gameObject.AddComponent<Fadeout>(); | |
CanvasGroup cg = gameObject.AddComponent<CanvasGroup>(); | |
fadeout.Obj = gameObject; | |
fadeout.Cg = cg; | |
fadeout.FadeDuration = fadeoutSec; | |
fadeout.Start(); | |
} | |
} | |
public class Fadeout : MonoBehaviour { | |
public GameObject Obj; | |
public CanvasGroup Cg; | |
public float FadeDuration; | |
float elapsedTime; // 経過時間 | |
public void Start() { | |
StartCoroutine(fadeoutCoroutine()); | |
} | |
IEnumerator fadeoutCoroutine() { | |
EventSystem eventSystem = GameObject.FindObjectOfType<EventSystem>(); | |
eventSystem.enabled = false; | |
while (elapsedTime <= FadeDuration) { | |
elapsedTime += Time.deltaTime; | |
Cg.alpha = Mathf.Lerp(1, 0, elapsedTime / FadeDuration); | |
yield return null; | |
} | |
Destroy(Cg); | |
Destroy(this); | |
Obj.SetActive(false); | |
eventSystem.enabled = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment