Setting the quick-select items

In many games, there is a mechanism known as quick-items or quick-select items. These are items that the player uses often and wants to have access to very quickly without having to stop the game to go into their inventory. They are typically accessed by the number keys on the keyboard or the directional pad on a controller. We will add a small function into our inventory that will allow us to assign quick-items.

Setting the quick-select items quickly

Add this function to your script, under the RemoveFromInventory function:

void SetQuickItem(GameObject NewItem, int QuickInput)
{
  if(QuickItems[QuickInput].name != NewItem.name)
      if(QuickInput < QuickItems.Length)
        QuickItems[QuickInput] = NewItem;
}

This function takes in two variables, the GameObject that we want as the quick-item and the slot that we want to assign it to. In the actual function, we check whether the name of the GameObject in the current QuickItems array is the same as the name of NewItem. If it is not same, then we will not do anything because the item is already in that quick-item slot. If it isn't, then we assign the QuickItems array slot, which is QuickInput, to the GameObject passed into the function.

Let's display the inventory

Now let's move on to the next important aspect of the inventory, showing it to the player! To do this, we will use GUILayout and a GUI window.

Using our custom inputs

To access the inventory, we have set up a couple of inputs for our player to use to view the inventory. Let's add an Update function along with some code that will show the inventory:

void Update()
{
  if(Input.GetButtonUp("I_Key") || Input.GetButtonUp("A_360"))
  {
    showInventory = (showInventory) ? false : true;
  }
}

In the Update function, we check whether the "I_Key" or "A_360" inputs are pressed. When one of the inputs is pressed, we switch the showInventory Boolean. When the showInventory Boolean is true, we show the inventory GUI to the player; if it is false, then we hide the inventory GUI.

Displaying the GUI

Now let's add the OnGUI function to the script, just below the Update function:

void OnGUI()
{
  if(showInventory)
  {
    inventoryRect = GUI.Window(0, inventoryRect, InventoryGUI, "Inventory");
  }
}

When the showInventory Boolean is true, we show the inventory GUI. We do this by setting our inventoryRect rectangle variable to a GUI window. The GUI window will show the inventory window, as well as give it the title "Inventory".

Running the GUI

Now, to run the window, we need a function that will show the contents of the GUI window. Add the following function to your script, under the OnGUI function:

void InventoryGUI(int ID)
{
  GUILayout.BeginArea(new Rect(0, 50, 400, 350));

  GUILayout.BeginHorizontal();
  GUILayout.Button(itemCount[0].Value.ToString() + " " + invItems[0].name, GUILayout.Height(75));
  GUILayout.Button(itemCount[1].Value.ToString() + " " + invItems[1].name, GUILayout.Height(75));
  GUILayout.Button(itemCount[2].Value.ToString() + " " + invItems[2].name, GUILayout.Height(75));
  GUILayout.EndHorizontal();
    
  GUILayout.BeginHorizontal();
  GUILayout.Button(itemCount[3].Value.ToString() + " " + invItems[3].name, GUILayout.Height(75));
  GUILayout.Button(itemCount[4].Value.ToString() + " " + invItems[4].name, GUILayout.Height(75));
  GUILayout.Button(itemCount[5].Value.ToString() + " " + invItems[5].name, GUILayout.Height(75));
  GUILayout.EndHorizontal();
    
  GUILayout.BeginHorizontal();
  GUILayout.Button(itemCount[6].Value.ToString() + " " + invItems[6].name, GUILayout.Height(75));
  GUILayout.Button(itemCount[7].Value.ToString() + " " + invItems[7].name, GUILayout.Height(75));
  GUILayout.Button(itemCount[8].Value.ToString() + " " + invItems[8].name, GUILayout.Height(75));
  GUILayout.EndHorizontal();
    
  GUILayout.BeginHorizontal();
  GUILayout.Button(QuickItems[0].name, GUILayout.Height(50));
  GUILayout.Button(QuickItems[1].name, GUILayout.Height(50));
  GUILayout.EndHorizontal();

  GUILayout.BeginHorizontal();
  GUILayout.Button(QuickItems[2].name, GUILayout.Height(50));
  GUILayout.Button(QuickItems[3].name, GUILayout.Height(50));
  GUILayout.EndHorizontal();

  GUILayout.EndArea();
}

To start off, we access GUILayout and use BeginArea. This is a useful tool to have a contained area to work with. The next step is to activate the BeginHorizontal function within GUILayout, which ensures that everything we put on the GUI is in an even, horizontal line. After this, we create GUILayout buttons, one for each slot in our inventory. We set the text of the button to the amount and name of an item within the inventory, then we set the height of the button.

We do this a few times to show all of our inventory items on screen in three different rows. After this, we do the same thing for QuickItems, except with two rows of two items. Now, the player can see their inventory by clicking on one of our custom inputs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.162.49