項目中要用到加載場景的時候有個漸入漸出的效果,做了一下,跟大家分享
首先,創建兩個場景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");
}這樣運行,就會出現漸入漸出的效果。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。