Unity学习日记16(场景跳转、异步加载)

目录

游戏场景

 添加要加载的场景/场景序号

场景跳转

输出当前场景名字

部分基础场景操作

异步加载场景/获取加载进度/演示跳转


游戏场景


 添加要加载的场景/场景序号

将需要加载的场景拖拽进去

扫描二维码关注公众号,回复: 14621014 查看本文章


场景跳转

直接打开该场景

using UnityEngine.SceneManagement;
……………………
    void Start()
    {
        SceneManager.LoadScene("SampleScene");
    }

打开一个场景和附加打开场景

附加打开会将两个场景中的物体叠加在一起

        //单个打开
        SceneManager.LoadScene("SampleScene", LoadSceneMode.Single);
        //附加打开
        SceneManager.LoadScene("SampleScene", LoadSceneMode.Additive);

输出当前场景名字

        Scene scene = SceneManager.GetActiveScene();
        Debug.Log(scene.name);

部分基础场景操作

        //获取当前场景名称
        Scene scene = SceneManager.GetActiveScene();
        Debug.Log(scene.name);
        //场景是否已经加载
        Debug.Log(scene.isLoaded);
        //路径
        Debug.Log(scene.path);
        //索引
        Debug.Log(scene.buildIndex);

        //返回场景的对象数量
        GameObject[] gos = scene.GetRootGameObjects();
        Debug.Log(gos.Length);

        //创建新场景
       Scene newS= SceneManager.CreateScene("123");

        //卸载新场景
        SceneManager.UnloadSceneAsync(newS);

        //场景管理类:检测多少已加载的活动场景
        Debug.Log(SceneManager.sceneCount);

异步加载场景/获取加载进度/演示跳转

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

public class Async : MonoBehaviour
{
    AsyncOperation operation;//场景异步加载
    float Timer=0;
    void Start()
    {
        StartCoroutine(LoadScene());//开启一个协程
    }

    IEnumerator LoadScene()
    {
        operation = SceneManager.LoadSceneAsync("SampleScene");//指定加载的场景,可以是数字,加载场景的序号
        operation.allowSceneActivation = false;//不允许场景自动跳转
        yield return operation;

    }
    void Update()
    {
        Debug.Log("加载进度:"+operation.progress);//加载进度(0~0.9)
        if (operation.progress >= 0.89)
        {
            Debug.Log("场景已加载完毕");
        }
        Timer += Time.deltaTime;            //计数器累加
        if (Timer > 5)                      //5秒后跳转
            operation.allowSceneActivation = true;
    }
}

其他

按X快捷键让当前编辑物体是处于世界坐标系还是相对坐标系下。

猜你喜欢

转载自blog.csdn.net/weixin_56537692/article/details/129764822