using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;
///
/// Allows a "cold start" in the editor, when pressing Play and not passing from the Initialisation scene.
///
public class EditorColdStartup : MonoBehaviour
{
#if UNITY_EDITOR
[SerializeField] private GameSceneSO _thisSceneSO = default;
[SerializeField] private GameSceneSO _persistentManagersSO = default;
[SerializeField] private AssetReference _loadSceneEventChannel = default;
private void Start()
{
if (!SceneManager.GetSceneByName(_persistentManagersSO.sceneReference.editorAsset.name).isLoaded)
{
_persistentManagersSO.sceneReference.LoadSceneAsync(LoadSceneMode.Additive, true).Completed += LoadEventChannel;
}
}
private void LoadEventChannel(AsyncOperationHandle obj)
{
_loadSceneEventChannel.LoadAssetAsync().Completed += ReloadScene;
}
private void ReloadScene(AsyncOperationHandle obj)
{
LoadEventChannelSO loadEventChannelSO = (LoadEventChannelSO)_loadSceneEventChannel.Asset;
loadEventChannelSO.RaiseEvent(_thisSceneSO);
SceneManager.UnloadSceneAsync(_thisSceneSO.sceneReference.editorAsset.name);
}
#endif
}