Переделывание загрузки сцены

This commit is contained in:
Vladislav Likhenko 2024-08-28 12:18:59 +03:00
parent 88e182c7c8
commit dcbc536143
7 changed files with 36 additions and 1 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: eb2ac3cb50a78dc4cbf5389d3450dc06
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 4890085278179872738
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,3 +1,4 @@
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
@ -5,6 +6,32 @@ public class StartGameScript : MonoBehaviour
{
public void StartGame()
{
SceneManager.LoadSceneAsync(StaticDataMap.SelectedMap);
StartCoroutine(LoadSceneAsync(StaticDataMap.SelectedMap, SceneManager.GetActiveScene().name));
Debug.Log("Çàãðóçêà");
}
private IEnumerator LoadSceneAsync(string newScene, string oldScene)
{
// Çàãðóæàåì íîâóþ ñöåíó àñèíõðîííî
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(newScene, LoadSceneMode.Additive);
// Îæèäàåì, ïîêà íîâàÿ ñöåíà ïîëíîñòüþ çàãðóçèòñÿ
while (!asyncLoad.isDone)
{
yield return null;
}
// Äåëàåì çàãðóæåííóþ ñöåíó àêòèâíîé
SceneManager.SetActiveScene(SceneManager.GetSceneByName(newScene));
// Âûãðóæàåì ñòàðóþ ñöåíó
AsyncOperation asyncUnload = SceneManager.UnloadSceneAsync(oldScene);
// Îæèäàåì, ïîêà ñòàðàÿ ñöåíà ïîëíîñòüþ âûãðóçèòñÿ
while (!asyncUnload.isDone)
{
yield return null;
}
Debug.Log("Ñöåíà " + oldScene + " âûãðóæåíà, à ñöåíà " + newScene + " çàãðóæåíà è àêòèâíà.");
}
}

Binary file not shown.