Last active
May 15, 2017 11:22
-
-
Save aberloni/4f3ec22cc9d735d936784df41e8ddd4b to your computer and use it in GitHub Desktop.
To have a way to fade in/out the screen with a color (u5.6)
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 System.Collections.Generic; | |
using UnityEngine; | |
using System; | |
/// <summary> | |
/// Author : https://gist.github.com/Legogo | |
/// 2017-05-15 | |
/// </summary> | |
public class PlaneFader : MonoBehaviour { | |
[RuntimeInitializeOnLoadMethod] | |
static protected void create(){ | |
_manager = createCarrier<PlaneFader>("[fader]"); | |
} | |
static protected PlaneFader _manager; | |
static public PlaneFader get(){ return _manager; } | |
Camera cam; | |
Coroutine coProcess; | |
Color nothing; | |
Material planeMaterial; | |
Color startColor; | |
Color endColor; | |
float fadeSpeed = 1f; | |
float progress = 0f; | |
void Awake() | |
{ | |
transform.position = Vector3.up * 10000f; | |
cam = gameObject.AddComponent<Camera>(); | |
cam.depth = 100; | |
cam.farClipPlane = 10; | |
cam.useOcclusionCulling = false; | |
cam.enabled = false; | |
cam.clearFlags = CameraClearFlags.Depth; | |
nothing = new Color(0f, 0f, 0f, 0f); | |
GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Quad); | |
plane.transform.SetParent(transform); | |
plane.transform.localScale = Vector3.one * 100f; | |
plane.transform.localPosition = Vector3.forward; | |
MeshRenderer render = plane.GetComponent<MeshRenderer>(); | |
planeMaterial = new Material(render.material); | |
render.material = planeMaterial; | |
planeMaterial.SetFloat("_Mode", 3f); // transparent | |
planeMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); | |
planeMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); | |
planeMaterial.SetInt("_ZWrite", 0); | |
planeMaterial.DisableKeyword("_ALPHATEST_ON"); | |
planeMaterial.EnableKeyword("_ALPHABLEND_ON"); | |
planeMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON"); | |
planeMaterial.renderQueue = 3000; | |
planeMaterial.color = nothing; | |
} | |
public void callFadeOut(Action cb = null){ | |
//Debug.Log("fade out"); | |
if (coProcess != null) StopCoroutine(coProcess); | |
coProcess = StartCoroutine(processFadeTo(planeMaterial.color, nothing, 1f, cb)); | |
} | |
public void callFadeIn(Color overlay, Action cb = null){ | |
//Debug.Log("fade in"); | |
if (coProcess != null) StopCoroutine(coProcess); | |
coProcess = StartCoroutine(processFadeTo(nothing, overlay, 0f, cb)); | |
} | |
public void callFadeInOut(Color overlay, Action cbFadedIn = null, Action cbFadedOut = null){ | |
if (coProcess != null) StopCoroutine(coProcess); | |
coProcess = StartCoroutine(processFadeTo(nothing, overlay, 0f, | |
delegate() { | |
if (coProcess != null) StopCoroutine(coProcess); | |
coProcess = StartCoroutine(processFadeTo(planeMaterial.color, nothing, 1f, cbFadedOut)); | |
}) | |
); | |
} | |
IEnumerator processFadeTo(Color origin, Color target, float originAlpha = 1f, Action cb = null){ | |
startColor.r = origin.r; | |
startColor.g = origin.g; | |
startColor.b = origin.b; | |
startColor.a = originAlpha; | |
planeMaterial.color = startColor; | |
cam.enabled = true; | |
progress = 0f; | |
endColor = target; | |
while (planeMaterial.color != endColor) | |
{ | |
progress += GameTime.deltaTime * fadeSpeed; | |
planeMaterial.color = Color.Lerp(startColor, endColor, progress); | |
yield return null; | |
} | |
cam.backgroundColor = endColor; | |
if (cb != null) cb(); | |
} | |
static public T createCarrier<T>(string nm) where T : MonoBehaviour | |
{ | |
GameObject obj = GameObject.Find(nm); | |
T tmp = null; | |
if (obj != null) | |
{ | |
tmp = obj.GetComponent<T>(); | |
} | |
if (tmp != null) return tmp; | |
if (obj == null) | |
{ | |
obj = new GameObject(nm, typeof(T)); | |
tmp = obj.GetComponent<T>(); | |
} | |
else tmp = obj.AddComponent<T>(); | |
return tmp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment