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 alphardzz · Dec 11, 2014 at 04:09 PM · guidynamicrect

How can I position my dynamically generated GUI boxes at the center of the screen using the screen resolution?

Hey guys, I tried making an Inventory system and I'm having this problem where I create boxes dynamically to make it look like a grid, but seem to be stuck in place no matter what the screen size is.

Here's what I currently have now:

 void DrawInventory()
     {
         Event e = Event.current;
         int i = 0;
 
         for (int y = 0; y < slotsY; y++)
         {
             for (int x = 0; x < slotsX; x++)
             {
                 Rect slotRect = new Rect(x * 60, y * 60, 40, 40);
                 GUI.Box(slotRect, "", skin.GetStyle("Slot"));
                 slots[i] = inventory[i];
 
                 item itemz  = slots[i];
 
                 i++;
             }
     
         }
 
     }


Say I assigned 5 for slotX and 3 for slotY, that would look like this: alt text

Then I tried to center it by changing the Rect's values X and Y statically but obviously that would not be a responsive UI. I tried googling for ways but i couldnt find a solution for centering a GUI boxes generated from a for loop. I'm still new to Unity's GUI feature so if you could please provide a solution for this bug/ point me in the right direction, i would really appreciate it. Thanks everyone.

P.S. Credits for AwfulMedia's YT channel for their free tutorials. That's where i got this code.

gui1.png (1.8 kB)
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 gjf · Dec 11, 2014 at 04:03 PM 0
Share

bets advice is to adapt it to use 4.6's new GUI (you're using the legacy stuff) - it has the ability to maintain relative positions, etc. with varying resolutions.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ijatsu · Dec 11, 2014 at 08:29 PM

I think what you want is to get the screen's size. Here's the code I'm using to place my GUI skin at the center of the screen no matter what.

GUI.skin = reticle;

         GUI.Box(
             new Rect(
                 Screen.width/2 - reticle.box.normal.background.width /2, 
                 Screen.height/2 - reticle.box.normal.background.height/2,
                 reticle.box.normal.background.width/2 ,
                 reticle.box.normal.background.height/2),
             "");
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

Help with destroying guiRect? 0 Answers

Can't destroy an ui element 2 Answers

Rect to RectTransform on overlay Canvas? 1 Answer

Developing a Dynamic GUI 1 Answer

Expression denotes a 'type', where a 'variable', 'value' or 'method group' was expected, and 2 other errors. 2 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