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 /
  • Help Room /
avatar image
0
Question by loxagos_snake · Sep 23, 2016 at 07:35 PM · guiinventory

Keeping the scale of inventory icons across resolutions

Hi! So, I'm working on an adventure game and I decided to reuse my old inventory system/item database with some slight modifications. It's like Resident Evil: you pick an item up, and it gets stored in a grid. Unfortunately, everything is hardcoded, meaning that aside from the Canvas, I didn't use any of the editor's UI elements to design it. Below is the function that gets called inside OnGUI() when the conditions are met:

 void DrawInventory()
     {
 
         // Render the background
         Rect backRect = new Rect(0,0,Screen.width,Screen.height);
         GUI.DrawTexture (backRect,background,ScaleMode.StretchToFill,true,Screen.width/Screen.height);
         
         // Assign the items in slots
         int i = 0;
         for (int x = 0; x < slotsX; x++)
         {
             for (int y = 0; y < slotsY; y++)
             {   
                 Rect slotRect = new Rect(y * 135 + Screen.height/y_mod, x * 40 + Screen.width/x_mod, Screen.width/w_mod, Screen.height/h_mod); // Reversed to respect horizontal arrangement                
                 slots[i] = inventory[i];                                                                                                      // and soft-coded to match all resolutions
                 
                 if (slots[i].itemName != null)
                 {
                     GUI.DrawTexture (slotRect,slots[i].itemIcon);
 
                     if (isMouseOver (slotRect))
                     {
                         GUI.DrawTexture (slotRect,selectionGrid);
                         DrawDescription(slots[i].itemDescription);
 
                         if (mousePressed)
                         {
                             DrawInvMenu(slotRect);    
                         } 
                     }
                 }
                 i++;    
             }
                 
         }
     }
 

Now, the code itself (picking items, opening the inventory and some other functions I didn't include for simplicity's sake) work perfectly. But since I'm just drawing a texture against which I place my invisible grid, I ran into an unsurprising problem: changing the size of my Game windows naturally messes the positions and scales of the picked items up. Here's an example: https://i.snag.gy/xna0yt.jpg

I've broken my back thinking of different ways to work around this before I ultimately have to rewrite the whole system. To be honest, I'm a bit clueless when it comes to the GUI, so I don't have many thinking patterns to follow. Any help will be much appreciated!

EDIT: The x_mod, y_mod, w_mod and h_mod variables you see above are publicly defined test variables that I used to tweak the positions at runtime.

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

100 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 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 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 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 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 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

Display list as UI or GUI 0 Answers

Inventory stacking 1 Answer

How to make an Inventory Hotbar (C#) 0 Answers

How to create an if statement for each element within a list C# 2 Answers

Unity 5.1 GUI mask disappears in Android build 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