- Home /
This post has been wikified, any user with enough reputation can edit it.
How to select an Game Object from an Item List
I have an inventory system that uses a list to create icons and all, but I want the icons to represent the game objects listed in an array. I want to be able to click on the icon and drag out a game object representing that item in the inventory list. C Sharp only
here is the code for the items: using UnityEngine; using System.Collections;
[System.Serializable]
public class item {
public string itemName;
public int itemID;
public string itemDisc;
public Texture2D icon;
public item(string name, int id, string desc){
itemName = name;
itemID = id;
itemDisc = desc;
icon = Resources.Load<Texture2D> ("Icons/" + name);
}
public item(){
}
}
and here is the code for the inventory that I am working with:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour {
public int slotsX, slotsY;
public List<item> inventory = new List<item>();
public List<item> slots = new List<item>();
private bool showInventory;
private itemDatabase database;
private bool showToolTip;
private string toolTip;
private bool dragItem;
private item draggedItem;
public GameObject[] items;
private itemPlacement ItemPlacement;
// Use this for initialization
void Start () {
ItemPlacement = GetComponent<itemPlacement> ();
for(int i = 0; i < (slotsX * slotsY); i++){
slots.Add(new item());
inventory.Add (new item());
}
database = GameObject.FindGameObjectWithTag ("Item Database").GetComponent<itemDatabase> ();
addItem (0);
addItem (1);
addItem (2);
addItem (3);
}
void Update(){
if(Input.GetButtonDown("Inventory Menu"))
{
showInventory = !showInventory;
}
}
// Update is called once per frame
void OnGUI () {
toolTip = "";
if(showInventory)
{
drawInventory ();
}
if (showToolTip){
GUI.Box (new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 200, 50),toolTip);
}
if (dragItem){
GUI.DrawTexture (new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50),draggedItem.icon);
}
}
void drawInventory(){
Event e = Event.current;
int i = 0;
for (int x = 0; x < slotsX; x++) {
for (int y = 0; y < slotsY; y++) {
Rect slotRect = new Rect (x * 60, y * 60, 60, 60);
slots [i] = inventory [i];
if (slots [i].itemName != null) {
GUI.DrawTexture (slotRect, slots [i].icon);
if (slotRect.Contains (Event.current.mousePosition)) {
toolTip = createToolTip (slots [i]);
showToolTip = true;
if (e.button == 0 && e.type == EventType.MouseDrag && !dragItem) {
dragItem = true;
draggedItem = slots [i];
}
}
for (int g = 0; g <items.Length; g++) {
if (e.type == EventType.mouseUp && dragItem) {
dragItem = false;
draggedItem = null;
g = slots[i].itemID;
ItemPlacement.SetItem (items [g]);
}
}
if (toolTip == "") {
showToolTip = false;
}
i++;
}
}
}
}
string createToolTip (item Item){
toolTip = Item.itemName + "\n" + Item.itemDisc;
return toolTip;
}
void removeItem(int id){
for (int i = 0; i < inventory.Count; i++){
if (inventory [i].itemID == id){
inventory[i] = new item();
break;
}
}
}
void addItem(int id){
for (int i = 0; i < inventory.Count; i++) {
if (inventory [i].itemName == null) {
inventory [i] = database.items[id];
break;
}
}
}
}
Comment