U3DC.COM | 优三帝研究院

Menu

场景切换的淡入淡出

两个场景需要跳转,如果直接跳过去,那么就会显得很生硬,使用一个过渡,就会使整体切换显得很自然。

using UnityEngine;
using System.Collections;
/*
 * 使用方法:将该脚本附在镜头下面,然后切换场景时镜头不销毁
 * 要调用的时候获得该脚本,然后调用StartSplash()
 */
public class SceneLoad : MonoBehaviour {
    
    public int guiDepth = 0;
    
//    private 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(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=1f;
            waitTime=0.5f;
            break;
        case 2:
            fadeSpeed=1.5f;
            waitTime=0.8f;
            break;
        case 3:
            fadeSpeed=2.5f;
            waitTime=0.5f;
            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
            {
                
                
            }
        }
    }
    
    
    
    
}

 

打赏
— 于 共写了4898个字
— 文内使用到的标签:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据