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

目录

游戏场景

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

场景跳转

输出当前场景名字

部分基础场景操作

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


游戏场景


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

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


场景跳转

直接打开该场景

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快捷键让当前编辑物体是处于世界坐标系还是相对坐标系下。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值