- Home /
inventory system - change GUI.DrawTexture texture
hi! i have started working on a inventory system (c#). every slot in the inventory is a GUI.Button.
now i want to draw a GUI.DrawTexture on every Button with an image of the Item that is in that slot.
at the beginning all slots are empty (ItemInSlot1 = 0;).
i have a function called PickUpItem(); that looks for a free slot and changes the ItemInSlot variable to the number that is representing the item.
but i need to make PickUpItem() to change the texture of the GUI.DrawTexture of SlotToTransferPickUp
in to the texture that is representing the item (ItemToPickUp)
first problem:
how can i check which texture to set for the SlotToTransferPick without hundreds line of code for each item/slot?
example:
if (ItemToPickUp == 1){ if (SlotToTransferPickUp == 1){ItemIconSlot1 = InventoryIconHammer;} }
second problem: when i remove the // from this line, the whole inventory is not showing up in game:
//GUI.DrawTexture(new Rect(0,0,64,64), ItemIconSlot1);
just search for "first problem"/"second problem in the script:
http://pastebin.com/qbw9yPaW
/edit:
PickUpItem2() should be like this, because i have already reset ItemToPickUp:
if (SlotToTransferPickUp == 1){
if (ItemInSlot1 == 1){
ItemIconSlot1 = InventoryIconHammer;}
}
it looks like i can get it done on my own. but i still need to know how to change the texture of a GUI.DrawTexture by script.
Answer by poolts · Aug 11, 2011 at 02:18 PM
Why don't you just create a member variable for each item, i.e. itemImage. You can create it as either a public variable or a private and serialize it (my personal choice). And then simply link in the image for each item. Upon drawing each texture, use a for each loop to iterate through the collection of items, then within this loop create new gui buttons assigning each button with the m_itemImage property of each Item.
In pseudo-code it would be something like:
foreach(GameObject item in GameObject[] items)
Draw a button, and assign it's texture value as item.getItemImage() or use direct access to the variable i.e. item.m_itemImage (although I strongly suggest using accessor methods).
Hope that helps :)
Just as AaronG said (I assumed you had already done this) but it would be a more robust system, if you had an implemented class Item, a collection (I suggest using a directionary collection) called Inventory. And then either outside or within the Inventory class, iterated through each item, and drew there member variable itemImage to the button (in the parameter of the button method that declares the image to be used, it would be replaced with Item.itemImage) see above ^ for the pseudo-code for implementation of the for loop.
Answer by AaronG · Aug 11, 2011 at 02:22 PM
I suggest you write a system that utilises classes to determine the properties of each item. That way you can define properties common amongst every item such as the icon used. Here's an example.
Them, to draw it via the GUI you would use
GUI.DrawTexture(new Rect(0, 0, 64, 64), myInventory.Icon[0]);
Where '0' would be the number of whichever slot that GUI element represents.
Answer by _RC · Aug 11, 2011 at 08:45 PM
thank you all for your help!
i know my inventory script is probably totally chaotic, but it is working so far.
and my english is not the best, and i have started coding/scripting just a few days ago.
so i dont unterstand the most things you have said :O
but i will google them and try to get my inventory working. thanks!
Your answer
Follow this Question
Related Questions
GUI.DrawTexture not working? (nothing shows up) 1 Answer
proper inventory system issue.. 1 Answer
how to change a gui button texture 1 Answer
How to display textures that are in an array? 1 Answer
Toggle GUITexture V.2 1 Answer