Last active
June 29, 2016 15:13
-
-
Save kazumalab/2646d8afd02db4ef635612df8135981e to your computer and use it in GitHub Desktop.
Unityでのフェードイン、フェードアウト
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 UnityEngine; | |
using System.Collections; | |
using UnityEngine.SceneManagement; | |
using UnityEngine.UI; | |
public class FadeManager : MonoBehaviour { // ここでゲームオーバー処理とクリア処理 | |
// --------------------------- | |
/// <summary> | |
/// 基本的にenableFadeをtrueにしたあと、したいフェードのboolをtrueに変える | |
/// </summary> | |
public bool enableFade = false; | |
public bool enableFadeIn = false; | |
public bool enableFadeOut = false; | |
public bool enableFadeOn = false; | |
public float speed = 0.02f; | |
public Image FadeImage; | |
private float count = 1f; | |
private bool enableAlphaTop = false; | |
// --------------------------- | |
void Start () { | |
enableFade = true; | |
enableFadeIn = true; | |
setAlpha (FadeImage, count); | |
} | |
void Update () { | |
if (enableFadeOn) { | |
FadeInAndOut (FadeImage); | |
} | |
if (enableFadeIn) { | |
FadeIn (FadeImage); | |
} | |
if (enableFadeOut) { | |
FadeOut (FadeImage); | |
} | |
} | |
void setAlpha(Image image,float alpha) { | |
image.color = new Color (image.color.r, image.color.g, image.color.b, alpha); | |
} | |
public void FadeOut(Image image) { | |
if (enableFade) { | |
count += speed; | |
setAlpha (image, count); | |
if (image.color.a >= 1f) { | |
enableFade = false; | |
if (enableFadeOut) { | |
//SceneManager.LoadScene (1); | |
// フェードアウトした時の処理をここに書く | |
} | |
} | |
} | |
} | |
void FadeIn(Image image) { | |
if (enableFade) { | |
count -= speed; | |
setAlpha (image, count); | |
if (image.color.a <= 0f) { | |
enableFade = false; | |
enableFadeIn = false; | |
} | |
} | |
} | |
void FadeInAndOut(Image image) { | |
if (enableFade) { | |
if (!enableAlphaTop) { | |
count += speed; | |
} else { | |
count -= speed; | |
if (image.color.a <= 0f) { | |
enableFade = false; | |
enableFadeOn = false; | |
enableAlphaTop = false; | |
} | |
} | |
setAlpha (image, count); | |
if (image.color.a >= 1f) { | |
enableAlphaTop = true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment