Open Panel Code

Niroan

Member
Hello everyone.

How would i go if i want to open a panel with code?
I managed to do this below. And i can then attach OpenPanel() to a custom button. That when clicked will OpenMainMenu.
If i click the OpenCraftingMenu() it will open crafting menu.

But is this the correct way? It seems wrong to do it like this.
Can i please get some help here?

Also what is the correct way to code a close button via code? Is there something i need to handle inside the code? Like events or update inventory??


C#:
using Opsive.UltimateInventorySystem.Demo.UI.Menus.Crafting;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Opsive.UltimateInventorySystem.Demo.Interaction.Interactables
{
    using Opsive.Shared.Utility;
    using Opsive.UltimateInventorySystem.Core;
    using Opsive.UltimateInventorySystem.Core.InventoryCollections;
    using Opsive.UltimateInventorySystem.Crafting;
    using Opsive.UltimateInventorySystem.Demo.UI.Menus.Crafting;
    using Opsive.UltimateInventorySystem.Interactions;
    using Opsive.UltimateInventorySystem.Storage;
    using Opsive.UltimateInventorySystem.UI;
    using Opsive.UltimateInventorySystem.UI.Panels;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI.Extensions.Examples;

    public class testscr : MonoBehaviour
    {
        [Tooltip("The crafting menu.")]
        [SerializeField] protected CraftingMenu m_CraftingMenu;
        [Tooltip("The recipes to display in the menu.")]
        [SerializeField] protected CraftingRecipe[] m_MiscellanousRecipes;
        [Tooltip("The recipes with the categories specified will be visible in the menu.")]
        [SerializeField] protected CraftingCategory[] m_RecipeCategories;

        [SerializeField] protected DisplayPanelManager m_DisplayPanelManager;

        protected CraftingRecipe[] m_Recipes;

        public Inventory Inventory;
        public GameObject DMP;

        public DisplayPanelManager _DPM;
        

        // Start is called before the first frame update
        void Start()
        {
            var recipeList = new List<CraftingRecipe>(m_MiscellanousRecipes);

            for (int i = 0; i < m_RecipeCategories.Length; i++)
            {
                var pooledArray = GenericObjectPool.Get<CraftingRecipe[]>();
                var recipesCount = m_RecipeCategories[i].GetAllChildrenElements(ref pooledArray);
                for (int j = 0; j < recipesCount; j++)
                {
                    recipeList.Add(pooledArray[j]);
                }
                GenericObjectPool.Return(pooledArray);
            }

            m_Recipes = recipeList.ToArray();
        }

        public void OpenPanel() {
            _DPM.OpenMainMenu();
        }

        public void OpenCraftingMenu() {
            m_CraftingMenu.SetClientInventory(Inventory);
            m_CraftingMenu.SetRecipes(m_Recipes);
            m_CraftingMenu.Open(m_CraftingMenu);
        }
    
    }
}
 
You are extremely close to the right way of doing things.
I answered your other post where i explain in detail how to open the shop and crafting menu correctly here:

The only noticeable difference is the line where you open the crafting menu. If you read the comments for the parameters you'll understand why it's wrong:
C#:
/// <summary>
        /// Open the panel.
        /// </summary>
        /// <param name="previousPanel">The previous panel.</param>
        /// <param name="previousSelectable">The previous selectable.</param>
        /// <param name="selectDefault">Select the default selectable.</param>
        public virtual void Open(DisplayPanel previousPanel, Selectable previousSelectable = null,
            bool selectDefault = true)
        {
            //This is required even if it is done in OpenInternal,
            //because the selectable needs to be active before getting selected.
            if (m_SetActiveOnOpen) { gameObject.SetActive(true); }

            m_PreviousSelectable = previousSelectable;
            m_PreviousPanel = previousPanel;
            if (selectDefault && m_SelectableOnOpen != null) {
                m_SelectableOnOpen.Select();
            }

            OpenInternal();
        }

As you can see the first parameter is the "previous" panel. Meaning the panel you want to go back to when closing the panel you just opened.
When using menus you can use this function:

m_PanelManager.OpenMenu(m_CraftingMenu);

Which will check the currently open menu and use that as parameter of the previous panel. This only works if the menu is set as not standalone in the inspector.
 
Okay. But the whole idea is not to open another panel after i close current.
That is exactly what issue i had yesterday,when i closed the inventory it opened crafting panel.Then i got error
 
In that case you can simply pass in "null" as the previous panel parameter such that when closed no panel will be opened
 
Top