Files
3d-qiaozha/Assets/Scripts/scene2.cs
2026-03-10 10:07:11 +08:00

60 lines
1.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class scene2 : MonoBehaviour
{
// 目标场景名称确保与Build Settings中完全一致
public string targetSceneName = "SampleScene1";
//public string targetSceneName2 = "SampleScene";
public string targetSceneName3 = "SampleSceneche";
public string targetSceneName4 = "SampleSceneshu0930";
void Awake()
{
// 启动时检查并加载场景
LoadSceneIfNotLoaded();
}
public void LoadSceneIfNotLoaded()
{
// 分别检查并加载三个场景
CheckAndLoadScene(targetSceneName);
//CheckAndLoadScene(targetSceneName2);
CheckAndLoadScene(targetSceneName3);
CheckAndLoadScene(targetSceneName4);
}
// 先检查场景是否已加载,未加载则启动异步加载
private void CheckAndLoadScene(string sceneName)
{
// 通过场景名称获取场景信息
Scene scene = SceneManager.GetSceneByName(sceneName);
// 判断场景是否已加载isLoaded为true表示已加载
if (scene.isLoaded)
{
Debug.Log($"场景 {sceneName} 已加载,无需重复加载");
return; // 已加载则直接返回,不执行加载
}
// 未加载则启动异步加载协程
StartCoroutine(LoadSceneAdditive(sceneName));
}
// 异步叠加加载场景
private IEnumerator LoadSceneAdditive(string s)
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(s, LoadSceneMode.Additive);
// 等待加载完成
while (!asyncLoad.isDone)
{
// 可选:添加加载进度日志
// Debug.Log($"加载 {s} 进度:{asyncLoad.progress * 100:F1}%");
yield return null;
}
Debug.Log($"场景 {s} 加载完成");
}
}