Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Generate Level Select Menu

Discussion in 'Extensions & OnGUI' 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. EddyEpic

    EddyEpic

    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:
    222
    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.