Unity(第十一部)场景

发布于:2024-03-01 ⋅ 阅读:(67) ⋅ 点赞:(0)

游戏有多个场景组成(新手村,某某副本,主城)
场景是有多个物体组成(怪物,地形,玩家等)
物体是有多个组件组成(刚体组件,自定义脚本)

在这里插入图片描述

创建场景

在这里插入图片描述
编辑场景
在这里插入图片描述
在另一个场景加载自创建的场景
在这里插入图片描述
在这里插入图片描述

做好准备工作,开始写场景类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;//场景类需要导入

public class game : MonoBehaviour
{
 

	void Start()
	{
		//两个类 场景类,场景管理类
		//通过索引或者索引名称跳转
		// SceneManager.LoadScene(1);
		// SceneManager.LoadScene(0);
		//SceneManager.LoadScene("SampleScene");
		//SceneManager.LoadScene("myscenes");
		//获取当前场景
		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);
 
		SceneManager.LoadScene("myscenes", LoadSceneMode.Additive);
	}


	void Update()
	{
		 
	}
}

开始了解场景管理类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;//场景类需要导入

public class game : MonoBehaviour
{
 

	void Start()
	{
		//两个类 场景类,场景管理类
		 

		//场景管理类

		//创建新场景
		Scene newScene = SceneManager.CreateScene("newScene");
        //加载的场景数量
        Debug.Log(SceneManager.sceneCount);
        //卸载场景
        SceneManager.UnloadSceneAsync(newScene);
		//加载场景替换场景(替换当前场景,和跳转类型)
		SceneManager.LoadScene("myscenes", LoadSceneMode.Single);
		//加载场景添加加载(两个场景会叠加在一起,会卡顿)
		SceneManager.LoadScene("myscenes", LoadSceneMode.Additive);
		
	}


	void Update()
	{
		 
	}
}

异步加载场景添加加载

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;//场景类需要导入

public class game : MonoBehaviour
{
	//声明一下 operation
	AsyncOperation operation;
	//跳转时间
	float timer = 0;
	void Start()
	{

		//场景管理类
		StartCoroutine(loadScene());
	}
	//创建协程方法用来异步记载场景 
	IEnumerator loadScene() {
		operation = SceneManager.LoadSceneAsync(0);
		//加载完成后不要跳转
		operation.allowSceneActivation = false;
		yield return operation;

	}

	void Update()
	{
		//读取加载进度,进度最大0-0.9
		Debug.Log(operation.progress);

		timer += Time.deltaTime;//上一帧到这一帧所用的游戏时间//Debug.Log(Time.deltaTime);//如果大于3秒
		if (timer > 5)
		{
			operation.allowSceneActivation = true;
		}
	 
	}
}

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到