Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gongalone · Aug 03, 2013 at 07:24 PM · c#inventorydrag and drop

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();
     
 
     }
             
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges