一、问题

想要在场景的转换做个过渡,不想直接的跳转。最简单的就是做个淡入淡出的效果。

 

二、搜索

百度基本是不指望了,资料太少,所以要用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的界面,之后再淡入。不过这淡入淡出的效果在好的机子上的效果还是不错的,大家可以试试看。