Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 _RC · Aug 10, 2011 at 07:35 PM · guitextureinventorydrawtextureslot

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;}
}

Comment
Add comment · Show 1
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
avatar image _RC · Aug 11, 2011 at 02:08 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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 :)

Comment
Add comment · Show 1 · 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
avatar image poolts · Aug 11, 2011 at 02:52 PM 0
Share

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.

avatar image
0

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.

http://pastebin.com/CKqXF46M

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.

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
avatar image
0

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!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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