找回密码
 立即注册
查看: 5067|回复: 93

[特效] 进入画面时显示logo 渐入渐出

  [复制链接]
发表于 2012-6-11 23:02 | 显示全部楼层 |阅读模式
本帖最后由 机器人 于 2012-6-11 23:39 编辑
  1. using UnityEngine;
  2. using System.Collections;
  3. public class splashScreen : MonoBehaviour {

  4.     public int levelToLoad = 2;                         // this has to correspond to a levelIndex (file>build settings)
  5.     public Texture2D splashLogo;                        // the logo to splash;
  6.     public Color sceneBackgroundColor = new Color(0,0,0);    // choose a background color for the screen
  7.     public float fadeSpeed = 0.3f;
  8.    
  9.     private float alpha = 0f;
  10.     private enum fadeStatus
  11.     {
  12.         fadeIn,
  13.         fadeOut,
  14.         fadeWaiting,
  15.         fadeExit
  16.     }
  17.     private fadeStatus status = fadeStatus.fadeIn;
  18.     private Object[] cams;
  19.     private Camera oldCam;
  20.     private Texture2D backTexture = new Texture2D(1, 1);

  21.    

  22.     void setAlpha(float alpha)
  23.     {
  24.         sceneBackgroundColor = new Color(sceneBackgroundColor.r, sceneBackgroundColor.g, sceneBackgroundColor.b, alpha);
  25.         for (int x = 1; x <= backTexture.width; x++)
  26.         {
  27.             for (int y = 1; y <= backTexture.height; y++)
  28.             {
  29.                 backTexture.SetPixel(x, y, sceneBackgroundColor);
  30.             }
  31.         }
  32.     }

  33.     void Start () {
  34.         cams = GameObject.FindObjectsOfType(typeof(Camera));
  35.         oldCam = Camera.main;
  36.         setAlpha(1.0f);
  37.         DontDestroyOnLoad(this);
  38.         DontDestroyOnLoad(Camera.main);
  39.         DontDestroyOnLoad(backTexture);
  40.         Destroy(Camera.main.GetComponent(typeof (AudioListener)));
  41.         if ((Application.levelCount <= 1) || (Application.levelCount < levelToLoad))
  42.         {
  43.             Debug.Log("I need to have a level to load or the value of level To load is wrong!");
  44.             return;
  45.         }
  46.     }

  47.     void Update () {
  48.         switch(status) {
  49.             case fadeStatus.fadeIn:
  50.                 alpha += 1.0f * 0.3f * Time.deltaTime;
  51.             break;
  52.             case fadeStatus.fadeOut:
  53.                 alpha += -1.0f * 0.3f * Time.deltaTime;
  54.             break;
  55.             case fadeStatus.fadeWaiting:
  56.                 Application.LoadLevel(levelToLoad);
  57.                 status = fadeStatus.fadeOut;
  58.             break;
  59.            // case fadeStatus.fadeExit:
  60.            //     backAlpha += -1.0f * exitTimer * Time.deltaTime;
  61.            //     setAlpha(backAlpha);
  62.            // break;
  63.         }
  64.     }

  65.     void OnGUI()
  66.     {
  67.         if (splashLogo != null)
  68.         {
  69.             GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), backTexture, ScaleMode.StretchToFill, false);
  70.             float left = (Screen.width * 0.5f) - (splashLogo.width * 0.5f);
  71.             float top = (Screen.height * 0.5f) - (splashLogo.height * 0.5f);
  72.             GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, Mathf.Clamp01(alpha));
  73.             GUI.DrawTexture(new Rect(left, top, splashLogo.width, splashLogo.height), splashLogo);
  74.             GUI.Label(new Rect((Screen.width * 0.5f) - 50, top + splashLogo.height, 100, 20), "loading level....");
  75.             backTexture.Apply(); // You need this or you will not get the color you want in the back ground.
  76.             if (alpha > 1.0f)
  77.             {
  78.                 status = fadeStatus.fadeWaiting;
  79.                 alpha = 1.0f;
  80.             }
  81.             if (alpha < 0.0f)
  82.             {
  83.                 status = fadeStatus.fadeExit;
  84.                 oldCam.depth = -1000;
  85.                 Destroy(this);
  86.             }
  87.         }
  88.     }

  89.     void OnDrawGizmos()
  90.     {
  91.         Gizmos.color =new Color(1f, 0f, 0f, .5f);
  92.         Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1));
  93.     }
  94. }
复制代码
绑定到相机上
发表于 2013-5-17 15:17 | 显示全部楼层

不错 不错 不错{:soso__3922851084632044791_6:}
发表于 2013-5-31 10:09 | 显示全部楼层

感谢楼主的无私分享!{:soso__11402694654016840197_7:}
发表于 2014-5-8 14:10 | 显示全部楼层
好东西 好东西
发表于 2017-4-19 15:18 | 显示全部楼层
楼主是超人
发表于 2017-4-19 15:16 | 显示全部楼层
好帖就是要顶
发表于 2017-4-19 15:39 | 显示全部楼层
顶顶多好
发表于 2017-4-19 15:53 | 显示全部楼层
难得一见的好帖
发表于 2017-4-19 15:38 | 显示全部楼层
说的非常好
发表于 2017-5-1 13:23 | 显示全部楼层
好帖就是要顶
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-28 01:54 , Processed in 0.094872 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表