Skip to content

Instantly share code, notes, and snippets.

@soraphis
Last active March 13, 2018 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soraphis/977bcc831161877a4b341f84970687ee to your computer and use it in GitHub Desktop.
Save soraphis/977bcc831161877a4b341f84970687ee to your computer and use it in GitHub Desktop.
Implementation of an CameraFadeScript for Unity 5 with Code only, no gameobjects or ui elements needed. (There has to be a main camera in the scene :P)
using System.Collections;
using UnityEngine;
// does need UnityStandardAssets > ImageEffects
// does not work if you use the ImageEffect "ScreenOverlay" on your camera for visual effects
// Usage:
// From a MonoBehaviour-Class: StartCoroutine(CameraFade.FadeOut(0.8f));
// From inside a coroutine (if you want to wait for finished fading): yield return CameraFade.FadeOut(0.5f);
// From inside a coroutine (if you dont want to wait): CameraFade.FadeOut(0.5f);
class CameraFadeScript : MonoBehaviour {
[SerializeField] private Shader shader;
private Material material;
[Range(0, 1)] public float intensity;
void Awake() {
shader = shader ?? Shader.Find("Hidden/FadeToBlack");
material = new Material(shader);
material.SetColor("_Color", Color.black);
}
void Update() {
material = new Material(shader);
material.SetFloat("_Intensity", intensity);
}
private void OnRenderImage(RenderTexture src, RenderTexture dest) {
Graphics.Blit(src, dest, material);
}
public static CameraFadeScript SetupCameraFade() {
var x = Camera.main.GetComponent<CameraFadeScript>();
if (x == null) {
x = Camera.main.gameObject.AddComponent<CameraFadeScript>();
}
return x;
}
public static IEnumerator FadeOut(float seconds) {
var x = SetupCameraFade();
x.intensity = 0; // fade out: 0 -> 1
for(float i = 0; i < seconds; i+= Time.unscaledDeltaTime) {
x.intensity = i / seconds;
yield return null;
}
x.intensity = 1;
yield return null;
}
public static IEnumerator FadeIn(float seconds) {
var x = SetupCameraFade();
x.intensity = 1; // fade in: 1 -> 0
for (float i = 0; i < seconds; i += Time.unscaledDeltaTime) {
x.intensity = 1 - i / seconds;
yield return null;
}
x.intensity = 0;
yield return null;
}
}
Shader "Hidden/FadeToBlack"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color("FadeColor", Color) = (0,0,0,1)
_Intensity("Intensity", float) = 0
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float _Intensity;
fixed4 _Color;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
col.rgb = col.rgb*(1-_Intensity) + (_Intensity * _Color) ;
return col;
}
ENDCG
}
}
}
public class GameManager : Singleton<GameManager> {
void Awake() {
var x = CameraFadeScript.SetupCameraFade();
x.intensity = 1;
}
private IEnumerator Start() {
var loadUIScene = SceneManager.LoadSceneAsync(3, LoadSceneMode.Additive);
yield return null; // wait for first frame
while (!loadUIScene.isDone) { yield return null; }
yield return CameraFadeScript.FadeIn(1.4f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment