using UnityEngine;
using System.Collections.Generic;
[ExecuteInEditMode]
[AddComponentMenu("NGUI/Examples/Item Database")]
public class InvDatabase : MonoBehaviour
{
// Cached list of all available item databases
static InvDatabase[] mList;
static bool mIsDirty = true;
///
/// Retrieves the list of item databases, finding all instances if necessary.
///
static public InvDatabase[] list
{
get
{
if (mIsDirty)
{
mIsDirty = false;
mList = NGUITools.FindActive();
}
return mList;
}
}
///
/// Each database should have a unique 16-bit ID. When the items are saved, database IDs
/// get combined with item IDs to create 32-bit IDs containing both values.
///
public int databaseID = 0;
///
/// List of items in this database.
///
public List items = new List();
///
/// UI atlas used for icons.
///
public UIAtlas iconAtlas;
///
/// Add this database to the list.
///
void OnEnable () { mIsDirty = true; }
///
/// Remove this database from the list.
///
void OnDisable () { mIsDirty = true; }
///
/// Find an item by its 16-bit ID.
///
InvBaseItem GetItem (int id16)
{
for (int i = 0, imax = items.Count; i < imax; ++i)
{
InvBaseItem item = items[i];
if (item.id16 == id16) return item;
}
return null;
}
///
/// Find a database given its ID.
///
static InvDatabase GetDatabase (int dbID)
{
for (int i = 0, imax = list.Length; i < imax; ++i)
{
InvDatabase db = list[i];
if (db.databaseID == dbID) return db;
}
return null;
}
///
/// Find the specified item given its full 32-bit ID (not to be confused with individual 16-bit item IDs).
///
static public InvBaseItem FindByID (int id32)
{
InvDatabase db = GetDatabase(id32 >> 16);
return (db != null) ? db.GetItem(id32 & 0xFFFF) : null;
}
///
/// Find the item with the specified name.
///
static public InvBaseItem FindByName (string exact)
{
for (int i = 0, imax = list.Length; i < imax; ++i)
{
InvDatabase db = list[i];
for (int b = 0, bmax = db.items.Count; b < bmax; ++b)
{
InvBaseItem item = db.items[b];
if (item.name == exact)
{
return item;
}
}
}
return null;
}
///
/// Get the full 32-bit ID of the specified item.
/// Use this to get a list of items on the character that can get saved out to an external database or file.
///
static public int FindItemID (InvBaseItem item)
{
for (int i = 0, imax = list.Length; i < imax; ++i)
{
InvDatabase db = list[i];
if (db.items.Contains(item))
{
return (db.databaseID << 16) | item.id16;
}
}
return -1;
}
}