unity用 AsyncOperation 實現異步加載場景的slider進度條面板

 

public class AsyncLoadScene : BaseUIForm
{
    private const string NAME = "AsyncLoadScene";


    /// 通常的進度條
    public Slider loadingSlider;
    //public Text Tips;
    public Text loadingText;

    public Canvas canvas;

    private int displayProgress;
    private int toProgress;

    AsyncOperation operation;


    private void Start()
    {
        operation = null;
        InitScene();

        if (SceneManager.GetActiveScene().name == "LoadingScene")
        {
            //啓動協程  
            StartCoroutine(AsyncLoading());
        }

    }
    #region 額外的初始化
    protected override void Initialization()
    {
        MsgTypeName = NAME;
        CurrentUIType.UIForms_LucencyType = UIFormLucenyType.DnotMask;
    }

    // 背景圖隨機2選1
    protected override void InitUIInfo()
    {
        int BgSignal=Random.Range(1,3);
        this.SetSprite(Managers.AtlasType.GeneralUI, "LoadScene_bg" + BgSignal);
    }
     

 
    /// 初始化場景
    void InitScene()
    {
        loadingSlider.value = 0;
        loadingSlider.maxValue = 100;

        loadingSlider.gameObject.SetActive(true);
        GetComponent<Image>().color = new Color(1, 1, 1, 1);
        switch (Globe.LoadingSceneKind)
        {
            case LoadingKind.InitGame:
                {
                    m_LaodGameMap.gameObject.SetActive(false);
                }
                break;
            case LoadingKind.BattleScene:
                {
                    m_LaodGameMap.gameObject.SetActive(false);
                }
                break;

        }
    }


    /// 異步加載
    private IEnumerator AsyncLoading()
    {
        TipsInfo.text = Globe.nextSceneName;
        operation = SceneManager.LoadSceneAsync(Globe.nextSceneName);
        operation.allowSceneActivation = false;

            while (operation.progress < 0.9f)
            {
                // 獲取真實的加載進度
                toProgress = (int)(operation.progress * 100);
                // 讓進度條滑動更平滑
                while (displayProgress < toProgress)
                {
                    ++displayProgress;
                    SetLoadingPercentage(displayProgress);
                    yield return new WaitForEndOfFrame();
                }

            }

            toProgress = 100;
            StartCoroutine(LoadScene(operation));
                

    }


    IEnumerator LoadScene(AsyncOperation operation)
    {
        switch (Globe.LoadingSceneKind)
        {
            case LoadingKind.InitGame:
                {
                    while (displayProgress < toProgress)
                    {
                        ++displayProgress;
                        SetLoadingPercentage(displayProgress);
                        yield return new WaitForEndOfFrame();
                    }
                    //operation.allowSceneActivation = true;

                    // 後臺異步加載完成後暫停切換場景,按任何鍵繼續切換
                    StartCoroutine(PauseSwitchScene());
                }
                break;
            case LoadingKind.BattleScene:
                {
                   
                    // 繼續加載剩餘部分
                    while (displayProgress < toProgress)
                    {
                        ++displayProgress;
                        SetLoadingPercentage(displayProgress);
                        //yield return new WaitForEndOfFrame();
                        yield return null;
                    }

                    // 後臺異步加載完成後暫停切換場景,按任何鍵繼續切換
                    StartCoroutine(PauseSwitchScene());
                   
                }
                break;


                    }

                
   }

    public void ShowScene()
    {
        UIFORM.Globe.ShowAllUI();
    }


 



    /// 設置通常的加載信息

    private void SetLoadingPercentage(int Percentage)
    {
        loadingSlider.value = Percentage;
        loadingText.text = Percentage + "%";
    }

    /// 設置遊戲地圖的加載信息
    private void SetLoadingGameMapPercentage(int Percentage)
    {
        LoadingGameMapSlider.value = Percentage;
        LoadingTextGameMap.text = "生成地圖中..."+Percentage + "%";
    }


    /// 後臺異步加載完成後暫停切換場景,按任何鍵繼續切換
    private IEnumerator PauseSwitchScene()
    {
        bool IsGoOn = false;
        while (!IsGoOn)
        {
            if (displayProgress >= 90)
            {
                loadingText.text = "按任意鍵繼續";
                if (Input.anyKeyDown)
                {
                    IsGoOn = true;
                    operation.allowSceneActivation = true;
                }
            }
            yield return null;
        }

    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章