欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

unity异步加载场景

程序员文章站 2022-04-03 12:28:39
...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public enum Zhuan_huan
{
    one,
    two
}
public class Head_trigger : MonoBehaviour {
    public Zhuan_huan zhuan;
    public GameObject fogdemo;

    private bool one_xunhuan;
    private AsyncOperation _async;
    private int _nowProgress;
    // Use this for initialization
    void Start () {
     
       
	}
	
	// Update is called once per frame
	void Update () {
        if (_async == null)
        {
            return;
        }
        int toProgress;
        if (_async.progress < 0.9f)
        {
            toProgress = (int)_async.progress * 100;
        }
        else
        {
            toProgress = 100;
        }
        if (_nowProgress < toProgress)
        {
            _nowProgress++;
        }
        Debug.Log("现在的进度是:" + _nowProgress + "%");
        if (_nowProgress == 100)
        {
            _async.allowSceneActivation = true;
        }

    }
    private void OnTriggerEnter(Collider other)
    {
        if (one_xunhuan == false)
        {
            if (other.name == "传送门")
            {
                StartCoroutine(Ynsshi());
                fogdemo.SetActive(true);
            }
            one_xunhuan = true;
        }
        
    }
    IEnumerator Ynsshi()
    {
        yield return new WaitForSeconds(3);
        if (zhuan == Zhuan_huan.one)
        {
            //SceneManager.LoadScene(1);
            _async = SceneManager.LoadSceneAsync("modo2");
            _async.allowSceneActivation = false;
            yield return _async;
        }
        if (zhuan == Zhuan_huan.two)
        {
            //SceneManager.LoadScene(0);

            _async = SceneManager.LoadSceneAsync("modo");
            _async.allowSceneActivation = false;
            yield return _async;
        }
        
        
    }
}

原网址:https://blog.csdn.net/yy763496668/article/details/78182247  (写的挺好)