Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by AmasterAmaster · Jun 27, 2015 at 06:05 AM · unity 5guiscalegridscaling

How to scale a GUI (Lagacy) grid?

Hello. I am having an issue where I want a grid of GUI (Legacy) components to scale. But the problem is that I am having trouble with the scaling logic of the grid. I want it so that when the window is smaller, then the grid accommodates to the changes of the window (via stretching/shrinking/growing/etc). Here is what I have so far inside "onGUI()":

         Rect r = new Rect(300, 60, Screen.width - 300, Screen.height - 65);
         GUI.BeginGroup(r);
         
         //offsetX = (-r.width / 10);
         offsetX = Mathf.Lerp(-52, 0, r.width);
         
         for(int i = 0; i < slotHeight; i++)
         {
             for(int j = 0; j < slotWidth; j++)
             {
                 GUI.Box(new Rect(100 * j + (j * offsetX), 110 * i + (i * offsetY), r.width / slotWidth, r.height / slotHeight), visualSlots[slotCounter]);
                     
                 //Counter
                 slotCounter++;
             }
         }
     
         GUI.EndGroup();


Here is what I am trying to scale:

alt text

As seen from the picture, the GUI boxes are not consistent and can add padding (which I do not want) and most of the time are off the screen or overlapping themselves. The offsets I put in there are for testing to see if I could have gotten the Mathf.Lerp function working with the screen/group width.

I have tried using different methods here and there on this grid (such as "GUI.Matrix = Matrix4x4.TRS()" and "Mathf.Lerp()"). Is there a simple way to scale a grid of GUI components consistently?

guibox-problem.png (243.3 kB)
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

1 Reply

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

Answer by Bunny83 · Jun 27, 2015 at 06:54 AM

Are you sure that your used GUIStyle (visualSlots) doesn't have a "fixedHeight" / "fixedWidth" set? This would prevent any size changes.

However i don't quite get your position calculations (especially since some code is missing)

Your position is effectively:

 j * (100 + offsetX) // x position

 i * (110 + offsetY) // y position

This line makes not much sense:

 offsetX = Mathf.Lerp(-52, 0, r.width);

you pass r.width as "t" parameter. This parameter has to be between 0 and 1. Since you width is most likely greater than 1 offsetX is always 0.

You should use your actual box width and height to offset each box

      float boxWidth = r.width / slotWidth;
      float boxHeight = r.height / slotHeight;

      for(int i = 0; i < slotHeight; i++)
      {
          for(int j = 0; j < slotWidth; j++)
          {
              GUI.Box(new Rect(j * (boxWidth), i * (boxHeight), boxWidth, boxHeight), visualSlots[slotCounter]);
              //Counter
              slotCounter++;
          }
      }

If you want a fix sized spacing between each box you would simply subtract that from from boxWidth and boxHeight and once from the over all width / height. Just like this:

 float boxWidth = (r.width - xSpacing) / slotWidth;
 float boxHeight = (r.height - ySpacing) / slotHeight;


 GUI.Box(new Rect(j * (boxWidth), i * (boxHeight), boxWidth - xSpacing, boxHeight - ySpacing), visualSlots[slotCounter]);
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 AmasterAmaster · Jun 27, 2015 at 07:05 AM 0
Share

Ok, I will give that a try, I will let you know what happens after I experiment with what you say. thanks.

avatar image AmasterAmaster · Jun 27, 2015 at 07:10 AM 0
Share

And as soon as I put it in, it works. I will accept this as the answer and thanks again for helping me.

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

22 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

Related Questions

How To Scale GUI Based On Screen Resolution 1 Answer

Scaling GUI correctly according to game window resolution? 1 Answer

Grid Layout Group with scalable content 5 Answers

Dynamically adding (prefab) Buttons to a ScrollView 0 Answers

Unity Skew A Game Object 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