Last active
April 20, 2018 02:37
-
-
Save KRNKRS/32846446e5cc5cb13a84b05c8b067ec3 to your computer and use it in GitHub Desktop.
フェードイン / アウトの挙動をUI含め1スクリプトで制御する
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; | |
using System.Collections; | |
using UnityEngine; | |
using UnityEngine.UI; | |
[DisallowMultipleComponent] | |
public class FadeController : MonoBehaviour { | |
private const string COROUTINE_NAME = "AlphaTransition"; | |
private Canvas m_canvas; | |
private static Image s_fadePanel; | |
[SerializeField] | |
private Color m_color = Color.black; | |
[HideInInspector] | |
public delegate void CallBack(); | |
private CallBack m_callBack; | |
private static FadeController s_instance; | |
private FadeController() { } | |
private void Start () { | |
m_callBack = null; | |
IsComplete = false; | |
} | |
public void FadeIn(float _fadeTime) { | |
StartCoroutine(COROUTINE_NAME, -(_fadeTime)); | |
} | |
public void FadeOut(float _fadeTime) { | |
StartCoroutine(COROUTINE_NAME, _fadeTime); | |
} | |
public void FadeIn(float _fadeTime, Color _color) { | |
SetColor(_color); | |
FadeIn(_fadeTime); | |
} | |
public void FadeOut(float _fadeTime, Color _color) { | |
SetColor(_color); | |
FadeOut(_fadeTime); | |
} | |
public void FadeIn(float _fadeTime, CallBack _finishedCallBack) { | |
m_callBack = _finishedCallBack; | |
FadeIn(_fadeTime); | |
} | |
public void FadeOut(float _fadeTime, CallBack _finishedCallBack) { | |
m_callBack = _finishedCallBack; | |
FadeOut(_fadeTime); | |
} | |
public void FadeIn(float _fadeTime, Color _color, CallBack _finishedCallBack) { | |
SetColor(_color); | |
FadeIn(_fadeTime, _finishedCallBack); | |
} | |
public void FadeOut(float _fadeTime, Color _color, CallBack _finishedCallBack) { | |
SetColor(_color); | |
FadeOut(_fadeTime, _finishedCallBack); | |
} | |
public void SetColor(Color _color) { | |
m_color = _color; | |
} | |
private IEnumerator AlphaTransition(float _fadeTime) { | |
float alfa = _fadeTime < 0 ? 1.0f : 0.0f; | |
IsComplete = false; | |
while (0.0f <= alfa && alfa <= 1.0f) { | |
float delta = Time.deltaTime; | |
alfa += delta / _fadeTime; | |
s_fadePanel.color = new Color(m_color.r, m_color.g, m_color.b, alfa); | |
yield return new WaitForSeconds(delta); | |
} | |
IsComplete = true; | |
//コールバック | |
if (m_callBack != null) { | |
m_callBack.Invoke(); | |
m_callBack = null; | |
} | |
} | |
public bool IsComplete { private set; get; } | |
public static FadeController Instance { | |
get { | |
if(s_instance == null) { | |
GameObject controllerObj = new GameObject("FadeControllerObject"); | |
s_instance = controllerObj.AddComponent<FadeController>(); | |
DontDestroyOnLoad(controllerObj); | |
GameObject canvasObj = new GameObject("FadeCnavas"); | |
Canvas canvasComp = canvasObj.AddComponent<Canvas>(); | |
canvasObj.AddComponent<CanvasScaler>(); | |
canvasObj.AddComponent<GraphicRaycaster>(); | |
canvasObj.transform.SetParent(controllerObj.transform); | |
GameObject fadePanelObj = new GameObject("FadePanel"); | |
fadePanelObj.AddComponent<CanvasRenderer>(); | |
fadePanelObj.transform.SetParent(canvasObj.transform); | |
s_fadePanel = fadePanelObj.AddComponent<Image>(); | |
canvasComp.renderMode = RenderMode.ScreenSpaceOverlay; | |
s_fadePanel.rectTransform.anchorMin = Vector2.zero; | |
s_fadePanel.rectTransform.anchorMax = Vector2.one; | |
s_fadePanel.rectTransform.sizeDelta = Vector2.zero; | |
} | |
return s_instance; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment