zoukankan      html  css  js  c++  java
  • Unity中场景异步加载

    引入命名空间

    using UnityEngine.UI;

    using UnityEngine.SceneManagement;

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI; //引入命名空间
    using UnityEngine.SceneManagement;//引入命名空间
    
    public class S2Manager : MonoBehaviour
    {
    //UI进度条
        private Slider _proSlider;  //滑动条
    //目的是对场景进行控制 获取进度值 和允许显示
        private AsyncOperation _async;
    //UI应该达到的进度
        private int _currProgress;
        //1. 获取滑动条
        //协同加载(异步加载 不断获取进度值 经过计算赋值给滑动条)
        // Use this for initialization
        void Start ()
        {
            _currProgress = 0;
            _async = null;
            _proSlider = GameObject.Find("Slider").GetComponent<Slider>();
            StartCoroutine("LoadScene");
        }
        
        // Update is called once per frame
        void Update ()
        {
            //目的就是现实进度
            _proSlider.value = _currProgress / 100.0f;
        }
    
        IEnumerator LoadScene()
        {
           //临时的进度
            int tmp;
            //异步加载
            _async = SceneManager.LoadSceneAsync("S3");  //跳转场景为S3
    
            //先不显示场景 等到进度为100%的时候显示场景 必须的!!!!
            _async.allowSceneActivation = false;
           #region 优化进度的 
            while (_async.progress < 0.9f)
            {
                //相当于滑动条应该到的位置
                tmp = (int) _async.progress * 100;
                
                //当滑动条 < tmp 就意味着滑动条应该变化
                while (_currProgress < tmp)
                {
                    ++_currProgress;
                    yield return  new WaitForEndOfFrame();
                }
            }//while end   进度为90%
    
            tmp = 100;
            while (_currProgress < tmp)
            {
    
                ++_currProgress;
                yield return  new WaitForEndOfFrame();
            }
            #endregion
            //处理进度为0 ~0.9的0
            
            //进度条完成 允许显示
            _async.allowSceneActivation = true;
            
        }
    }

    同步和异步:

    1. 同步直接怼过来 (若机器low或场景大 就会卡)
    2. 异步 直接怼到一个中间场景(过度场景(显示进度条)) --> 到场景S3
    3. 在异步中的两个while循环没啥大作用, 目的就是优化进度条的!!!

  • 相关阅读:
    POJ1201 Intervals & TYVJ 1589 西瓜种植 差分约束
    洛谷 P3385 【模板】负环 SPFA
    Test 2018-07-19 二中集训
    《一出好戏》一中高三学子誓师大会后记
    [NOI2012] 迷失游乐园 概率 期望 基环树DP
    codeforces CF983E NN country 树上倍增
    「Luogu P3521 [POI2011]ROT-Tree Rotations」
    「高等数学学习笔记 DAY18」
    「高等数学学习笔记 DAY17」
    「高等数学学习笔记 DAY16」
  • 原文地址:https://www.cnblogs.com/lk95/p/9885612.html
Copyright © 2011-2022 走看看