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 Trollvahkiin · Oct 18, 2013 at 01:06 AM · guiarraysinventoryselect

How to select a texture in an inventory?

Hey, so I have this inventory system, it works amazingly but I want to add some stuff to it that I don't know how to approach. I'm able to pick an item up and display them wich then they get deactivated. When I right click them in my inventory they drop under me. I want to add that If I left click in one of the squares it will be selected and 3 buttons pop out, Drop, Equip and Use. I can deal with the button functions but the biggest problem is item selection. How do I select a button.

Inventory:

 static var statInventory : Inventory;
 
 //Our inventory
 var inventory : Array;
 
 //This will be drawn when a slot is empty
 public var emptyTex : Texture;
 
 //the size of the inventory in x and y dimension
 public var inventorySizeX = 8;
 public var inventorySizeY = 5;
 
 //The pixel size (height and width) of an inventory slot
 var iconWidthHeight = 20;
 
 //Space between slots (in x and y)
 var spacing = 4;
 
 //set the position of the inventory
 public var offSet = Vector2( 100, 100 );
 
 // TEST VARIABLES
 // Assign these to test adding Items
 public var testTex : Texture;
 public var testTex2 : Texture;
 
 public var testInvObject : GameObject;
 public var testInvObject2 : GameObject;
 
 var invOpen : boolean = false;
 
 var unZipSound : AudioClip;
 var zipSound : AudioClip;
 
 //Our Representation of an InventoryItem
 @System.Serializable
 class InventoryItem
 {
    //GameObject this item refers to
    var worldObject : GameObject;
    //What the item will look like in the inventory
    var texRepresentation : Texture;
 }
 
 // Create the Inventory
 function Awake()
 {
     statInventory = this;
     
    inventory = new Array(inventorySizeX);
    
     for( var i = 0; i < inventory.length; i ++ )
     {
         inventory[i] = new Array(inventorySizeY);
     }
 }
 
 function Start()
 {
     Screen.showCursor = false;
     Screen.lockCursor = true;
 }
 
 function Update()
 {
     if(Input.GetKeyDown(KeyCode.I))
     {
         invOpen = !invOpen;
         
         if(invOpen)
         {
             audio.PlayOneShot(unZipSound);
             
             Screen.showCursor = true;
             Screen.lockCursor = false;
         }
         else
         {
             audio.PlayOneShot(zipSound);
             Screen.showCursor = false;
             Screen.lockCursor = true;
         }
     }
 }
 
 function OnGUI()
 {
 
     if(invOpen)
     {
     var texToUse : Texture;
     var currentInventoryItem : InventoryItem;
    
      //Go through each row
      for( var i = 0; i < inventory.length; i ++ )
      {
          // and each column
          for( var k = 0; k < inventory[i].length; k ++ )
          {
              currentInventoryItem = inventory[i][k];
            
              //if there is an item in the i-th row and the k-th column, draw it
              if( inventory[i][k] == null )
              {
                  GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), emptyTex );
              }
              else
              {
                  GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), currentInventoryItem.texRepresentation );
              }
              
                 if(currentInventoryItem != null && 
                    GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), "", GUIStyle("label") ))
              {
                  
                  currentInventoryItem.worldObject.transform.position = transform.position;
                  currentInventoryItem.worldObject.transform.rotation = transform.rotation;
                  currentInventoryItem.worldObject.active = true;
                  
                  if(Input.GetMouseButtonUp(0))
                  {        
                      //Equip it
                      currentInventoryItem.worldObject.transform.parent = transform;
                      
                  } else if(Input.GetMouseButtonUp(1))
                  {
                      //Drop it
                      inventory[i][k] = null;    
                      currentInventoryItem.worldObject.transform.parent = null;
       
                  }
              }
          }
     }
     
       }  
 }
 
 function AddItem( item : InventoryItem )
 {
     //Go through each row
     for( var i = 0; i < inventory.length; i ++ )
     {
         // and each column
         for( var k = 0; k < inventory[i].length; k ++ )
         {
            //If the position is empty, add the new item and exit the function
            if( inventory[i][k] == null )
             {
                 inventory[i][k] = item;
                 return;
             }
         }
     }   
    
     //If we got this far, the inventory is full, do somethign appropriate here   
 }
 
 function AddItem( worldObject : GameObject, texRep : Texture )
 {
    var newItem = new InventoryItem();
    
    newItem.worldObject = worldObject;
    newItem.texRepresentation = texRep;
       
    AddItem( newItem );   
 }
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

0 Replies

· Add your reply
  • Sort: 

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

Adding Item object to Inventory List 0 Answers

need to shorten my code but unsure of how 3 Answers

Remove Items and Item Tooltips 0 Answers

Windows within windows 0 Answers

Mysterious crash involving an array 2 Answers


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