Neg_Utils/NEG/UI/UnityUi/MonoUiManager.cs

49 lines
2.0 KiB
C#

using NEG.UI.Area;
using NEG.UI.Popup;
using NEG.UI.UnityUi.Popup;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Assertions;
using UnityEngine.SceneManagement;
namespace NEG.UI.UnityUi
{
/// <summary>
/// Implements ui using UnityUI and Unity Event System with New Input System.
/// <para>You have to provide prefabs with addresses:</para>
/// <para> - NEG/UI/PopupCanvas - prefab with canvas to create popups (will be created on every scene)</para>
/// <para> - NEG/UI/DefaultPopupPrefab - prefab of default popup with 2 options (has to have <see cref="MonoDefaultPopup"/> component)</para>
/// </summary>
public class MonoUiManager : UiManager
{
private readonly MonoDefaultPopup defaultPopupPrefab;
private readonly GameObject canvasPrefab;
public MonoUiManager(IArea startArea) : base(startArea)
{
var prefabs =
Addressables.LoadAssetsAsync<GameObject>(new List<string>() { "NEG/UI/PopupCanvas", "NEG/UI/DefaultPopupPrefab" }, (_) => { }, Addressables.MergeMode.Union).WaitForCompletion();
Assert.AreEqual(prefabs.Count, 2, "No prefabs was provided. Please check MonoUiManager class documentation");
Assert.IsNotNull(prefabs[0].GetComponent<Canvas>());
Assert.IsNotNull(prefabs[1].GetComponent<MonoDefaultPopup>());
canvasPrefab = prefabs[0];
defaultPopupPrefab = prefabs[1].GetComponent<MonoDefaultPopup>();
SpawnDefaultPopup();
SceneManager.activeSceneChanged += (_, _) => SpawnDefaultPopup();
}
private void SpawnDefaultPopup()
{
var canvas = Object.Instantiate(canvasPrefab);
canvas.name = "DefaultPopupCanvas";
SetDefaultPopup(Object.Instantiate(defaultPopupPrefab, canvas.transform));
currentDefaultPopup.Close(true);
}
}
}