Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

centering onGUI rects (solved)

Discussion in 'Immediate Mode GUI (IMGUI)' started by twn9009, May 27, 2019.

  1. twn9009

    twn9009

    Joined:
    May 2, 2019
    Posts:
    47
    below is some code for drawing my inventory, ive drawn all the rects in a grid based off of the amount of invslots, they originate from the top left corner and i'm wanting them to be in the center of the screen, i've tried modifying a few values but cant work it out, if you need a screenshot of the layout in playmode let me know, thanks for reading.
    Code (CSharp):
    1.  void DrawInventory()
    2.     {
    3.         Event e = Event.current;
    4.         int i = 0;
    5.         for (int y = 0 ; y < slotsY; y++)
    6.         {
    7.             for (int x = 0 ; x < slotsX; x++)
    8.             {
    9.                 Rect slotRect = new Rect  (x * 60, y * 60, 50, 50);
    10.                 GUI.Box(new Rect(slotRect), "", skin.GetStyle("Slot"));
    11.                 slots[i] = inventory[i];
    12.                 if (slots[i].itemName != null)
    13.                 {
    14.                     GUI.DrawTexture(slotRect, slots[i].itemIcon);
    15.                    if (slotRect.Contains(Event.current.mousePosition))
    16.                     {
    17.                        tooltip = CreatTooltip(slots[i]);
    18.                         showTooltip = true;
     
  2. twn9009

    twn9009

    Joined:
    May 2, 2019
    Posts:
    47
    i've tried setting the x and y max/min for the boxes based on the inventory slot but it produces a very strange result, they all cascade diagonally within each other,
    Code (CSharp):
    1.   Rect slotRect = new Rect  (x * 60, y * 60, 50, 50)  ;
    2.                 slotRect.xMax = i + 100;
    3.                 slotRect.xMin = i + 50;
    4.                 slotRect.yMax = i + 100;
    5.                 slotRect.yMin = i + 50;
     
  3. twn9009

    twn9009

    Joined:
    May 2, 2019
    Posts:
    47
    forgive me for i am noob and should not code late at night or when hungover
    Rect slotRect = new Rect (Screen.width/3 + x * 60, Screen.height /4 + y * 60, 50, 50) ;
     
  4. IC_

    IC_

    Joined:
    Jan 27, 2016
    Posts:
    45
    Nice. Why don't you use canvas with automatic scaling and other stuff?
     
  5. twn9009

    twn9009

    Joined:
    May 2, 2019
    Posts:
    47
    i was using the outdated GUI stuff, using unity UI now with canvas scaler etc, much easier
     
unityunity