- Home /
GUI 2D Array
Hi, I want to create a grid of gui boxes (or labels) and control the properties of each box or label. I defined :
public GUIStyle[,] st=null;
and in Start() I have:
st = new GUIStyle[rows,columns];
for (int i=0;i<rows;i++){
for (int j=0;j<columns;j++){
stocksValue[i,j]=rnd.Next(1,10000);
st[i,j]=new GUIStyle();
st[i,j].fontSize=50;
}
}
and in OnGui() I have:
for (int i=0; i<rows; i++) {
for (int j=0; j<columns; j++) {
GUI.Label (new Rect (boxWidth*j, boxHeight*i, boxWidth, boxHeight), new GUIContent (""+conTstr[i,j]),st[i,j]);
}
}
now, I need your help for adding border on box and I want to change background color of each box by code (based on random number e.g. if rnd < 10 background box color=red)
Thanks
Comment
Answer by sys12 · Sep 18, 2015 at 12:51 PM
You can do both by utilizing the GUIStyle values.
http://docs.unity3d.com/ScriptReference/GUIStyle-border.html
You can make a texture with border and use it as the background of your image .
For example, the background colors:
style.normal.background = MakeTex(2, 2, new Color(0.75f, 0.75f, 0.75f))
public Texture2D MakeTex(int width, int height, Color col)
{
Color[] pix = new Color[width * height];
for (int i = 0; i < pix.Length; ++i)
{
pix[i] = col;
}
Texture2D result = new Texture2D(width, height);
result.SetPixels(pix);
result.Apply();
return result;
}
The example texture doesn't have any borders.