- Home /
 
               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
              
 
               
              Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                