Author Topic: Loading Inventory into Backpack  (Read 2191 times)

SandiCassidy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Loading Inventory into Backpack
« on: December 08, 2013, 12:35:59 PM »
Hi;
I am building a dressing room scene and using the Inventory example as a basis. I want to load all my inventory items into the backpack (I call it Selection) and allow the user to take one and equip it (move item from backpack to a slot). I have items in the inventory but am not sure how to preload the backpack. Can you give me an idea of how to do that?

Thanks

Sandi

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Loading Inventory into Backpack
« Reply #1 on: December 08, 2013, 04:33:25 PM »
Backpack slots in the inventory example are instantiated dynamically at run-time. If you have an existing inventory to populate it, that would be the time to set the data or instantiate different prefabs as needed.

maconbot

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Loading Inventory into Backpack
« Reply #2 on: February 07, 2014, 05:07:28 PM »
Hi;
I am building a dressing room scene and using the Inventory example as a basis. I want to load all my inventory items into the backpack (I call it Selection) and allow the user to take one and equip it (move item from backpack to a slot). I have items in the inventory but am not sure how to preload the backpack. Can you give me an idea of how to do that?

Just spent several hours tracking this down. The naming conventions were not very obvious.

Think of it like this:
UIItemStorage = InvEquipment
and
UIStorageSlot = UIEquipmentSlot

Once you realize this…it only takes 10 minutes…code below:

Add this method to UIItemStorage (attached to the backpack.)
  1.         public InvGameItem Equip (int slot, InvGameItem item)
  2.         {
  3.                 if (item != null)
  4.                 {
  5.                         InvBaseItem baseItem = item.baseItem;
  6.                         if (baseItem != null) return Replace(slot, item);
  7.                         else Debug.LogWarning("Can't resolve the item ID of " + item.baseItemID);
  8.                 }
  9.                 return item;
  10.         }

Then create a new CS script called EquipBackpack.cs
  1. using UnityEngine;
  2.  
  3. /// <summary>
  4. /// Fill the specified items into the bag when the script is started.
  5. /// </summary>
  6.  
  7. [AddComponentMenu("NGUI/Examples/Equip Backpack")]
  8. public class EquipBackpack : MonoBehaviour
  9. {
  10.         public int[] itemIDs;
  11.  
  12.         void Start ()
  13.         {
  14.                 if (itemIDs != null && itemIDs.Length > 0)
  15.                 {
  16.                         //UIItemStorage used instead of InvEquipment
  17.                         UIItemStorage eq = GetComponent<UIItemStorage>();
  18.                         if (eq == null) eq = gameObject.AddComponent<UIItemStorage>();
  19.  
  20.                         int qualityLevels = (int)InvGameItem.Quality._LastDoNotUse;
  21.  
  22.                         for (int i = 0, imax = itemIDs.Length; i < imax; ++i)
  23.                         {
  24.                                 int index = itemIDs[i];
  25.                                 InvBaseItem item = InvDatabase.FindByID(index);
  26.  
  27.                                 if (item != null)
  28.                                 {
  29.                                         InvGameItem gi = new InvGameItem(index, item);
  30.                                         gi.quality = (InvGameItem.Quality)Random.Range(0, qualityLevels);
  31.                                         gi.itemLevel = NGUITools.RandomRange(item.minItemLevel, item.maxItemLevel);
  32.                                        
  33.                                         //Our custom equip requires a slot # pulled from the inspector in this case.
  34.                                         eq.Equip(i, gi);
  35.                                 }
  36.                                 else
  37.                                 {
  38.                                         Debug.LogWarning("Can't resolve the item ID of " + index);
  39.                                 }
  40.                         }
  41.                 }
  42.                 Destroy(this);
  43.         }
  44. }
  45.  

Finally just drag the new script to the backpack, and add some items. It works just like the Orc's InvEquipment. ArenMook please consider adding this to the demo. Everyone will want to be able have a way to save their inventory.