Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GUI Running Backwards?

Discussion in 'Scripting' started by MF5, Jun 8, 2015.

  1. MF5

    MF5

    Joined:
    Apr 12, 2015
    Posts:
    15
    so I am working on my first inventory system when I ran into a sorta "problem" that I really have no idea why...
    Here is my code to generate a responsive dock bar and uses Labels to show the number it is.

    Code (CSharp):
    1. float slotAmount = 9F; //The Amount of Slots to Show
    2. float slotSize = 40F; //The Size of Each Slot (Width & Height)
    3. Texture2D slotImage; //The Texture to Display as a Slot Placeholder
    4. void OnGUI{
    5.     Rect rect = new Rect(0, 0, Screen.width, Screen.height);
    6.     int currentSlot = 1;
    7.     while(currentSlot < slotAmount + 1){
    8.         float left = ((rect.width / 2) - ((currentSlot * slotSize) + (currentSlot * 5))) + ((slotAmount * slotSize) + (slotAmount * 5)) / 2;
    9.         float top = rect.height - slotSize; //Height - Image Size - Spacing (5);
    10.         GUI.DrawTexture(new Rect(left, top, slotSize, slotSize), slotImage);
    11.         GUI.Label (new Rect(left + 5, top, slotSize, slotSize), currentSlot + "");
    12.         currentSlot++;
    13.     }
    14. }
    All of this code works great except when you look at the bar in game it shows something like this:
    "9 8 7 6 5 4 3 2 1"
    Instead of going from 1-9 like in Minecrafts Inventory Dock it starts backwards and goes from 9-1?
    Any suggestions are appreciated. Thanks!
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Unrelated, but have you considere using the UI tools in 4.6 or 5.0? Beats OnGUI by a mile.
     
  3. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I'd say the problem is your 'float left' variable. From the look of it, you're telling the GUI to draw the first box near the center, and then the final box to the left. If you're reading from left to right, it will go 987654321.
     
  4. MF5

    MF5

    Joined:
    Apr 12, 2015
    Posts:
    15
    I don't like how Unity did the new UI, I think its very clunky to use in code.


    Yea I saw that after looking more into it. I just winged it while right the equation and now I can't understand how to reverse it. Could you show me the code reversed?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Phooey. Add a panel with a horizontal layout. Add a set of 9 images and you are done. Check out my sig for some Examples of coding with the UI. You'll be interested in the dynamic menu one.

    Anyway, back to your question, you could just reverse the numbering. On line 11 use (slotAmount - currentSlot).ToString()

    A better way would be to write this from the beginning with proper code. Use a for loop instead of a while. And generate proper rects instead of the weird clunky math you have going on.
     
  6. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I strongly suggest you do some tutorials and learn more about the new UI. I don't think many people will agree with you that it is clunky. It is much much easier to use than the old GUI

    You wrote the original line of code, didn't you? Surly you know the difference between left and right?
    I agree with @BoredMormon. This looks like it could use a serious refactor:

    Code (CSharp):
    1. [SerializeField] int numSlots = 9; //whole numbers should use integers.
    2. [SerializeField] float bounds = 40f; //additionally, serialize these fields so you can change them in the editor.
    3. [SerializeField] float padding = 5f;
    4. [SerializeField] Texture2D slotImage;  //Should also be serialized, so you can choose the image in the editor.
    5.  
    6. void OnGUI() {
    7.  
    8.     //Write your code with descriptive variable names that are self documenting
    9.     float centerScreen = (float)Screen.width * 0.5f;
    10.     float slotWidthOffset = numSlots * bounds * 0.5f;
    11.  
    12.     float offsetX = centerScreen - slotWidthOffset;
    13.  
    14.     //place values that arn't going to change outside of the loop
    15.     float y = (float)Screen.height - bounds;
    16.  
    17.     for(int i = 0; i < numSlots; i++) {
    18.  
    19.         float x = offsetX + i * (bounds + padding);
    20.         string numLabel = i.toString();
    21.  
    22.         GUI.DrawTexture( new Rect(x, y, bounds, bounds), slotImage);
    23.         GUI.Label( new Rect(x + padding, y, bounds, bounds), numLabel);
    24.  
    25.     }  
    26.  
    27. }
     
    Kiwasi likes this.