List of buttons

I need a little help figuring out how exactly I can setup a basic group of buttons. Right now they spawn ontop of one another and was testing out some GUILayouts and such but with no luck. Trying to create a 6 x 10 button grid with a scrollview as well. I guess I just have no idea how to space everything out depending on how many objects are in my list.

function OnGUI () {

	for(var x = 0; x < myList.Count; x++) {
		 if (GUI.Button(Rect(Screen.width/2,Screen.height/2,64,64), GUIContent (myList[x]._icon, "test ")) {
		 		 
		 }
				         	
	}
}

1 Answer

1

So something like this:

  var numberOfButtonsWide = 6;
  var topLeft = Vector2(100,100);
  var width = 800;
  var buttonHeight = 60;

  function DrawButtons()
  {
     var buttonWidth = width/numberOfButtonsWide;
     for(var x =0; x < myList.Count; x++)
     {
          if(GUI.Button(
               Rect(topLeft.x + (x % numberOfButtonsWide) * buttonWidth, 
                      topLeft.y + Mathf.FloorToInt(x / numberOfButtonsWide) * buttonHeight, 
                      buttonWidth, 
                      buttonHeight),
               GUIContent (myList[x]._icon, "test ")))
          {
          }
     }
  }

Didn't seem to work. Is there anyway to do something like this. function OnGUI () { for(var x = 0; x < myList.Count && x < 6; x++) { for(var z = 0; z < myList.Count; z++) { if (GUI.Button(Rect(Screen.width/2 + (64 * z),Screen.height/2 + (64 * x),64,64), GUIContent (myList[x]._icon, "test ")) { } } } } The only problem is that it creates rows of the same button acting as one button. So how would I set the Count limit for each row. Like a max of 6 instead of creating extra non functioning buttons?

var counter = 0; for(var x = 0; x < 6 && counter < myList.Count; x++) { for(var z = 0; z < 10; z++) { if (GUI.Button(Rect(Screen.width/2 + (64 * z),Screen.height/2 + (64 * x),64,64), GUIContent (myList[x]._icon, "test ")) { counter++; if(counter >= myList.Count) break; } } }

I tried that out but with no luck. It just creates a giant grid of buttons even if there's only 10 items

No it doesn't if I'd put in the right closing brace "}"

var counter = 0; for(var x = 0; x < 6 && counter < myList.Count; x++) { for(var z = 0; z < 10; z++) { if (GUI.Button(Rect(Screen.width/2 + (64 * z),Screen.height/2 + (64 * x),64,64), GUIContent (myList[x]._icon, "test "))) { //Do something } counter++; if(counter >= myList.Count) break; } } }