多米诺 发表于 2012-6-11 23:02

进入画面时显示logo 渐入渐出

本帖最后由 机器人 于 2012-6-11 23:39 编辑

using UnityEngine;
using System.Collections;
public class splashScreen : MonoBehaviour {

    public int levelToLoad = 2;                         // this has to correspond to a levelIndex (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;
   
    private float alpha = 0f;
    private enum fadeStatus
    {
      fadeIn,
      fadeOut,
      fadeWaiting,
      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);
      DontDestroyOnLoad(this);
      DontDestroyOnLoad(Camera.main);
      DontDestroyOnLoad(backTexture);
      Destroy(Camera.main.GetComponent(typeof (AudioListener)));
      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) {
            case fadeStatus.fadeIn:
                alpha += 1.0f * 0.3f * Time.deltaTime;
            break;
            case fadeStatus.fadeOut:
                alpha += -1.0f * 0.3f * Time.deltaTime;
            break;
            case fadeStatus.fadeWaiting:
                Application.LoadLevel(levelToLoad);
                status = fadeStatus.fadeOut;
            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;
                alpha = 1.0f;
            }
            if (alpha < 0.0f)
            {
                status = fadeStatus.fadeExit;
                oldCam.depth = -1000;
                Destroy(this);
            }
      }
    }

    void OnDrawGizmos()
    {
      Gizmos.color =new Color(1f, 0f, 0f, .5f);
      Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1));
    }
}
绑定到相机上

cxbsr 发表于 2013-5-17 15:17


不错 不错 不错{:soso__3922851084632044791_6:}

cxbsr 发表于 2013-5-31 10:09


感谢楼主的无私分享!{:soso__11402694654016840197_7:}

aaabbbsss12345 发表于 2014-5-8 14:10

好东西 好东西

老徐 发表于 2017-4-19 15:18

楼主是超人

guba_game 发表于 2017-4-19 15:16

好帖就是要顶

逝水 发表于 2017-4-19 15:39

顶顶多好

阡陌道人 发表于 2017-4-19 15:53

难得一见的好帖

Roland_TIN 发表于 2017-4-19 15:38

说的非常好

︶ ̄单行线╭丶 发表于 2017-5-1 13:23

好帖就是要顶
页: [1]
查看完整版本: 进入画面时显示logo 渐入渐出