Unity中的淡入淡出效果
一、问题想要在场景的转换做个过渡,不想直接的跳转。最简单的就是做个淡入淡出的效果。二、搜索百度基本是不指望了,资料太少,所以要用google,并且英文搜索。搜关键字"unity3d change scene effect"(学会一点外语是多么的重要啊)http://answers.unity3d.com/questions/8237/changing-scenes.htmlhttp://answers.unity3d.com/questions/15289/can-you-create-transitions-between-levels-using-ap.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的界面,之后再淡入。不过这淡入淡出的效果在好的机子上的效果还是不错的,大家可以试试看。
代码不会呀{:5_406:} 插件不能下载吗 累了。。半天。。为什么一点效果也没有。。。。。{:5_408:} 谢谢 大神的分享是促使我们进步的动力 楼主是超人 好帖就是要顶 难得一见的好帖 很好哦 LZ真是人才
页:
[1]