I have a 4x4 grid of buttons. I need to cycle through and display (the same) three visual states when a button is tapped …
Not tapped = no texture seen
Tapped once = texture A
Tapped a second time = texture B
Tapped a third time = back to no texture seen
All 16 buttons use the same textures A and B.
Can someone tell me the best way to do that programmatically? Thank you!
var buttonTextures : Texture2D[];
var rows : int = 4;
var columns : int = 4;
private var buttonStates : int[];
function Start () {
buttonStates = new int[rows * collumns];
for(i=0; i<buttonStates.Length; i++) {
buttonStates[i] = -1;
}
}
function OnGUI () {
GUILayout.BeginVertical();
for(i=0; i<rows; i++) {
GUILayout.BeginHorizontal();
for(j=0; j<columns; j++) {
if(buttonsStates[i*columns + j] > -1) {
if(GUILayout.Button(buttonTextures[buttonsStates[i*columns + j]]) {
buttonsStates[i*columns + j]++;
if(buttonsStates[i*columns + j] >= buttonTextures.Length)
buttonsStates[i*columns + j] = -1;
}
} else {
if(GUILayout.Button("") {
buttonsStates[i*columns + j]++;
if(buttonsStates[i*columns + j] >= buttonTextures.Length)
buttonsStates[i*columns + j] = -1;
}
}
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
This was hastily typed up, so you might need to tweak it into working. The whole buttonStates = -1 thing might be able to be simplified if GUILayout.Button(null) works. If so, you could use a texture array with the first entry empty. I haven’t tested it myself. Hope this gets you started!
var buttonTextures : Texture2D[];
var rows : int = 4;
var columns : int = 4;
private var buttonStates : int[];
function Start () {
buttonStates = new int[rows * collumns];
for(i=0; i<buttonStates.Length; i++) {
buttonStates[i] = -1;
}
}
function OnGUI () {
GUILayout.BeginVertical();
for(i=0; i<rows; i++) {
GUILayout.BeginHorizontal();
for(j=0; j<columns; j++) {
if(buttonsStates[i*columns + j] > -1) {
if(GUILayout.Button(buttonTextures[buttonsStates[i*columns + j]]) {
buttonsStates[i*columns + j]++;
if(buttonsStates[i*columns + j] >= buttonTextures.Length)
buttonsStates[i*columns + j] = -1;
}
} else {
if(GUILayout.Button("") {
buttonsStates[i*columns + j]++;
if(buttonsStates[i*columns + j] >= buttonTextures.Length)
buttonsStates[i*columns + j] = -1;
}
}
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
This was hastily typed up, so you might need to tweak it into working. The whole buttonStates = -1 thing might be able to be simplified if GUILayout.Button(null) works. If so, you could use a texture array with the first entry empty. I haven’t tested it myself. Hope this gets you started!
It will, thanks! I usually just need a push in the right direction and then can hack things to make them work to actually understand them.