79 lines
3.1 KiB
C#
79 lines
3.1 KiB
C#
using NEG.UI.Area;
|
|
using NEG.UI.Popup;
|
|
using NEG.UI.UnityUi.Buttons.Reaction;
|
|
using NEG.UI.UnityUi.Buttons.Settings;
|
|
using NEG.UI.UnityUi.Popup;
|
|
using NEG.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Assertions;
|
|
using UnityEngine.SceneManagement;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace NEG.UI.UnityUi
|
|
{
|
|
/// <summary>
|
|
/// Implements ui using UnityUI and Unity Event System with New Input System.
|
|
/// <para>You have to provide prefabs within resources:</para>
|
|
/// <para> - UI/PopupCanvas - prefab with canvas to create popups (will be created on every scene)</para>
|
|
/// <para> - UI/DefaultPopupPrefab - prefab of default popup with 2 options (has to have <see cref="MonoDefaultPopup"/> component)</para>
|
|
/// NEG_UI_DISABLE_WARNING_DEFAULT_SELECTION
|
|
/// </summary>
|
|
public class MonoUiManager : UiManager, IDisposable
|
|
{
|
|
//TODO: use default unity selection
|
|
//TODO: window snaping to slots
|
|
public static new MonoUiManager Instance { get; private set; }
|
|
|
|
public ButtonSettings DefaultUiSettings { get; }
|
|
public KeyBasedFactory<string, ButtonElementBehaviour> BehavioursFactory { get; private set; }
|
|
|
|
//TODO: editor to auto add slots, buttons
|
|
|
|
private readonly MonoDefaultPopup defaultPopupPrefab;
|
|
private readonly GameObject canvasPrefab;
|
|
|
|
private UiInputModule inputModule;
|
|
|
|
public MonoUiManager(IArea startArea, Type inputModuleType, ButtonSettings defaultUiSettings) : base(startArea)
|
|
{
|
|
Instance = this;
|
|
|
|
var popupCanvas = Resources.Load<GameObject>("UI/PopupCanvas");
|
|
var defaultPopup = Resources.Load<GameObject>("UI/DefaultPopupPrefab");
|
|
|
|
Assert.IsNotNull(popupCanvas,"No canvas prefab was provided. Please check MonoUiManager class documentation");
|
|
Assert.IsNotNull(defaultPopup,"No popup prefab was provided. Please check MonoUiManager class documentation");
|
|
Assert.IsNotNull(popupCanvas.GetComponent<Canvas>());
|
|
Assert.IsNotNull(defaultPopup.GetComponent<MonoDefaultPopup>());
|
|
|
|
canvasPrefab = popupCanvas;
|
|
defaultPopupPrefab = defaultPopup.GetComponent<MonoDefaultPopup>();
|
|
|
|
SpawnDefaultPopup();
|
|
|
|
SceneManager.activeSceneChanged += (_, _) => SpawnDefaultPopup();
|
|
|
|
BehavioursFactory = new KeyBasedFactory<string, ButtonElementBehaviour>();
|
|
BehavioursFactory.FireRegistration();
|
|
|
|
inputModule = (UiInputModule)Activator.CreateInstance(inputModuleType);
|
|
DefaultUiSettings = defaultUiSettings;
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
base.Dispose();
|
|
Instance = null;
|
|
}
|
|
|
|
private void SpawnDefaultPopup()
|
|
{
|
|
var canvas = Object.Instantiate(canvasPrefab);
|
|
canvas.name = "DefaultPopupCanvas";
|
|
SetDefaultPopup(Object.Instantiate(defaultPopupPrefab, canvas.transform));
|
|
currentDefaultPopup.Close(true);
|
|
}
|
|
}
|
|
} |