using System;
namespace Gravitons.UI.Modal
{
using UnityEngine;
public class ModalManager : MonoBehaviour
{
[Tooltip("The modal database reference, all modals will be retrieved from this database")]
[SerializeField] protected ModalDatabase m_ModalDatabase;
[Tooltip("The identifier of the default modal")]
[SerializeField] protected string m_DefaultIdentifier = "Default";
private static ModalManager s_Instance;
private static ModalManager Instance
{
get { return s_Instance; }
}
public void OnEnable()
{
s_Instance = this;
}
private void OnDisable()
{
s_Instance = null;
}
///
/// Shows a generic (default) modal
///
/// Modal title
/// Modal body
/// Modal button properties
/// The modal instantiated
public static Modal Show(string title, string body, ModalButton[] buttons)
{
return Instance.InternalShow(Instance.m_DefaultIdentifier, new GenericModalContent(){ Title = title, Body = body} , buttons);
}
///
/// Shows a modal with the specified identifier
///
/// The identifier of the modal to show
/// The content to show in the modal
/// Modal button properties
/// The modal instantiated
public static Modal Show(string identifier, ModalContentBase modalContent, ModalButton[] modalButton)
{
return Instance.InternalShow(identifier, modalContent, modalButton);
}
///
/// Shows a generic (default) modal
///
/// The identifier of the modal to show
/// The content to show in the modal
/// Modal button properties
/// The modal instantiated
private Modal InternalShow(string identifier, ModalContentBase modalContent, ModalButton[] modalButton)
{
GameObject modalObj = m_ModalDatabase.GetModal(identifier);
if (modalObj == null)
{
Debug.LogError("Error! Failed to get a modal prefab with the identifier: " + identifier + ". Ensure that the identifier matches the one in the database");
return null;
}
GameObject clone = Instantiate(modalObj, transform);
var modal = clone.GetComponent();
modal.Show(modalContent, modalButton);
return modal;
}
#if UNITY_2019_3_OR_NEWER
///
/// Reset the static variables for domain reloading.
///
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void DomainReset()
{
s_Instance = null;
}
#endif
}
}