Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Generate Level Select Menu

Discussion in 'Immediate Mode GUI (IMGUI)' started by xKroniK13x, Jul 11, 2012.

  1. xKroniK13x

    xKroniK13x

    Joined:
    Jun 19, 2012
    Posts:
    164
    Hey guys, I'm trying to make a level select menu that is easily editable. I have it working in one column, but I want to be able to make it expand over a number of columns because I have a lot of levels.

    This is what I have so far...
    Code (csharp):
    1.  
    2. var numberOfLevels : int;
    3. private var levelChoice : String;
    4.  
    5. function levelList() {
    6.     for(var n = 1; n < numberOfLevels; n++){
    7.         var height = 35 * n;
    8.         if (GUI.Button (Rect (Screen.width/2-50, 100 + height, 100, 30), "Level " + n)) {
    9.             levelChoice = "Level" + n;
    10.             loadLevel();
    11.         }
    12.     }
    13. }
    14.  
     
  2. ModStoryGames

    ModStoryGames

    Joined:
    Apr 27, 2012
    Posts:
    179
    Code (csharp):
    1.  
    2.             // For this example, we are using an array of GUIContent items.
    3.             GUIContent[] items = new GUIContent[100];
    4.             // We must create an index variable to track our location in the GUIContent array.
    5.             int index = 1;
    6.             // We also need to know how many items can appear in one row.
    7.             int xCount = 10;
    8.             // We use a while loop in case there are no items to draw.
    9.             while (index < items.Length)
    10.             {
    11.                 // Begin a Row
    12.                 GUILayout.BeginHorizontal();
    13.                 // Use a for loop to create the row contents
    14.                 for (int x = 0; x < xCount; x++)
    15.                 {
    16.                     // If the index is greater than the array size,
    17.                     if (index >= items.Length)
    18.                         // end the for loop.
    19.                         break;
    20.                     // Otherwise, create a button for the item at the current index,
    21.                     // and increase the index by one.
    22.                     GUILayout.Button(items[index++]);
    23.                 }
    24.                 // End a Row
    25.                 GUILayout.EndHorizontal();
    26.             }
    27.         }
    28.  
    This is how I create a multicolumn list of controls. You can put your while loop inside of an Area you define with a Rect.
     
  3. xKroniK13x

    xKroniK13x

    Joined:
    Jun 19, 2012
    Posts:
    164
    I'll give it a try! Thanks!
     
  4. xKroniK13x

    xKroniK13x

    Joined:
    Jun 19, 2012
    Posts:
    164
    I came up with this code which works pretty well.
    Code (csharp):
    1.  
    2.     for(var n=1; n < maxAllowedLevel;){
    3.         for(var i=0; i<nomRows; i++){
    4.             for(var j=0;j<nomCols; j++){
    5.                 GUI.enabled = n <= maxAllowedLevel;
    6.                 if(GUI.Button(new Rect(Screen.width/2 - screenAdjust + i*80,j*30 + 150,buttonWidth,25),"Level " + n)){
    7.                     //Load level N
    8.                 }
    9.             n++;
    10.             }  
    11.         }
    12.     }
    13.  
     
  5. Paulo-Henrique025

    Paulo-Henrique025

    Joined:
    Dec 12, 2010
    Posts:
    230
    Thats basic Matrix handling =D
     
  6. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    i did something similar which is like a grid of small buttons, and it makes an object the with and height of the button click's width and height, for example 4th one up in first row will make the object 4 heigh 1 wide

    Code (csharp):
    1. //GRID SELECTOR
    2.  
    3.     for (var b7:int = 0; b7 < 36; b7 ++)    {
    4.     var sz = 12;
    5.     if (GUI.Button (Rect(b7*12% 72+240,Mathf.Floor(-b7*12/72)*12+72,12,12),GUIContent("b7","fast selection for digger size")))
    6.         {
    7.             Wt=(b7%6)+1;   Ht=(Mathf.Floor(b7/6))+1;
    8.         }//+++++++++++++++++++++++++++++++++++++++++  
    9.     }  
    10.