Unity的场景加载

unity3d 场景加载

unity 场景加载分为同步加载和异步加载。

同步加载 loadScene

首先将前置工作做好。。。。
创建一个项目工程,然后创建三个场景 loading00、loading01、loading02。每个场景分别创建一个cube、Sphere、Capsule 。然后打开File -> Build Settings, 然后将创建的 loading00、loading01、loading02拖拽到Scenes In Build中。如下图:
请添加图片描述
然后再创建一个 loading.cs 脚本,然后写上这段代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class loading : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
    	// 同步加载场景
        SceneManager.LoadScene(1, LoadSceneMode.Additive);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

SceneManager.LoadScene(1, LoadSceneMode.Additive);
第一个参数是表示加载的场景序号,第二个参数是 加载场景后,是否删除旧场景。

场景序号就是在 BuildSettings 中的序号如下图:
请添加图片描述
当然,第一个参数也可以写场景的路径:
SceneManager.LoadScene(“Scenes/loading01”, LoadSceneMode.Additive);

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class loading : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
    	// 同步加载场景
        SceneManager.LoadScene("Scenes/loading01", LoadSceneMode.Additive);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

与上述代码想过是一致的。

异步加载

异步加载需要先介 AsyncOperation 类

SceneManager.LoadSceneAsync(1, LoadSceneMode.Additive) 这个是异步加载的函数,其参数的用法跟同步加载的用法一致,然后,返回值 是一个 As yncOperation 类。

AsyncOperation 的用处:

  • AsyncOperation.isDone 这个是用来判断加载是否完成。这个属性是加载完并且跳转成功后才会变成完成
  • AsyncOperation.allowSceneActivation 这个是加载完后,是否允许跳转,当为false时,即使场景加载完了,也不会跳转
  • AsyncOperation.progress 这个时表示加载场景的进度。实际上的值时 0 - 0.9, 当值为0.9的时候,场景就已经加载完成了。

我们要异步加载场景的话,一般都是需要用协程一起工作
代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class loading : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(Load());
    }


    IEnumerator Load()
    {

        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(1, LoadSceneMode.Additive);

        asyncOperation.allowSceneActivation = false;    // 这里限制了跳转


        // 这里就是循环输入进度
        while(asyncOperation.progress < 0.9f)
        {
            Debug.Log(" progress = " + asyncOperation.progress);
        }

        asyncOperation.allowSceneActivation = true;    // 这里打开限制
        yield return null;

        if(asyncOperation.isDone)
        {
            Debug.Log("完成加载");
        }
    }

    // Update is called once per frame
    void Update()
    {
      
    }
}

异步和同步的区别

异步不会阻塞线程,可以在加载过程中继续去执行其他的一些代码,(比如同时加载进度)而同步会阻塞线程,只有加载完毕显示后才能继续执行之后的一些代码。

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值