Files
2025-05-29 22:31:40 +03:00

155 lines
4.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadScene : MonoBehaviour
{
[SerializeField] private string sceneName;
[SerializeField] string[] additionalScenes;
public bool loadAdditiveSceneOnAwake;
private void Awake()
{
if (loadAdditiveSceneOnAwake)
{
LoadAdditiveScene();
}
}
public void LoadSceneSimpleMode()
{
SceneManager.LoadScene(sceneName);
}
public void LoadAdditiveScene()
{
SceneManager.LoadScene(sceneName, LoadSceneMode.Additive);
}
public void LoadSceneAsyncAdditive()
{
//SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
StartCoroutine(LoadSceneAsyncAdditiveEnumerator());
}
private IEnumerator LoadSceneAsyncAdditiveEnumerator()
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
// Wait until the new scene is fully loaded
while (!asyncLoad.isDone)
{
yield return null;
}
//// Unload the current scene if needed
//SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene().buildIndex);
}
public void LoadMainAndAdditiveScenesAsync()
{
//StartCoroutine(LoadMainAndAdditiveScenesAsyncEnumerator());
LoadManager.GetInstance().LoadMainAndAdditiveScenesAsync(sceneName, additionalScenes);
}
public void LoadMainAndAdditiveScenesSynchronously()
{
// LoadMainAndAdditiveScenesTogether(sceneName, additionalScenes);
StartCoroutine(LoadScene.LoadMainAndAdditiveScenesTogetherCoroutine(sceneName, additionalScenes));
}
public static void LoadMainAndAdditiveScenesTogether(string mainSceneName, string[] additionalScenes)
{
//// Load the main scene
////SceneManager.LoadScene(mainSceneName);
//AsyncOperation loadMainSceneOperation = SceneManager.LoadSceneAsync(mainSceneName);
//while (!loadMainSceneOperation.isDone)
//{
// float progressValue = Mathf.Clamp01(loadMainSceneOperation.progress / 0.9f);
// yield return null;
//}
//// Load additional scenes additively synchronously
//foreach (string AdditionalSceneName in additionalScenes)
//{
// SceneManager.LoadScene(AdditionalSceneName, LoadSceneMode.Additive);
// // Call a method or perform actions to handle the completion of loading each additional scene
// HandleSceneLoaded(AdditionalSceneName);
//}
//// Call a method or perform actions to handle the completion of loading the main scene
//HandleSceneLoaded(mainSceneName);
//Debug.Log("All scenes are loaded!");
}
public static IEnumerator LoadMainAndAdditiveScenesTogetherCoroutine(string mainSceneName, string[] additionalScenes)
{
// Load the main scene
//SceneManager.LoadScene(mainSceneName);
AsyncOperation loadMainSceneOperation = SceneManager.LoadSceneAsync(mainSceneName);
while (!loadMainSceneOperation.isDone)
{
float progressValue = Mathf.Clamp01(loadMainSceneOperation.progress / 0.9f);
yield return null;
}
// Load additional scenes additively synchronously
foreach (string AdditionalSceneName in additionalScenes)
{
AsyncOperation loadAdditiveScenesOperation = SceneManager.LoadSceneAsync(AdditionalSceneName);
//SceneManager.LoadScene(AdditionalSceneName, LoadSceneMode.Additive);
// Call a method or perform actions to handle the completion of loading each additional scene
HandleSceneLoaded(AdditionalSceneName);
// Wait for the additional scene to finish loading
yield return loadAdditiveScenesOperation;
}
// Call a method or perform actions to handle the completion of loading the main scene
HandleSceneLoaded(mainSceneName);
Debug.Log("All scenes are loaded!");
}
// Example method to handle the completion of loading a scene
private static void HandleSceneLoaded(string sceneName)
{
Debug.Log(sceneName + " is loaded!");
// Add any additional logic you want to perform when a scene is loaded
}
private IEnumerator LoadMainAndAdditiveScenesAsyncEnumerator()
{
yield return StartCoroutine(AsyncLoader.LoadMainAndAdditiveScenesAsync(sceneName, additionalScenes));
}
public void LoadSceneAsync()
{
StartCoroutine(LoadSceneAsyncEnumerator());
}
private IEnumerator LoadSceneAsyncEnumerator()
{
yield return StartCoroutine(AsyncLoader.LoadSceneAsync(sceneName, OnProgress, OnComplete));
}
private void OnProgress(float progress)
{
Debug.Log("Loading progress: " + (progress * 100) + "%");
}
private void OnComplete()
{
Debug.Log("Scene loaded!");
}
}