unity加载场景和进度条

假设有大厅,加载场景和游戏场景这三个场景,下面这个脚本是在大厅放置

using Newtonsoft.Json;
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public enum SceneIndex
{
    HallScene,
    LoadScene,
    GameScene,
}

public class Main : MonoBehaviour
{
    public static void RunLoginScene()
    {
        Instance.LoadNextScene(SceneIndex.IndexScene);
        Instance.SceneLoadedAction = delegate
        {
            // 这里能放在加载完成后到下一个场景之前要做的事,比如给Canvas添加脚本
            //GameObject.Find("Canvas").AddComponent();
        };
    }

    public static void RunHallScene()
    {
        //sceneMgr.LoadSceneMC(SceneIndex.HallScene);

        Instance.LoadNextScene(SceneIndex.HallScene);

        Instance.SceneLoadedAction = delegate
        {
            //  同上
        };
    }

    SceneIndex nextSceneIndex;

    void LoadNextScene(SceneIndex index)
    {
        this.nextSceneIndex = index;
        //this.OnSceneLoaded = loadedAction;
        SceneManager.LoadScene("666.LoadScene"); // 加载场景的名字
        SceneManager.sceneLoaded += SceneManager_SceneLoaded;
    }

    void SceneManager_SceneLoaded(Scene scene, LoadSceneMode mode)
    {
        SceneManager.sceneLoaded -= SceneManager_SceneLoaded;

        LoadScene loadScene = GameObject.Find("Canvas").AddComponent<LoadScene>(); // 给加载场景添加加载脚本
        loadScene.nextSceneIndex = this.nextSceneIndex;
    }


    Action SceneLoadedAction;
    public static void SceneLoaded()
    {
        Instance.StartCoroutine(Instance.loaded());
    }

    IEnumerator loaded()
    {
        yield return null; // 一帧之后执行委托,中间留出一帧的时间来添加脚本
        Instance.SceneLoadedAction?.Invoke();
    }

}

这个脚本可以不用动直接拿来用,改下场景名字就行了,然后接下来是加载的脚本
加载场景里没啥东西,就是一个slider进度条

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LoadScene : MonoBehaviour
{
    //显示进度的文本
    Text TextProgress;
    //进度条
    Slider slider;
    int currProgress; // 当前进度显示
    AsyncOperation async;
    public SceneIndex nextSceneIndex;
    public Action LoadedAction;

    void Start()
    {
        this.slider = transform.Find("SliderLoadProcess").GetComponent<Slider>();
        this.TextProgress = transform.Find("TextProgress").GetComponent<Text>();
        currProgress = 0;
        async = null;
        StartCoroutine(LoadAsync());
    }

    IEnumerator LoadAsync()
    {
        async = SceneManager.LoadSceneAsync((int)this.nextSceneIndex); // 游戏场景的名字或者场景代号
        async.allowSceneActivation = false; // 不激活场景转换 直到加载完毕. 这个地方是为了防止场景直接跳转
        yield return async;
    }

    void Update()
    {
        if (async == null)
            return;

        int toProcess;
        if (async.progress < 0.9f)
        {
            toProcess = (int)(async.progress * 100);//因为async是0-1的小数  
        }
        else //当progress大于0.9时,直接给toProcess赋值100,因为这个值不可能到达100  
        {
            toProcess = 100;
        }
        if (this.currProgress < toProcess)
        {
            this.currProgress++;
            this.TextProgress.text = "资源加载中 " + this.currProgress + "%";
        }
        slider.value = this.currProgress / 100f;
        if (this.currProgress == 100)
        {
            UnityEngine.Debug.Log("加载完毕...");
            async.allowSceneActivation = true;
            Main.SceneLoaded();// 这里执行main脚本里的委托,加载完毕之后留出一帧时间来加载脚本或者做其他的,这个地方如果不需要预操作也可以省去这一步
        }
    }

}

你可能感兴趣的:(unity加载场景和进度条)