using System.Collections.Generic; using UnityEngine; public class PanelManager : Singleton { /// /// This is going to hold all of our instances /// private List _panelInstanceModels = new List(); /// /// Pool of panels /// private ObjectPool _objectPool; private void Start() { // Cache the ObjectPool _objectPool = ObjectPool.Instance; } public void ShowPanel(string panelId, PanelShowBehaviour behaviour = PanelShowBehaviour.KEEP_PREVIOUS) { // Get a panel instance from the ObjectPool GameObject panelInstance = _objectPool.GetObjectFromPool(panelId); // If we have one if (panelInstance != null) { // If we should hide the previous panel, and we have one if (behaviour == PanelShowBehaviour.HIDE_PREVIOUS && GetAmountPanelsInQueue() > 0) { // Get the last panel var lastPanel = GetLastPanel(); // Disable it lastPanel?.PanelInstance.SetActive(false); } // Add this new panel to the queue _panelInstanceModels.Add(new PanelInstanceModel { PanelId = panelId, PanelInstance = panelInstance }); } else { Debug.LogWarning($"Trying to use panelId = {panelId}, but this is not found in the ObjectPool"); } } public void HideLastPanel() { // Make sure we do have a panel showing if (AnyPanelShowing()) { // Get the last panel showing var lastPanel = GetLastPanel(); // Remove it from the list of instances _panelInstanceModels.Remove(lastPanel); // Pool the object _objectPool.PoolObject(lastPanel.PanelInstance); // If we have more panels in the queue if (GetAmountPanelsInQueue() > 0) { lastPanel = GetLastPanel(); if (lastPanel != null && !lastPanel.PanelInstance.activeInHierarchy) { lastPanel.PanelInstance.SetActive(true); } } } } /// /// Returns the last panel in the queue /// /// The last panel in the queue PanelInstanceModel GetLastPanel() { return _panelInstanceModels[_panelInstanceModels.Count - 1]; } /// /// Returns if any panel is showing /// /// Do we have a panel showing? public bool AnyPanelShowing() { return GetAmountPanelsInQueue() > 0; } /// /// Returns how many panels we have in queue /// /// Amount of panels in queue public int GetAmountPanelsInQueue() { return _panelInstanceModels.Count; } }