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.)
public InvGameItem Equip (int slot, InvGameItem item)
{
if (item != null)
{
InvBaseItem baseItem = item.baseItem;
if (baseItem != null) return Replace(slot, item);
else Debug.LogWarning("Can't resolve the item ID of " + item.baseItemID);
}
return item;
}
Then create a new CS script called EquipBackpack.cs
using UnityEngine;
/// <summary>
/// Fill the specified items into the bag when the script is started.
/// </summary>
[AddComponentMenu("NGUI/Examples/Equip Backpack")]
public class EquipBackpack : MonoBehaviour
{
public int[] itemIDs;
void Start ()
{
if (itemIDs != null && itemIDs.Length > 0)
{
//UIItemStorage used instead of InvEquipment
UIItemStorage eq = GetComponent<UIItemStorage>();
if (eq == null) eq = gameObject.AddComponent<UIItemStorage>();
int qualityLevels = (int)InvGameItem.Quality._LastDoNotUse;
for (int i = 0, imax = itemIDs.Length; i < imax; ++i)
{
int index = itemIDs[i];
InvBaseItem item = InvDatabase.FindByID(index);
if (item != null)
{
InvGameItem gi
= new InvGameItem
(index, item
); gi.quality = (InvGameItem.Quality)Random.Range(0, qualityLevels);
gi.itemLevel = NGUITools.RandomRange(item.minItemLevel, item.maxItemLevel);
//Our custom equip requires a slot # pulled from the inspector in this case.
eq.Equip(i, gi);
}
else
{
Debug.LogWarning("Can't resolve the item ID of " + index);
}
}
}
Destroy(this);
}
}
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.