控制切換場景淡入淡出的教程

using UnityEngine;
using System.Collections;
/*
 * 使用方法:將該腳本附在鏡頭下面,然後切換場景時鏡頭不銷燬
 * 要調用的時候獲得該腳本,然後調用StartSplash()
 */
 
//@author joi 
public class SceneLoad : MonoBehaviour {
 
    public int guiDepth = 0;
     
    public string levelToLoad = "";
    //將要加載的場景序號
    public int levelToLoadInt;
    //切換場景的紋理
    public Texture2D splashLogo;
    //淡入淡出的速度
    float fadeSpeed = 0.8f;
    //保持紋理最高透明度的時間
    float waitTime = 0f;
    //是否要等待輸入
    public bool waitForInput = false;
     
    public bool startAutomatically = false;
    public bool IsDealPlayer=false;
    private float timeFadingInFinished = 0.0f;
    //處理切換場景後需要實現的事件
    public delegate void EventHandler();
 
     
    public event EventHandler trigger;
    public delegate void EventHandler2nd();
 
     
    public event EventHandler2nd triggerAtLoading;
    //淡入淡出方式
    public enum SplashType
    {
        LoadNextLevelThenFadeOut,
        FadeOutThenLoadNextLevel
    }
    public SplashType splashType;
 
    private float alpha = 0.0f;
    //紋理的狀態
    private enum FadeStatus
    {
        Paused,
        FadeIn,
        FadeWaiting,
        FadeOut
    }
    private FadeStatus status = FadeStatus.Paused;
 
    private Rect splashLogoPos = new Rect();
    //是否要自適應屏幕大小
    public enum LogoPositioning
    {
        Centered,
        Stretched
    }
    public LogoPositioning logoPositioning;
 
    private bool loadingNextLevel = false;
     
    void Start ()
    {
         
//      if(startAutomatically)
//      {
//          status = FadeStatus.FadeIn;
//      }
//      else
//      {
//          status = FadeStatus.Paused;
//      }
        if(logoPositioning == LogoPositioning.Centered)
        {
            splashLogoPos.x = (Screen.width * 0.5f) - (splashLogo.width * 0.5f);
            splashLogoPos.y = (Screen.height * 0.5f) - (splashLogo.height * 0.5f);
             
            splashLogoPos.width = splashLogo.width;
            splashLogoPos.height = splashLogo.height;
        }
        else
        {
            splashLogoPos.x = 0;
            splashLogoPos.y = 0;
             
            splashLogoPos.width = Screen.width;
            splashLogoPos.height = Screen.height;
        }
         
        if(splashType == SplashType.LoadNextLevelThenFadeOut)
        {
            DontDestroyOnLoad(this);
             
        }
         
        if(Application.levelCount <= 1)
        {
            Debug.LogWarning("Invalid levelToLoad value.");
        }
    }
     
    //開始切換,要跳轉的場景和設置對應的切換速率
    public void StartSplash (int level,int i)
    {
        status = FadeStatus.FadeIn;
        levelToLoadInt=level;
        SetValue(i);
         
    }
    public void setIsDealPlayer(bool isDealPlayer)
    {
        IsDealPlayer=isDealPlayer;
    }
    public void SetValue(int i)
    {
        switch(i)
        {
        case 1:
            fadeSpeed=0.8f;
            waitTime=1.0f;
            break;
        case 2:
            fadeSpeed=0.8f;
            waitTime=1.0f;
            break;
        case 3:
            fadeSpeed=0.8f;
            waitTime=1.0f;
            break;
        }
    }
    void Update ()
    {
 
        switch(status)
        {
        case FadeStatus.FadeIn:
             
            alpha += fadeSpeed*Time.deltaTime;
            break;
        case FadeStatus.FadeWaiting:
            if((!waitForInput && Time.time >= timeFadingInFinished + waitTime)||(waitForInput && Input.anyKey))
            {
                status = FadeStatus.FadeOut;
                 
            }
            break;
        case FadeStatus.FadeOut:
            alpha += -fadeSpeed*Time.deltaTime*2;
            if(alpha<=0.0f)
            {
                if(trigger!=null){
                    trigger();
                }
                 
                status=FadeStatus.Paused;
             
            }
            break;
        }
    }
     
    void OnGUI()
    {
         
        GUI.depth = guiDepth;
        if(splashLogo != null)
        {
            GUI.color = new Color(GUI.color.r,GUI.color.g,GUI.color.b,Mathf.Clamp01(alpha));
            GUI.DrawTexture(splashLogoPos,splashLogo);
        }
        if(alpha > 1.0f)
        {
 
            status = FadeStatus.FadeWaiting;
             
            timeFadingInFinished = Time.time;
            alpha = 1.0f;
            if(splashType == SplashType.LoadNextLevelThenFadeOut)
            {
             
                loadingNextLevel = true;
                if((Application.levelCount) >= 1)
                {
//                  print(levelToLoadInt);
                    Application.LoadLevel(levelToLoadInt);
                    if(triggerAtLoading!=null)
                        triggerAtLoading();
                     
 
                     
                }
            }
        }
        if(alpha < 0.0f)
        {
            if(splashType == SplashType.FadeOutThenLoadNextLevel)
            {
                if(Application.levelCount >= 1)
                {
                    Application.LoadLevel(levelToLoadInt);
                }
            }
            else
            {
 
                 
            }
        }
    }
     
 
     
 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章