Last active
January 13, 2019 04:48
-
-
Save TomoG29/b1f281fbaa19bb9659a083426151106e to your computer and use it in GitHub Desktop.
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.UI; | |
using UnityEngine.SceneManagement; | |
//フェードイン・アウトを行うクラス | |
public class FadeController : MonoBehaviour { | |
private static GameObject fadeCanvas; | |
private static Canvas canvas; | |
private static GameObject imageObject; | |
private static float alfa; | |
private static int FadeLayer = 0;//フェードイン・アウトのレイヤー | |
public bool FadeControl(string SceneName,float FadeSpeed) | |
{ | |
switch (FadeLayer) | |
{ | |
case 0://ゲームオブジェクト作成 | |
FadeControllerInit(); | |
FadeLayer++; | |
break; | |
case 1://フェードアウト | |
if (StartFadeOut(FadeSpeed) == true) | |
{ | |
FadeLayer++; | |
} | |
break; | |
case 2://シーン変更 | |
SceneManager.LoadScene(SceneName); | |
FadeLayer++; | |
break; | |
case 3://フェードイン | |
if (StartFadeIn(FadeSpeed) == true) | |
{ | |
FadeLayer++; | |
} | |
break; | |
case 4://終了 | |
FadeLayer = 0; | |
Destroy(imageObject); | |
Destroy(fadeCanvas); | |
return true; | |
} | |
return false; | |
} | |
//フェードイン・アウトをコントロールするクラスの初期化 | |
private void FadeControllerInit() | |
{ | |
CreatePanel(); | |
alfa = imageObject.GetComponent<Image>().color.a; | |
DontDestroyOnLoad(fadeCanvas); | |
DontDestroyOnLoad(imageObject); | |
} | |
//フェードインを行う関数 | |
private bool StartFadeIn(float fadeSpeed) | |
{ | |
bool flag = false; | |
alfa -= fadeSpeed; | |
if(alfa <= 0) | |
{ | |
flag = true; | |
alfa = 0; | |
} | |
setAlfa(); | |
return flag; | |
} | |
//フェードアウトを行う関数 | |
private bool StartFadeOut(float fadeSpeed) | |
{ | |
bool flag = false; | |
alfa += fadeSpeed; | |
if (alfa >= 1.0f) | |
{ | |
flag = true; | |
alfa = 1.0f; | |
} | |
setAlfa(); | |
Debug.Log(alfa); | |
return flag; | |
} | |
//透明度の設定を行う関数 | |
private void setAlfa() | |
{ | |
imageObject.GetComponent<Image>().color = new Color(imageObject.GetComponent<Image>().color.r, imageObject.GetComponent<Image>().color.g, imageObject.GetComponent<Image>().color.b, alfa); | |
} | |
//フェードイン・アウト用のパネル作成 | |
private void CreatePanel() | |
{ | |
fadeCanvas = new GameObject("FadeCanvas"); | |
//fadeCanvas.transform.SetParent(transform); | |
//fadeCanvas.SetActive(false); | |
canvas = fadeCanvas.AddComponent<Canvas>(); | |
canvas.renderMode = RenderMode.ScreenSpaceOverlay; | |
canvas.sortingOrder = 999; | |
fadeCanvas.AddComponent<CanvasScaler>().uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; | |
fadeCanvas.AddComponent<GraphicRaycaster>(); | |
imageObject = new GameObject("Image"); | |
imageObject.name = "FadePanel"; | |
imageObject.transform.SetParent(fadeCanvas.transform, false); | |
imageObject.AddComponent<Image>().color = Color.black; | |
imageObject.GetComponent<Image>().color = new Color(imageObject.GetComponent<Image>().color.r, imageObject.GetComponent<Image>().color.g, imageObject.GetComponent<Image>().color.b, 0); | |
imageObject.GetComponent<RectTransform>().sizeDelta = new Vector2(2000, 2000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment