Unity中的淡入淡出效果


一、問題

想要在場景的轉換做個過渡,不想直接的跳轉。最簡單的就是做個淡入淡出的效果。

 

二、搜索

百度基本是不指望了,資料太少,所以要用google,並且英文搜索。

搜關鍵字"unity3d change scene effect"(學會一點外語是多么的重要啊)

http://answers.unity3d.com/questions/8237/changing-scenes.html

還發現一個插件:

http://gameprefabs.com/products/preview/107

 

三、代碼

using UnityEngine;
using System.Collections;

public class LevelLoadFade : MonoBehaviour 
{
    public static void FadeAndLoadLevel(string levelName, Texture2D fadeTexture, float fadeLength)
    {
        if (null == fadeTexture)
        {
            FadeAndLoadLevel(levelName, Color.white, fadeLength);
        }
        
        GameObject fade = new GameObject("Fade");
        fade.AddComponent<LevelLoadFade>();
        
        fade.GetComponent<LevelLoadFade>().DoFade(levelName, fadeLength, fadeTexture, Color.white, false);            
    }
    
    public static void FadeAndLoadLevel(string levelName, Color color, float fadeLength)
    {
        color.a = 1;
        Texture2D fadeTexture = new Texture2D(1, 1);
        fadeTexture.SetPixel(0, 0, color);
        fadeTexture.Apply();
        DontDestroyOnLoad(fadeTexture);
        
        GameObject fade = new GameObject("Fade");
        fade.AddComponent<LevelLoadFade>();
        fade.GetComponent<LevelLoadFade>().DoFade(levelName, fadeLength, fadeTexture, color, true);
    }
    
    private Texture2D     m_FadeTexture;
    private Rect         m_Rect;
    private Color         m_Color;

    
    void Awake()
    {
        m_Rect = new Rect(0, 0, Screen.width, Screen.height);
        m_FadeTexture = null;
    }
    
    
    void OnGUI()
    {
        if (m_FadeTexture != null)
        {
            GUI.depth = -100;
            GUI.color = m_Color;
            GUI.DrawTexture(m_Rect, m_FadeTexture);
        }
    }
    
    void DoFade(string levelName, float fadeLength, 
        Texture2D fadeTexture, Color color, bool destroyTexture)
    {
        transform.position = Vector3.zero;
        // Don't destroy the fade game object during level load
        DontDestroyOnLoad(gameObject);
        m_Color = color;
        m_FadeTexture = fadeTexture;
    
        StartCoroutine(DoCoroutineFade(levelName, fadeLength, fadeTexture, color, destroyTexture));
    }
    
    IEnumerator DoCoroutineFade(string levelName, float fadeLength,  Texture2D fadeTexture, Color color, bool destroyTexture)
    {
        // Fade texture in
        float time = 0;
        while (time < fadeLength)
        {
            time += Time.deltaTime;
            m_Color.a = Mathf.InverseLerp(0, 1, time/fadeLength);
            yield return null;
        }
        
        // Fadeout to start with
        color.a = 1;
        
        Application.LoadLevel(levelName);
        
        // Fade texture out
        time = 0;
        while (time < fadeLength)
        {
            time += Time.deltaTime;
            m_Color.a = Mathf.InverseLerp(1, 0, time/fadeLength);
            yield return null;
        }
        
        m_Color.a = 0;
        
        m_FadeTexture = null;
        Destroy(gameObject);
        
        if (destroyTexture)
        {
            Destroy(m_FadeTexture);
        }
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}

 

這邊用的是

Application.LoadLevel

所以,會阻塞,如果是差的機子,還是會卡在你的Fade的畫面,如果用的是異步的

  Application.LoadLevelAsync

那就會是把淡入和淡出一起播了。。然后再卡住。。這個可能得再考慮下,主要是異步的加載完成給的值不太准,似乎沒法拿來用做完成進度百分比判斷。

 

四、總結

其實我想的效果是在淡出的時間就開始加載,然后再沒加載完的時候就卡在Fade的界面,之后再淡入。不過這淡入淡出的效果在好的機子上的效果還是不錯的,大家可以試試看。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM