溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Unity實現(xiàn)場景加載漸入漸出效果

發(fā)布時間:2020-04-03 03:59:23 來源:網(wǎng)絡(luò) 閱讀:793 作者:速度速度撒 欄目:開發(fā)技術(shù)

項目中要用到加載場景的時候有個漸入漸出的效果,做了一下,跟大家分享

首先,創(chuàng)建兩個場景Main和Game場景;

其次,在Main場景中創(chuàng)建FandeScene.cs腳本,創(chuàng)建Fade空對象,掛載,給一張黑色的圖片,拖成預(yù)設(shè)體,同樣也拖到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);  
    }

}

再次,加載場景

協(xié)程加載
    IEnumerator FadeLoadScene()
    {
        float time = GameObject.Find ("Fade").GetComponent <FadeScene> ().BeginFade (1);
        yield return new WaitForSeconds (time);
        SceneManager.LoadSceneAsync ("Game");
    }

這樣運行,就會出現(xiàn)漸入漸出的效果。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI