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 DeadKenny · Nov 11, 2013 at 08:52 PM · guirpggui-buttonitem

Loot GUI, chest, lists and gameobjects Problem.

Ok what want to make is to make a lootable chest or in my case a crate/box.

The problem I have is that I cannot get a good way to link the objects in the crate to the UI and then to the icon of the object.

I need an automatic way, so the UI when opening the crate will go looking for the icon of the related gameobject in the List of the crate.

Let me try and explain better in the code.

         private Crate crateScript;
         private BaseObjectClass objectScript;    
     private List<BaseObjectClass> crateItems;
     
     
     private const string OBJECT_CAMEO_PATH = "UI/Cameos/";
     
     
     
 
     // Use this for initialization
     void Awake () {
         
         
         crateScript = this.gameObject.GetComponent<Crate>();
         // This is the crate items list.
         crateItems = crateScript.baseObjeectList;
         
         //This is the part I can't figure out... How to get the game objects in the list then get the name and load icon from resources?
         //This is Pseudo retard code: 
 
           var item = crateItems.Find(The Items, (GameObject)Resources.Load("The Items Icon"));
 
                 
         //  
 
     }
     
     //Now show the buttons.
      
     void OnGUI(){
     
             
         for(int cnt = 0; cnt < crateItems.Count; cnt++){
             
         GUILayout.BeginHorizontal("box");
     
            if(GUILayout.Button(item){
 
                         //Then get the object its linked to if the button is pressed.
                         //Pseudo Code:
                            
                         item.transform.parent = player;
                         
 
                      }
 
         GUILayout.EndHorizontal();
 
 
        
 
         // Hope you guys understand..
 
 
 
 }
 
 
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DeadKenny · Nov 14, 2013 at 01:39 AM

Case solved.

I think I just hit a wall. I took time off and after I came back, bam.

// _cameo is the texture I set in the Item and now its linked.

code:

for(int cnt = 0; cnt < crateItems.Count; cnt++){

    GUILayout.BeginHorizontal("box");

       if(GUILayout.Button(crateItems[cnt]._cameo){

                    Instatiate(bla bla bla);


                  }

    GUILayout.EndHorizontal();


//

Only thing left is removing from list once clicked... and replacing button with texture.

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 undead-steve · Nov 12, 2013 at 12:38 AM

Have you tried storing the path to the correct icon as a field on each of the items in your crate?

   class CrateItem  : ScriptableObject
     {
         public string IconPath;
 
         public CrateItem(icon)
         { 
             IconPath = icon;
         }
 
         public Texture2D GetIcon()
         {
             return (Texture2D) Resources.load(IconPath) as Texture2D;
         }
     }
Comment
Add comment · Show 2 · 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 DeadKenny · Nov 12, 2013 at 03:32 PM 0
Share

Yeah that is in the BaseObjectClass... but I replaced it with a public Texture2D that you have to set manually.

So now I just need to get the texture in the Gameobject in the list.

Still can't figure it out.

avatar image undead-steve · Nov 12, 2013 at 09:41 PM 0
Share

What about giving the crate a list of Texture2Ds

 public Dictionary<crateItem, Texture2D> _ItemTextures;

Then in the awake you fill it out:

 foreach (CrateItem c_item in crateItems)
 {  
     _ItemTextures[c_item] = c_item.GetIcon();
 }

and in the update code you use the list to draw the gui:

 foreach (CrateItem c_item in crateItems)
 {
     Texture2D icon = _ItemTextures[c];
     if GUILayout.Button(icon, GuiLayout.width(32))
      {
        // do something using the item
       }
 }

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

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

Related Questions

Real Simple Inventory and Vendor System Help 1 Answer

problem with gui 2 Answers

how make a for loop that makes GUI boxes ? 1 Answer

Changing the font size of a GUI.Button in the inspector 1 Answer

On Screen Button Controls 0 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