I have a scroll view with lots of buttons on it. when i press one button, that button needs to change the sprite and stay like that. if any other button (or same one) is pressed previous button needs to revert back to original sprite..
here is some example
button 2 was pressed and changed sprite, it stays like that untill it is pressed again or any other (in this case button 3) is pressed
Here's a draft of a possible solution. Create a manager script for the "buttons group" and attach the script on the scrollview
public class ButtonsGroupController : MonoBehaviour
{
// List of all children buttons
private readonly List<Button> _buttons;
// Last pressed button index
private int _lastId = -1;
void Start(){
// Look for all buttons (children of this GO - scrollview)
_buttons = GetComponentsInChildren<Button>();
// Add event listener for buttons
for(var i = 0; i < _buttons.Count; i++){
var index = i;
_buttons[index].onClick.AddListener(() => ButtonPressed(index));
}
}
// Resolve buttons press event
private void ButtonPressed(int index){
// The same button has been pressed
if(_lastId == index)
return;
// Update the last selected button sprite - to normal state
// _buttons[_lastId]....
// Update the last id
_lastId = index;
// Update the sprite of newly pressed button
// _buttons[_lastId]....
}
}
Related
I been working in a Cinema Booking Software for windows, it has about 20 buttons and i can't get the button code of mine to work it only turns to (GREEN) When pressed and there is no function to change it to RED after pressing booking button.
private Button lastButton = null;
private void button57_Click(object sender, EventArgs e)
{
// Change the background color of the button that was clicked
Button current = (Button)sender;
current.BackColor = Color.GreenYellow;
// Revert the background color of the previously-colored button, if any
if (lastButton != null)
lastButton.BackColor = SystemColors.Control;
// Update the previously-colored button
lastButton = current;
}
A problem occurs when you press the same button twice in succession, because then the previous and the current buttons are the same. I would exclude this condition like this:
if (lastButton != null && lastButton != current) {
lastButton.BackColor = SystemColors.Control;
// Update the previously-colored button
lastButton = current;
}
I have a MainButton object with a Button component. A component has a property (or what it is) On Click (). This property can have an object and a method that will be executed by pressing a button. I tried to set these values in the inspector, but these values are not saved in the prefab, because they are assigned from Assets, and not from Scene. How to assign this method and object through programming? Who didn't understood - I need to change properties (object, method) of the event "OnClick()" by the script.
You're looking for the Unity.OnClick UnityEvent.
public class Example : MonoBehaviour
{
//Make sure to attach these Buttons in the Inspector
public Button m_YourFirstButton, m_YourSecondButton, m_YourThirdButton;
void Start()
{
//Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button
m_YourFirstButton.onClick.AddListener(TaskOnClick);
m_YourSecondButton.onClick.AddListener(delegate {TaskWithParameters("Hello"); });
m_YourThirdButton.onClick.AddListener(() => ButtonClicked(42));
m_YourThirdButton.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
//Output this to console when Button1 or Button3 is clicked
Debug.Log("You have clicked the button!");
}
void TaskWithParameters(string message)
{
//Output this to console when the Button2 is clicked
Debug.Log(message);
}
void ButtonClicked(int buttonNo)
{
//Output this to console when the Button3 is clicked
Debug.Log("Button clicked = " + buttonNo);
}
}
In my Unity project I have a InputField that a user can type into and click a button to trigger an action preformed based on what they entered. When I type into the InputField and then press the button, the text that was entered empties before I am able to grab it using inputtedFieldName.text
I have a InputField with the button as a child. The InputField has a component that attaches the script. Attached to the button is a method is called when the button is pressed. It appears when the button is pressed the text that was in the inputField disappears.
What can I do to fix this?
Method the button is calling
public void EnterTextToConsole() {
if (textInputted.text == "" ) {
ConsoleLog.addToConsole("NOTHING??");
} else {
ConsoleLog.addToConsole(textInputted.text);
}
I have a button2 which has a lot of code to execute when is clicked.
At some point, I need to wait until a boolean is set to true, and that boolean is made true when button 1 is pressed. (I can't press button 1 while button2's code is running).
I've searched, but all I found is with methods running async. How can I wait for button 1 to be pressed?
Here's a possible solution (may not be the most elegant):
public partial class Form1 : Form
{
// Variable to store whether button one has been clicked or not
private bool btnOneClicked = false;
// ..
private void btnOne_Click(object sender, EventArgs e)
{
// When button one is clicked, execute the code that needs to run before waiting for your second button to be clicked
LongCodeToExecuteFirst();
// Set your variable to true to say that button one has been clicked
btnOneClicked = true;
}
private void btnTwo_Click(object sender, EventArgs e)
{
// First check if button one has been clicked
if (btnOneClicked)
{
// If it has, execute the rest of the code that needed to be executed after button two was pressed
LongCodeToExecuteSecond();
// Reset our button one pressed variable to false
btnOneClicked = false;
}
}
private void LongCodeToExecuteFirst()
{
// Code to execute before button two is pressed
}
private void LongCodeToExecuteSecond()
{
// Code to execute after button two is pressed
}
}
private void Page_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Right)
{
if (shipPosition < right)
{
x = x + 10;
shipPosition = shipPosition + 10;
ship.Margin = new Thickness(shipPosition, y, 0, 0);
}
}
}
XAML - KeyDown="Page_KeyDown"
I have a rectangle named ship, and above is the function to move it, to be executed on right or left arrow key button press. For some reason, this doesn't work. The "KeyDown="Page_KeyDown"" is the xaml that links the button press with the event. There is another grid containing buttons that moves every time a timer ticks, and when I click on a button and highlight it, then press the arrow keys, the ship starts to move, with the highlighted button being changed as well. Does anyone have any idea what is going on?
Update: I removed the is focusable property on the buttons, and that then stopped ship movement all together. So I think it is something to do with the changing of focus on buttons.
Try this :
Keyboard.AddKeyDownHandler(this, Page_KeyDown);
Put it in the appropriate method of your control (constructor, initialized, ...)
public MainWindow()
{
InitializeComponent();
Keyboard.AddKeyDownHandler(this, Page_KeyDown);
}