Skip to content

Instantly share code, notes, and snippets.

@naojitaniguchi
Last active October 19, 2015 07:27
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 naojitaniguchi/f6b9ab7fa166da75873a to your computer and use it in GitHub Desktop.
Save naojitaniguchi/f6b9ab7fa166da75873a to your computer and use it in GitHub Desktop.
Splash screen script for Unity
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public enum Fade{ In, Out };
public class SplashImage : MonoBehaviour {
public float fadeTime = 1.0f ;
public int nextScene ;
// Use this for initialization
void Start () {
StartCoroutine ("coroutine");
}
IEnumerator coroutine(){
transform.GetComponent<RawImage>().color = new Vector4( 1.0f, 1.0f, 1.0f, 0.0f );
yield return new WaitForSeconds(0.5f);
yield return StartCoroutine (FadeGUITexture(fadeTime, Fade.In));
yield return new WaitForSeconds(0.25f);
yield return StartCoroutine (FadeGUITexture(fadeTime, Fade.Out));
yield return new WaitForSeconds(0.25f);
Application.LoadLevel(nextScene);
}
// Update is called once per frame
void Update () {
}
IEnumerator FadeGUITexture (float timer, Fade fadeType) {
float start = 0 ;
float end = 0 ;
if ( fadeType == Fade.In ){
start = 0.0f ;
end = 1.0f ;
}else if ( fadeType == Fade.Out ){
start = 1.0f ;
end = 0.0f ;
}
float i = 0.0f;
float step = 1.0f/timer;
while (i < 1.0) {
i += step * Time.deltaTime;
// Debug.Log( i );
float a = Mathf.Lerp(start, end, i)*.5f;
// Debug.Log( a );
transform.GetComponent<RawImage>().color = new Vector4( 1.0f, 1.0f, 1.0f, a );
yield return new WaitForEndOfFrame();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment