SplashScreen

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Fixed background color.)
Line 1: Line 1:
 
==SplashScreen==
 
==SplashScreen==
  
TODO: Test allot of things. this is a very early version.
+
TODO:
      fade the background color out.
+
* Test allot of things. this is a very early version.
      any additions to this script are always welcome!
+
* Fade the background color out.
 +
* Currently what is fading is a solid color, what's better is to fade the splash screen image itself.
 +
 
 +
Any additions to this script are always welcome!
  
 
Just create a new scene with a camera and empty gameobject.
 
Just create a new scene with a camera and empty gameobject.
Line 10: Line 13:
 
==Code==
 
==Code==
 
<csharp>
 
<csharp>
 
 
using UnityEngine;
 
using UnityEngine;
 
using System.Collections;
 
using System.Collections;
  
 +
//
 +
// SplashScreen Script
 +
//
 +
// Version 0.1 by Martijn Dekker
 +
// martijn.pixelstudio@gmail.com
 +
//
 +
// Version 0.2 by Ferdinand Joseph Fernandez, 2010Sep7 16:45 GMT + 8
 +
// Changes:
 +
//  * changed levelToLoad to a string, for easier usage
 +
//  * added waitTime, which adds a pause after fade in, and before fade
 +
//    out (during fade waiting)
 +
//  * added option to either automatically fade out after waitTime
 +
//    seconds (default), or wait for user input (press any key to continue)
 +
//  * added option to wait until fade out is complete before loading next
 +
//    level, instead of the default, which is to load the next level
 +
//    before fade out
 +
//
  
///////////////////////////////////////
+
public class SplashScreen : MonoBehaviour
/// splashScreen script.           ///
+
{
/// Version 0.1 by Martijn Dekker  ///
+
public string levelToLoad = ""; // this has to correspond to a level (file>build settings)
/// martijn.pixelstudio@gmail.com  ///
+
public Texture2D splashLogo; // the logo to splash;
///////////////////////////////////////
+
public Color sceneBackgroundColor = new Color(0,0,0); // choose a background color for the screen
 +
public float fadeSpeed = 0.3f;
 +
public float waitTime = 0.5f; // seconds to wait before fading out
 +
public bool waitForInput = false; // if true, this acts as a "press any key to continue"
 +
private float timeFadingInFinished = 0.0f;
  
public class splashScreen : MonoBehaviour {
+
public enum SplashType
 +
{
 +
LoadNextLevelThenFadeOut,
 +
FadeOutThenLoadNextLevel
 +
}
 +
public SplashType splashType;
  
    public int levelToLoad = 2;                        // this has to correspond to a levelIndex (file>build settings)
+
private float alpha = 0f;
    public Texture2D splashLogo;                        // the logo to splash;
+
private enum FadeStatus
    public Color sceneBackgroundColor = new Color(0,0,0);    // choose a background color for the screen
+
{
    public float fadeSpeed = 0.3f;
+
FadeIn,
 
+
FadeWaiting,
    private float alpha = 0f;
+
FadeOut//,
    private enum fadeStatus
+
//fadeExit
    {
+
}
        fadeIn,
+
private FadeStatus status = FadeStatus.FadeIn;
        fadeOut,
+
//private Object[] cams;
        fadeWaiting,
+
private Camera oldCam;
        fadeExit
+
private Texture2D backTexture = new Texture2D(1, 1);
    }
+
    private fadeStatus status = fadeStatus.fadeIn;
+
    private Object[] cams;
+
    private Camera oldCam;
+
    private Texture2D backTexture = new Texture2D(1, 1);
+
  
   
 
  
    void setAlpha(float alpha)
 
    {
 
        sceneBackgroundColor = new Color(sceneBackgroundColor.r, sceneBackgroundColor.g, sceneBackgroundColor.b, alpha);
 
        for (int x = 1; x <= backTexture.width; x++)
 
        {
 
            for (int y = 1; y <= backTexture.height; y++)
 
            {
 
                backTexture.SetPixel(x, y, sceneBackgroundColor);
 
            }
 
        }
 
    }
 
  
    void Start () {
+
void SetAlpha(float alpha)
        cams = GameObject.FindObjectsOfType(typeof(Camera));
+
{
        oldCam = Camera.main;
+
sceneBackgroundColor = new Color(sceneBackgroundColor.r, sceneBackgroundColor.g, sceneBackgroundColor.b, alpha);
        setAlpha(1.0f);
+
for (int x = 1; x <= backTexture.width; x++)
        DontDestroyOnLoad(this);
+
{
        DontDestroyOnLoad(Camera.main);
+
for (int y = 1; y <= backTexture.height; y++)
        DontDestroyOnLoad(backTexture);
+
{
        Destroy(Camera.main.GetComponent(typeof (AudioListener)));
+
backTexture.SetPixel(x, y, sceneBackgroundColor);
        if ((Application.levelCount <= 1) || (Application.levelCount < levelToLoad))
+
}
        {
+
}
            Debug.Log("I need to have a level to load or the value of level To load is wrong!");
+
            return;
+
        }
+
 
}
 
}
  
void Update () {
+
 
    switch(status) {
+
void Start()
            case fadeStatus.fadeIn:
+
{
                alpha += 1.0f * 0.3f * Time.deltaTime;
+
//cams = GameObject.FindObjectsOfType(typeof(Camera));
            break;
+
oldCam = Camera.main;
            case fadeStatus.fadeOut:
+
SetAlpha(1.0f);
                alpha += -1.0f * 0.3f * Time.deltaTime;
+
if (splashType == SplashType.LoadNextLevelThenFadeOut)
            break;
+
{
            case fadeStatus.fadeWaiting:
+
DontDestroyOnLoad(this);
                Application.LoadLevel(levelToLoad);
+
DontDestroyOnLoad(Camera.main);
                status = fadeStatus.fadeOut;
+
DontDestroyOnLoad(backTexture);
            break;
+
}
          // case fadeStatus.fadeExit:
+
if ((Application.levelCount <= 1) || (levelToLoad == ""))
          //    backAlpha += -1.0f * exitTimer * Time.deltaTime;
+
{
          //    setAlpha(backAlpha);
+
Debug.Log("I need to have a level to load or the value of level To load is wrong!");
          // break;
+
return;
        }
+
}
 
}
 
}
  
    void OnGUI()
+
void Update()
    {
+
{
        if (splashLogo != null)
+
switch(status)
        {
+
{
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), backTexture, ScaleMode.StretchToFill, false);
+
case FadeStatus.FadeIn:
            float left = (Screen.width * 0.5f) - (splashLogo.width * 0.5f);
+
alpha += 1.0f * fadeSpeed * Time.deltaTime;
            float top = (Screen.height * 0.5f) - (splashLogo.height * 0.5f);
+
break;
            GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, Mathf.Clamp01(alpha));
+
case FadeStatus.FadeWaiting:
            GUI.DrawTexture(new Rect(left, top, splashLogo.width, splashLogo.height), splashLogo);
+
if ((!waitForInput && Time.time >= timeFadingInFinished + waitTime) || (waitForInput && Input.anyKey))
            GUI.Label(new Rect((Screen.width * 0.5f) - 50, top + splashLogo.height, 100, 20), "loading level....");
+
{
            backTexture.Apply(); // You need this or you will not get the color you want in the back ground.
+
if (splashType == SplashType.LoadNextLevelThenFadeOut)
            if (alpha > 1.0f)  
+
{
            {
+
Application.LoadLevel(levelToLoad);
                status = fadeStatus.fadeWaiting;
+
}
                alpha = 1.0f;
+
status = FadeStatus.FadeOut;
            }
+
}
            if (alpha < 0.0f)
+
break;
            {
+
case FadeStatus.FadeOut:
                status = fadeStatus.fadeExit;
+
alpha += -1.0f * fadeSpeed * Time.deltaTime;
                oldCam.depth = -1000;
+
break;
                Destroy(this);
+
  // case FadeStatus.fadeExit:
            }
+
  //    backAlpha += -1.0f * exitTimer * Time.deltaTime;
        }
+
  //    SetAlpha(backAlpha);
    }
+
  // break;
 +
}
 +
}
  
    void OnDrawGizmos()
+
void OnGUI()
    {
+
{
        Gizmos.color =new Color(1f, 0f, 0f, .5f);
+
if (splashLogo != null)
        Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1));
+
{
    }
+
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), backTexture, ScaleMode.StretchToFill, false);
}
+
float left = (Screen.width * 0.5f) - (splashLogo.width * 0.5f);
 +
float top = (Screen.height * 0.5f) - (splashLogo.height * 0.5f);
 +
GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, Mathf.Clamp01(alpha));
 +
GUI.DrawTexture(new Rect(left, top, splashLogo.width, splashLogo.height), splashLogo);
 +
//GUI.Label(new Rect((Screen.width * 0.5f) - 50, top + splashLogo.height, 100, 20), "loading level....");
 +
backTexture.Apply(); // You need this or you will not get the color you want in the back ground.
 +
if (alpha > 1.0f)
 +
{
 +
status = FadeStatus.FadeWaiting;
 +
timeFadingInFinished = Time.time;
 +
alpha = 1.0f;
 +
}
 +
if (alpha < 0.0f)
 +
{
 +
//status = FadeStatus.fadeExit;
 +
if (splashType == SplashType.FadeOutThenLoadNextLevel)
 +
{
 +
Application.LoadLevel(levelToLoad);
 +
}
 +
else
 +
{
 +
Destroy(oldCam.GetComponent<AudioListener>());
 +
oldCam.depth = -1000;
 +
//Destroy(oldCam);
 +
//Destroy(backTexture);
 +
Destroy(this);
 +
}
 +
}
 +
}
 +
}
  
 +
void OnDrawGizmos()
 +
{
 +
Gizmos.color = new Color(1f, 0f, 0f, .5f);
 +
Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1));
 +
}
 +
}
 
</csharp>
 
</csharp>

Revision as of 13:06, 7 September 2010

SplashScreen

TODO:

  • Test allot of things. this is a very early version.
  • Fade the background color out.
  • Currently what is fading is a solid color, what's better is to fade the splash screen image itself.

Any additions to this script are always welcome!

Just create a new scene with a camera and empty gameobject. Add the script to the gameobject and set properties (nextlevel and logo)

Code

<csharp> using UnityEngine; using System.Collections;

// // SplashScreen Script // // Version 0.1 by Martijn Dekker // martijn.pixelstudio@gmail.com // // Version 0.2 by Ferdinand Joseph Fernandez, 2010Sep7 16:45 GMT + 8 // Changes: // * changed levelToLoad to a string, for easier usage // * added waitTime, which adds a pause after fade in, and before fade // out (during fade waiting) // * added option to either automatically fade out after waitTime // seconds (default), or wait for user input (press any key to continue) // * added option to wait until fade out is complete before loading next // level, instead of the default, which is to load the next level // before fade out //

public class SplashScreen : MonoBehaviour { public string levelToLoad = ""; // this has to correspond to a level (file>build settings) public Texture2D splashLogo; // the logo to splash; public Color sceneBackgroundColor = new Color(0,0,0); // choose a background color for the screen public float fadeSpeed = 0.3f; public float waitTime = 0.5f; // seconds to wait before fading out public bool waitForInput = false; // if true, this acts as a "press any key to continue" private float timeFadingInFinished = 0.0f;

public enum SplashType { LoadNextLevelThenFadeOut, FadeOutThenLoadNextLevel } public SplashType splashType;

private float alpha = 0f; private enum FadeStatus { FadeIn, FadeWaiting, FadeOut//, //fadeExit } private FadeStatus status = FadeStatus.FadeIn; //private Object[] cams; private Camera oldCam; private Texture2D backTexture = new Texture2D(1, 1);


void SetAlpha(float alpha) { sceneBackgroundColor = new Color(sceneBackgroundColor.r, sceneBackgroundColor.g, sceneBackgroundColor.b, alpha); for (int x = 1; x <= backTexture.width; x++) { for (int y = 1; y <= backTexture.height; y++) { backTexture.SetPixel(x, y, sceneBackgroundColor); } } }


void Start() { //cams = GameObject.FindObjectsOfType(typeof(Camera)); oldCam = Camera.main; SetAlpha(1.0f); if (splashType == SplashType.LoadNextLevelThenFadeOut) { DontDestroyOnLoad(this); DontDestroyOnLoad(Camera.main); DontDestroyOnLoad(backTexture); } if ((Application.levelCount <= 1) || (levelToLoad == "")) { Debug.Log("I need to have a level to load or the value of level To load is wrong!"); return; } }

void Update() { switch(status) { case FadeStatus.FadeIn: alpha += 1.0f * fadeSpeed * Time.deltaTime; break; case FadeStatus.FadeWaiting: if ((!waitForInput && Time.time >= timeFadingInFinished + waitTime) || (waitForInput && Input.anyKey)) { if (splashType == SplashType.LoadNextLevelThenFadeOut) { Application.LoadLevel(levelToLoad); } status = FadeStatus.FadeOut; } break; case FadeStatus.FadeOut: alpha += -1.0f * fadeSpeed * Time.deltaTime; break; // case FadeStatus.fadeExit: // backAlpha += -1.0f * exitTimer * Time.deltaTime; // SetAlpha(backAlpha); // break; } }

void OnGUI() { if (splashLogo != null) { GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), backTexture, ScaleMode.StretchToFill, false); float left = (Screen.width * 0.5f) - (splashLogo.width * 0.5f); float top = (Screen.height * 0.5f) - (splashLogo.height * 0.5f); GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, Mathf.Clamp01(alpha)); GUI.DrawTexture(new Rect(left, top, splashLogo.width, splashLogo.height), splashLogo); //GUI.Label(new Rect((Screen.width * 0.5f) - 50, top + splashLogo.height, 100, 20), "loading level...."); backTexture.Apply(); // You need this or you will not get the color you want in the back ground. if (alpha > 1.0f) { status = FadeStatus.FadeWaiting; timeFadingInFinished = Time.time; alpha = 1.0f; } if (alpha < 0.0f) { //status = FadeStatus.fadeExit; if (splashType == SplashType.FadeOutThenLoadNextLevel) { Application.LoadLevel(levelToLoad); } else { Destroy(oldCam.GetComponent<AudioListener>()); oldCam.depth = -1000; //Destroy(oldCam); //Destroy(backTexture); Destroy(this); } } } }

void OnDrawGizmos() { Gizmos.color = new Color(1f, 0f, 0f, .5f); Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1)); } } </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox