I’m writing an editor for some gamedata in Unity. The editor will be a standalone application. Unfortunately this means that I can only use GUI class, and not EditorGUI.
I’m trying to find a way to use BeginHorizontal/EndHorizontal to dynamically size some area to the amount of content I decide to put in it.
I’d like those areas to have a certain background color, but can’t seem to figure out how to do it.
I’m looking for the effect often seen on websites when they need to list many items, and they switch between two different bg colors on every row, in order to give a bit more visual context on where items begin and end.
I could make an GUI.BeginArea() or maybe a group, but then I need to know the exact coordinates beforehand, which I don’t.
For this effect I use BeginHorizontal/EndHorizontal with different GUIStyles depending on whether the list index is odd or not. Not sure exactly what you’re looking for?
BeginHorizontal takes an optional GUIStyle parameter. Assign something with a texture in the normal state to it and it will get rendered behind the controls inside the horizontal block
I have the same issue as the OP.
I create BeginHorizontal and I can see the GUIStyle parameter, but
I would like to just set Texture2d as different color dynamically created in the code, I fiddled around but I just cannot dynamically create one color texture to use in GUIStyle.normal.background.
Can you help?
One way to create a texture like this is just to fill in all the pixels yourself using the Texture2D.SetPixels:-
function MakeTex(width: int, height: int, col: Color) {
var pix = new Color[width * height];
for (i = 0; i < pix.Length; i++) {
pix[i] = col;
}
var result = new Texture2D(width, height);
result.SetPixels(pix);
result.Apply();
return result;
}
This might be inefficient if you are updating the texture each frame for colour animation but should be no problem if the colour changes infrequently.