- Home /
Draggable items in inventory
Hi, I made a inventory script that contains array of GUI.Button objects that keeps items the player picked in the game world. At this point script iterates through the current inventory and fills buttons with items. I'm curious if there's any way of modyfing the script to allow player to drag items by holding the left mouse button and placing them in different inventory slot. Here's my script.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Resources;
public class Inventory : MonoBehaviour {
public FPSController controller;
public PrefabList prefablist;
public bool isToggled = false;
public GUISkin inventorySkin, inventoryEquip;
public List<MasterObject> inventory = new List<MasterObject>();
/****************************/
private static int invID = 01;
private Rect invWindow = new Rect(Screen.width/2,Screen.height/2, 500, 400);
private int inventoryRows = 7;
private int inventoryCols = 5;
/****************************/
private int buttonWidth = 40;
private int buttonHeight = 40;
private int invChangeWidth = 60;
private int invChangeHeight = 20;
/*****TEMP BUTTONS CONTROL****/
public float buttonHorizontalPos;
public float buttonVerticalPos;
/*****PICKING FROM INVENTORY****/
bool isSelected = false;
public MasterObject selected;
void Start () {
controller = GetComponentInChildren<FPSController>();
}
void Update () {
AddInventory();
if(Input.GetKeyDown(KeyCode.I))
{
isToggled = !isToggled;
}
}
void AddInventory()
{
MasterObject toPick = controller.pick;
if(toPick != null)
{
GameObject item = prefablist.GetWeapon(toPick.name);
MasterObject itemToPick = item.transform.root.GetComponent<MasterObject>();
inventory.Add(itemToPick);
Destroy (controller.pick.gameObject);
}
}
void OnGUI()
{
if(isToggled == true)
{
drawInventory();
}
}
public void drawInventory()
{
GUI.skin = inventorySkin;
invWindow = GUI.Window(invID, invWindow, inventoryWindow, "inventory");
}
public MasterObject checkInventory(int count)
{
for(int i = count; i < inventory.Count; i++)
{
count = 0;
MasterObject inventoryItem = inventory[i].GetComponent<MasterObject>();
if(inventoryItem)
return inventory[i];
count++;
}
return null;
}
public void inventoryWindow(int id)
{
int cnt = 0;
GUI.BeginGroup(new Rect(invWindow.width/2, 20, 240, 370), "");
GUI.skin = inventoryEquip;
GUI.Box(new Rect(0, 0, 240, 370), "");
GUI.Button (new Rect(98.12f, 89.99f, buttonWidth, buttonHeight), "Chest");
GUI.Button (new Rect(34.4f, 164.7f, buttonWidth, buttonHeight), "LH");
GUI.Button (new Rect(160.4f, 22.81f, buttonWidth, buttonHeight), "Head");
GUI.Button (new Rect(160.4f, 227.65f, buttonWidth, buttonHeight), "Leg");
/**EQUIP CYBERWARE BUTTONS**/
GUI.Button (new Rect(27.43f, 338.73f, invChangeWidth, invChangeHeight), "EQUIP");
GUI.Button (new Rect(150.87f, 338.73f, invChangeWidth, invChangeHeight), "CYBER");
GUI.EndGroup();
GUI.BeginGroup(new Rect(10, 20, 240, 370), "");
GUI.skin = null;
GUI.Box(new Rect(0,0,240,370), "");
for(int x = 0; x < inventoryRows; x++)
{
for(int y = 0; y < inventoryCols; y++)
{
if(cnt < inventory.Count)
{
if(GUI.RepeatButton(new Rect(15 + (y * buttonWidth), 15 + (x * buttonHeight), buttonWidth, buttonHeight), checkInventory(cnt).image))
{
Debug.Log ("Selected ", checkInventory(cnt));
selected = checkInventory(cnt);
}
}
else
{
if(GUI.RepeatButton(new Rect(15 + (y * buttonWidth), 15 + (x * buttonHeight), buttonWidth, buttonHeight), ""))
{
}
}
cnt++;
}
}
GUI.EndGroup();
GUI.DragWindow();
}
}
Answer by WizzDE · Aug 03, 2013 at 09:53 PM
Use Input.GetMouseDown/GetMouseUp to detect whenever the user presses/releases the item. Combine this with the unity Gui and it should be really simple to do something like this. Remember that the OnGUI function is called every frame so you can assign the mouse position to an gui element and it will follow the mouse cursor. Hope I helped!
Wizz
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
unity3d simple inventory 1 Answer
How can I delete an item from this inventory system? 1 Answer
Adding Item to an Inventory. 1 Answer