Hi,
I have 216 buttons arranged in a color palette. How can I tell which button has been pressed? Is there an event generated? Can I capture it? Thanks
Hi,
I have 216 buttons arranged in a color palette. How can I tell which button has been pressed? Is there an event generated? Can I capture it? Thanks
Assuming you are using the new GUI you can add a listener to the onClick action:
using UnityEngine.UI;
...
// In Start or other initialisation method:
myButton.GetComponent<Button>().onClick.AddListener(() => ColorSelected([value]));
…
private void ColorSelected([argument])
{
// do work
}
Instead of [value] and [argument] insert what you will use to pass the colour value or any other data.
Thanks again. Here’s what I’m trying to do: http://www.bitsong.com/forPosting/Unity/Color%20Cube_Build.html/Color%20Cube_Build.html This is a 3D representation of the RGB color cube. I made it so you can use the mouse and arrow keys to go into it and look around and see how colors relate to each other. I made a palette of 216 colors (partially finished). I would like the user to be able to click on two colors in succession and the cube will then line up along those colors so the user can zoom from one color to the other. You can see this in action if you press the “t” key.
So far I have captured the button presses by putting this code in each button’s if clause: “buttonPressed = true;” Then I put the following code in function Start() and function Update(). Unfortunately, it doesn’t work right. I get buttonA and buttonB both reading true when I press just one button, and I would like them to be true one at a time. There’s something wrong with my logic. Can you help? Thanks.
function Start(){
buttonPressed = false;
buttonA = false;
buttonB = false;
}
function Update(){
if(buttonPressed == true){
buttonA = true;
buttonPressed = false;
}
if(buttonPressed == false && buttonA == true){
buttonB = true;
buttonPressed = false;
}
Debug.Log("buttonPressed: " + buttonPressed);
Debug.Log("buttonA: " + buttonA);
Debug.Log("buttonB: " + buttonB);
}
Think I got it, just needed some conditions: (My apologies for doing my thinking out loud here on Answers)
function Update(){
if(buttonA == false && buttonB == false && buttonPressed == true){
buttonA = true;
buttonPressed = false;
}else if(buttonA == true && buttonB == false && buttonPressed == true){
buttonB = true;
buttonPressed = false;
}