找回密码
 立即注册
楼主: easygame

[特效] Unity中的淡入淡出效果

[复制链接]
发表于 2012-12-27 22:55 | 显示全部楼层 |阅读模式

一、问题

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


二、搜索

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

搜关键字"unity3d change scene effect"(学会一点外语是多么的重要啊)

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


还发现一个插件:

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


三、代码

[code=csharp]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 () {
   
    }
}[/code]

这边用的是

Application.LoadLevel

所以,会阻塞,如果是差的机子,还是会卡在你的Fade的画面,如果用的是异步的

  Application.LoadLevelAsync

那就会是把淡入和淡出一起播了。。然后再卡住。。这个可能得再考虑下,主要是异步的加载完成给的值不太准,似乎没法拿来用做完成进度百分比判断。


四、总结

其实我想的效果是在淡出的时间就开始加载,然后再没加载完的时候就卡在Fade的界面,之后再淡入。不过这淡入淡出的效果在好的机子上的效果还是不错的,大家可以试试看。


发表于 2013-4-11 10:13 | 显示全部楼层
代码不会呀{:5_406:}
发表于 2013-6-13 13:37 | 显示全部楼层
插件不能下载吗
发表于 2013-9-2 21:25 | 显示全部楼层
累了。。半天。。为什么一点效果也没有。。。。。{:5_408:}
发表于 2014-5-27 11:09 | 显示全部楼层
谢谢 大神的分享是促使我们进步的动力
发表于 2017-4-9 10:59 | 显示全部楼层
楼主是超人
发表于 2017-4-9 10:43 | 显示全部楼层
好帖就是要顶
发表于 2017-4-9 10:10 | 显示全部楼层
难得一见的好帖
发表于 2017-4-9 11:03 | 显示全部楼层
很好哦
发表于 2017-4-9 10:41 | 显示全部楼层
LZ真是人才
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-4-28 07:05 , Processed in 0.655629 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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