Måske er der nogen herinden der kan hjælpe mig med mit script.
Jeg er ved at lave et Inventory system, og inventory scriptet er også ansvarlig for at vise tooltips for 'items', som spilleren 'hover' over med musen inde i inventoriet.
Scriptet som jeg har lavet indtil videre er dette (husk, scriptet bruger andre scripts, f.eks. et for item databasen, og så Item classen):
Code: Select all
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour
{
public int slotsX, slotsY;
public GUISkin skin;
public List<Item> inventory = new List<Item>();
public List<Item> slots = new List<Item>();
public bool showInventory;
private ItemDatabase database;
private bool showTooltip;
private string tooltip;
private bool draggingItem;
private Item draggedItem;
private int prevIndex;
void Start ()
{
for(int i = 0; i < (slotsX * slotsY); i++)
{
slots.Add(new Item());
inventory.Add(new Item());
}
database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
AddItem(101);
AddItem(0);
AddItem(1);
AddItem(201);
}
void Update()
{
if(Input.GetButtonDown ("Inventory"))
{
showInventory = !showInventory;
}
}
void OnGUI()
{
tooltip = "";
GUI.skin = skin;
if(showInventory)
{
DrawInventory();
if(showTooltip)
// GUI.Box (new Rect(Event.current.mousePosition.x + 30f, Event.current.mousePosition.y, 200, 200), tooltip, skin.GetStyle("Tooltip"));
DrawTooltip();
}
if(draggingItem)
{
GUI.DrawTexture(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50), draggedItem.itemIcon);
Screen.showCursor = false;
}
}
// This method draws the inventory
void DrawInventory()
{
Event e = Event.current;
int i = 0;
for(int y = 0; y < slotsY; y++)
{
for(int x = 0; x < slotsX; x++)
{
Rect slotRect = new Rect(x * 50, y * 50, 50, 50);
Item item = slots[i];
GUI.Box(slotRect, "", skin.GetStyle("Slot"));
slots[i] = inventory[i];
if(slots[i].itemName != null)
{
GUI.DrawTexture(slotRect, slots[i].itemIcon);
if(slotRect.Contains(e.mousePosition))
{
tooltip = CreateTooltip(slots[i]);
showTooltip = true;
if(e.button == 0 && e.type == EventType.mouseDrag && !draggingItem)
{
draggingItem = true;
prevIndex = i;
draggedItem = slots[i];
inventory[i] = new Item();
}
if(e.type == EventType.mouseUp && draggingItem)
{
inventory[prevIndex] = inventory[i];
inventory[i] = draggedItem;
draggingItem = false;
draggedItem = null;
Screen.showCursor = true;
}
if(e.isMouse && e.type == EventType.mouseDown && e.button == 1)
{
if(item.itemType == Item.ItemType.Consumable)
{
UseConsumable(slots[i], i, true);
}
}
}
}
else
{
if(slotRect.Contains(e.mousePosition))
{
if(e.type == EventType.mouseUp && draggingItem)
{
inventory[i] = draggedItem;
draggingItem = false;
draggedItem = null;
Screen.showCursor = true;
}
}
}
if(tooltip == "")
{
showTooltip = false;
}
i++;
}
}
}
private void DrawTooltip()
{
float dynamicSize = skin.box.CalcHeight(new GUIContent(tooltip), 200);
GUI.Box (new Rect(Event.current.mousePosition.x + 30f, Event.current.mousePosition.y, 200, dynamicSize), tooltip, skin.GetStyle("Tooltip"));
}
string CreateTooltip(Item item)
{
// string col;
// if(item.itemRarity == Item.Rarity.Common)
// {
// col = "<color=#f2f2f>";
// }
tooltip = "<color=#91492f>" + item.itemName + "</color>\n\n" + "<color=#401F12>" + item.itemDescription + "</color>\n\n" + "Rarity: " + item.itemRarity;
return tooltip;
}
// This method is used to add items to the inventory,
// based on the id-number provided
public void AddItem(int id)
{
for(int i = 0; i < inventory.Count; i++)
{
if(inventory[i].itemName == null)
{
for(int j = 0; j < database.items.Count; j++)
{
if(database.items[j].itemID == id)
{
inventory[i] = database.items[j];
}
}
break;
}
}
}
public void AddItemName(string item)
{
for(int i = 0; i < inventory.Count; i++)
{
if(inventory[i].itemName == null)
{
for(int j = 0; j < database.items.Count; j++)
{
if(database.items[j].itemName == item)
{
inventory[i] = database.items[j];
}
}
break;
}
}
}
// Use this method to remove the first item with the
// specified ID number
public void RemoveItem(int id)
{
for(int i = 0; i < inventory.Count; i++)
{
if(inventory[i].itemID == id)
{
inventory[i] = new Item();
break;
}
}
}
// Use this method to remove ALL items with the
// specified ID number
public void RemoveAll(int id)
{
for(int i = 0; i < inventory.Count; i++)
{
if(inventory[i].itemID == id)
{
inventory[i] = new Item();
}
}
}
// This method checks to see whether an item with a
// specified id-number is in the inventory
public bool InventoryContains(int id)
{
bool result = false;
for(int i = 0; i < inventory.Count; i++)
{
result = inventory[i].itemID == id;
if(result)
{
break;
}
}
return result;
}
private void UseConsumable(Item item, int slot, bool deleteItem)
{
switch(item.itemID)
{
case 0:
{
print ("Used Consumable: " + item.itemName);
break;
}
case 1:
{
print ("Used Consumable: " + item.itemName);
break;
}
}
if(deleteItem)
inventory[slot] = new Item();
}
}
Nogen idé til hvordan jeg får dét lavet?
Ps. hvis beskrivelsen ikke giver mening, så må i lige sige til