码迷,mamicode.com
首页 > 编程语言 > 详细

Unity中场景异步加载

时间:2018-10-31 20:51:17      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:位置   ogr   引入   get   engine   unit   game   bsp   ons   

引入命名空间

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循环没啥大作用, 目的就是优化进度条的!!!

 

Unity中场景异步加载

标签:位置   ogr   引入   get   engine   unit   game   bsp   ons   

原文地址:https://www.cnblogs.com/lk95/p/9885612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!