Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Unity 5.2中更改scrollview中的按钮精灵_C#_User Interface_Button_Unity3d - Fatal编程技术网

C# 在Unity 5.2中更改scrollview中的按钮精灵

C# 在Unity 5.2中更改scrollview中的按钮精灵,c#,user-interface,button,unity3d,C#,User Interface,Button,Unity3d,我有一个滚动视图,上面有很多按钮。当我按下一个按钮时,该按钮需要更改精灵并保持不变。如果按下任何其他按钮(或相同的按钮),则上一个按钮需要恢复为原始精灵。。 这里有一些例子 按下并更改了按钮2 sprite,直到再次按下按钮或按下任何其他按钮(在本例中为按钮3)之前,它一直保持此状态,这是一个可能的解决方案草案。为“按钮组”创建管理器脚本,并将脚本附加到scrollview上 public class ButtonsGroupController : MonoBehaviour { /

我有一个滚动视图,上面有很多按钮。当我按下一个按钮时,该按钮需要更改精灵并保持不变。如果按下任何其他按钮(或相同的按钮),则上一个按钮需要恢复为原始精灵。。 这里有一些例子


按下并更改了按钮2 sprite,直到再次按下按钮或按下任何其他按钮(在本例中为按钮3)之前,它一直保持此状态,这是一个可能的解决方案草案。为“按钮组”创建管理器脚本,并将脚本附加到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]....
    }
}
公共类按钮GroupController:MonoBehavior
{
//所有子按钮的列表
专用只读列表按钮;
//上次按下的按钮索引
private int_lastId=-1;
void Start(){
//查找所有按钮(此GO-Scroll视图的子项)
_buttons=GetComponentsInChildren();
//为按钮添加事件侦听器
对于(变量i=0;i<\u buttons.Count;i++){
var指数=i;
_按钮[index].onClick.AddListener(()=>ButtonPressed(index));
}
}
//解决按钮按下事件
私有无效按钮按下(整数索引){
//同样的按钮也被按下了
如果(_lastId==索引)
返回;
//将上次选择的按钮精灵更新为正常状态
//_按钮[_lastId]。。。。
//更新最后一个id
_lastId=索引;
//更新新按下按钮的精灵
//_按钮[_lastId]。。。。
}
}

这里这么多盐可能重复,不,这不是问题,请阅读。。我不需要一个按钮来更改精灵,我需要一组按钮,并且需要它们先保持更改状态,直到再次按下或按下同一组中的任何其他按钮。我使用“切换组”和“切换”解决了此问题。这真的很简单,而且用途广泛。。