项目中要用到加载场景的时候有个渐入渐出的效果,做了一下,跟大家分享
首先,创建两个场景Main和Game场景;
其次,在Main场景中创建FandeScene.cs脚本,创建Fade空对象,挂载,给一张黑色的图片,拖成预设体,同样也拖到Game场景中。
using UnityEngine;
using System.Collections;
public class FadeScene : MonoBehaviour {
public Texture blackTexture;
private float alpha = 1.0f;
public float fadespeed = 0.2f;
private int fadeDir = -1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
alpha += fadeDir * fadespeed * Time.deltaTime;
GUI.color = new Color (GUI.color .r ,GUI.color .g ,GUI.color .b,alpha);
GUI.DrawTexture (new Rect (0,0,Screen .width ,Screen .height), blackTexture);
}
public float BeginFade(int direction)
{
fadeDir = direction;
return 1 / fadespeed;
}
void OnLevelWasLoaded()
{
Debug.Log ("场景加载完毕!");
BeginFade (-1);
}
}
再次,加载场景
协程加载
IEnumerator FadeLoadScene()
{
float time = GameObject.Find ("Fade").GetComponent <FadeScene> ().BeginFade (1);
yield return new WaitForSeconds (time);
SceneManager.LoadSceneAsync ("Game");
}
这样运行,就会出现渐入渐出的效果。