Inspector extension: multiple buttons with for loop?

I want to make a script that extends my inspector.

I want to make lots of buttons with for loop. The buttons are made, but not as I want. They are in a row or column, depends on what do I use, GUILayout.BeginHorizontal() or GUILayout.BeginVertical(). I tried the EditorGUILayout versions too.

What I expected was that it’ll draw my buttons in a row, when it has no space for more, place them to the next row. So, I want them in multiple rows of buttons depends on free space in inspector. That’s all. I’m sure, it’s simple, but I couldn’t figure it out yet. I would be happy with that if it could place every 6th button in the next row.

Wow, that was quick. Or… well, not, but I realised, my logic was flawed. I wanted to use the loop’s variable to add GUILayout.BeginHorizontal() and GUILayout.EndHorizontal() and it looks like, it was a good idea, but I did it in a wrong way. I wanted to use remainder division, and it was also a good idea, but for both begin and end, I used the same result in my condition. That was the flaw. Now, it generates a nice palette of colors based on a texture. I hope, it’ll be helpful for someone.

The loop:

		for(int i=0;i<numOfCol;i++)
		{
			if(i%16==0)
			{
				EditorGUILayout.BeginHorizontal();
			}
			GUI.backgroundColor = palette.colors*;*
  •  	GUILayout.Button("",GUILayout.Width(12),GUILayout.Height(12));*
    
  •  	if(i%16==15)*
    
  •  	{*
    
  •  		//EditorGUILayout.Separator();*
    
  •  		EditorGUILayout.EndHorizontal();*
    
  •  	}*
    
  •  }*
    

Result:
[42426-nespalettesolution.png|42426]*
*