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;
        }

    }
}

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