Last active
December 10, 2015 23:19
-
-
Save benloong/4508691 to your computer and use it in GitHub Desktop.
simple camera fade script
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 UnityEngine; | |
using System.Collections; | |
public class CameraFader : MonoBehaviour { | |
public delegate void endFunction(); | |
public endFunction endCall; | |
Texture2D blackTex; | |
public bool fadeout = false; | |
public float delay = 0.0f; | |
float startTime = 0f; | |
Color color = new Color(1f,1f,1f,0); | |
bool needDestroy = false; //used for delay destroy, as fadeout then fadein screen flashing | |
float _duration = 1f; | |
public float duration { | |
set { | |
_duration = value; | |
} | |
get { | |
return _duration; | |
} | |
} | |
float elapseNormlized { | |
get { | |
#if UNITY_EDITOR | |
float norm = (Time.time - startTime ) / _duration; | |
#else | |
float norm = (Time.realtimeSinceStartup - startTime ) / _duration; | |
#endif | |
return norm; | |
} | |
} | |
public float target { | |
get { | |
return fadeout ? 1.0f : 0f; | |
} | |
} | |
void Awake() | |
{ | |
#if UNITY_EDITOR | |
startTime = Time.time + delay; | |
#else | |
startTime = Time.realtimeSinceStartup + delay; | |
#endif | |
blackTex = new Texture2D(1,1,TextureFormat.ARGB32,false); | |
Color[] colors = new Color[1]; | |
for (int i = 0; i < colors.Length; i++) { | |
colors[i]=Color.black; | |
} | |
blackTex.SetPixels(colors); | |
blackTex.Apply(); | |
} | |
void Update() { | |
if(needDestroy) { | |
Destroy(gameObject); | |
return; | |
} | |
float current = 0; | |
float norm = elapseNormlized; | |
if(norm > 1.0f) { | |
if(endCall != null) endCall(); | |
needDestroy = true; | |
return; | |
} | |
if(fadeout) { | |
current = Mathf.Lerp(0f, 1f, norm); | |
} | |
else { | |
current = Mathf.Lerp(1f, 0f, norm); | |
} | |
color.a = current; | |
} | |
void OnGUI () | |
{ | |
if(Event.current.type != Event.repaint) return; | |
#if UNITY_EDITOR | |
if(Time.time < startTime ) return; | |
#else | |
if(Time.realtimeSinceStartup < startTime) return; | |
#endif | |
Color c = GUI.color; | |
GUI.color = color; | |
int depth = GUI.depth; | |
GUI.depth = -10; | |
GUI.DrawTexture(new Rect(0,0, Screen.width, Screen.height), blackTex); | |
GUI.depth = depth; | |
GUI.color = c; | |
} | |
public static CameraFader NewFadeIn(float duration = 1f, float delay = 0.0f) | |
{ | |
GameObject go = new GameObject("CameraFader"); | |
DontDestroyOnLoad(go); | |
CameraFader fader = go.AddComponent<CameraFader>(); | |
fader.fadeout = false; | |
fader.duration = duration; | |
fader.delay = delay; | |
return fader; | |
} | |
public static CameraFader NewFadeOut(float duration = 1f, float delay = 0.0f) { | |
GameObject go = new GameObject("CameraFader"); | |
DontDestroyOnLoad(go); | |
CameraFader fader = go.AddComponent<CameraFader>(); | |
fader.fadeout = true; | |
fader.duration = duration; | |
fader.delay = delay; | |
return fader; | |
} | |
} |
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 UnityEngine; | |
using System.Collections; | |
public class CameraFaderTest : MonoBehaviour { | |
void Start(){ | |
//level 1 enter fade in | |
CameraFader.NewFadeIn().endCall = delegate { | |
//finish level1 | |
//level 1 fade out | |
CameraFader.NewFadeOut().endCall = delegate { | |
//load level2 | |
//and then fade in level2 | |
CameraFader.NewFadeIn(); | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment