0

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

enter image description here

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

user3071284
  • 6,955
  • 6
  • 43
  • 57
  • http://answers.unity3d.com/questions/967366/how-to-change-button-image-on-new-ui.html – Fattie Jan 21 '16 at 03:18
  • Possible duplicate of [4.6 New UI How to change Button Image?](http://stackoverflow.com/questions/27761021/4-6-new-ui-how-to-change-button-image) – Fattie Jan 21 '16 at 03:19
  • So much salt here, no its not that question, please read it.. I dont need 1 button to change sprite, i need group of them, and need them to first stay changed untill pressed again or any other button from same group was pressed – Srdjan Savic Jan 21 '16 at 06:59
  • I solved the problem using toggle group and toggles. Its really easy and very versatile.. – Srdjan Savic Jan 23 '16 at 10:45

1 Answers1

0

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]....
    }
}
Fattie
  • 27,874
  • 70
  • 431
  • 719
David Horák
  • 5,535
  • 10
  • 53
  • 77