Search Unity

Resizing elements to screen size within Arrays

Discussion in 'Immediate Mode GUI (IMGUI)' started by PotatoInsertion, Oct 30, 2013.

  1. PotatoInsertion

    PotatoInsertion

    Joined:
    Mar 11, 2013
    Posts:
    23
    Hey all,

    I'm having no luck with this question in Unity Answers, so maybe the forum is a more appropriate place for it, and maybe expand on what I'm asking.

    I've been recently making GUI elements change according to screen size using something like the following:

    Code (csharp):
    1. hudWindowRect = Rect((hudWindowRectX / 100) * Screen.width, (hudWindowRectY / 100) * Screen.height, (hudWindowRectW / 100) * Screen.width, (hudWindowRectH / 100) * Screen.height);
    2.  
    This is working for me fine. Each element in the Rect has its own variable worked as a percentage, so I can move elements around while in play mode until they're where I want them, I then copy the component and paste in the values outside of play mode. And because they're percentages, it adjusts to screen size.

    But I have two problems. The main one is working out to to do this in arrays. I'm working on a shop at the moment and I've made a pretty extensive inventory for a different project and they both use arrays of rects for button and label elements. So I can't (or don't know how) to get into each individual one to resize it according to screen size.

    Example of my code:

    Code (csharp):
    1. var index=0;
    2.  
    3.     for(var a in shipsToBuy){
    4.  
    5.        if(GUI.Button(shipsToBuyPosition[index],shipsToBuy[index].shipIcon, "ShipButtonStyle")){
    6.  
    7. }
    Secondly, things are getting unwieldy already, with a lot of variables (4 for each element). So if I could work this method into arrays, I can see it becoming almost unworkable. Rects are nice and handy in the inspector the way you can show and hide them, but my method means each one must have 4 separate variables, so after 5 or 6 elements, it's very easy to get lost.

    So, main question is, is there a more elegant solution for this? I've seen a lot of answers to the broad question of resizing, but can't find anything related to arrays of rects, or much that doesn't just say 'work it out as a percentage'.

    I'm coding in Javascript, suppose my level could be described as an intermediate beginner. Any help or even discussion would be appreciated.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're not increasing the index value anywhere. Either use a standard for loop (recommended), or increase it in the for/in loop.

    --Eric
     
  3. PotatoInsertion

    PotatoInsertion

    Joined:
    Mar 11, 2013
    Posts:
    23
    Thanks for the reply, Eric, but I mustn't have made myself clear enough.

    I realise that, this is just a small snippet of the code to show where I'm using the Rect array. The array and loops are working fine in my project, it's just that because I'm using an Rect array and not individual Rects for my buttons, I don't know how to make them resize along with the screen with the method I outlined above, assuming that's the right method to use. Which I think it's probably not, because like I said, it's very awkward when using multiple elements.
     
    Last edited: Oct 30, 2013
  4. PotatoInsertion

    PotatoInsertion

    Joined:
    Mar 11, 2013
    Posts:
    23
    Just an update to this. I've been playing around with it for a few hours, and I've figured out one way of getting each element in the Rect array to resize with the screen. It works in pretty much the same way I was doing above, I just discovered how to get into each individual Rect array element.

    This is the window function:

    Code (csharp):
    1. function DisplayBagAInventoryWindow (windowID :int){
    2.    
    3.     for(var index = 0; index < bagSlotA.bagSlots; index++){
    4.    
    5.         inventorySlots[index].width = inventorySlotWidth;
    6.         inventorySlots[index].height = inventorySlotHeight;
    7.        
    8.         inventorySlots[0].x = inventorySlot0X;
    9.         inventorySlots[0].y = inventorySlot0Y;
    10.         inventorySlots[1].x = inventorySlot1X;
    11.         inventorySlots[1].y = inventorySlot1Y;
    12.         inventorySlots[2].x = inventorySlot2X;
    13.         inventorySlots[2].y = inventorySlot2Y;
    14.         inventorySlots[3].x = inventorySlot3X;
    15.         inventorySlots[3].y = inventorySlot3Y;
    16.         inventorySlots[4].x = inventorySlot4X;
    17.         inventorySlots[4].y = inventorySlot4Y;
    18.         // etc, etc
    19.            
    20.         if(GUI.Button(inventorySlots[index],"Slot: " + index)){
    21.    
    22.         }  
    23.     }
    24. }
    And then inside the update function:
    Code (csharp):
    1. inventorySlotWidth = (inventorySlotWidthPercent / 100) * Screen.width;
    2.     inventorySlotHeight = (inventorySlotHeightPercent / 100) * Screen.height;
    3.    
    4.     inventorySlot0X = (inventorySlot0XPercent / 100) * Screen.width;
    5.     inventorySlot0Y = (inventorySlot0YPercent / 100) * Screen.height;
    6.    
    7.     inventorySlot1X = (inventorySlot1XPercent / 100) * Screen.width;
    8.     inventorySlot1Y = (inventorySlot1YPercent / 100) * Screen.height;
    9.    
    10.     inventorySlot2X = (inventorySlot2XPercent / 100) * Screen.width;
    11.     inventorySlot2Y = (inventorySlot2YPercent / 100) * Screen.height;
    12.    
    13.     inventorySlot3X = (inventorySlot3XPercent / 100) * Screen.width;
    14.     inventorySlot3Y = (inventorySlot3YPercent / 100) * Screen.height;
    15.  
    16. //etc, etc
    17.  
    18.  
    This works. I can resize the elements in play mode like I need to and they resize with the screen. But I'm sure you can imagine it's a bit of a mess to code and the inspecter for this .js file is very awkward to read. In the actual .js file the array has 12 rects, so it goes on even longer; I've just deleted most of them here as they're not needed to get the point across.

    Surely there's a better way of doing this, no? That's not massively complicated code wise. Getting things to resize with resolution is something every game needs, I'd be surprised if Unity hasn't a less messy way of doing it. I could probably make it a bit simpler, resize it by rows and columns (many of the X Y values are the same in the end) but it still feels like an awkward workaround as opposed to a solution.