To use Reactive Extension to find out how many buttons in the button list have been pressed

Asked 1 years ago, Updated 1 years ago, 73 views


using UniRx (Reactive Extension for Unity) I'm trying to create a screen that looks like a card list.

If it's an individual button, you can write a pair of actions with the button, so
If there is a click notification, go to Subscribe.You can write things like
In the case of a list structure, just by pressing a button, which index button is
I don't know if it's pressed.

I think I can use ObeservableCollection with a newer version of .net.
It was not available in Unity.


because it is created with MVP pattern The data exchange is done only by the Model side
The View side is responsible for events and actions
The interaction between Model and View will be written to Presenter.

So, which index button was pressed
I would like to write to View side, but I don't know how to relate it.

Could you tell me how to think and sample sources?

Note: I tried to search myself using IndexOf, but
   I don't know how to hand myself over in the Lambda style.
   I want to pass IObservable <Unit> instead of IObservable <Unit> units

//<summary>
/// List of Buttons
/// </summary>
publicList<Button>ButtonList=newList<Button>();
/// <summary>
/// button event notification list
/// </summary>
List <IObservable<Unit>>abc=new List<IObservable<Unit>>();
/// <summary>
/// Hash List for Button Notifications
/// </summary>
List<int>abcHash=new List<int>();

void Start() 
{
    abc.Add (ButtonList[0].OnClickAsObservable());
    abcHash.Add(abc[0].GetHashCode());

    abc.Add (ButtonList[1].OnClickAsObservable());
    abcHash.Add(abc[1].GetHashCode());

    abc.Add (ButtonList[2].OnClickAsObservable());
    abcHash.Add(abc[2].GetHashCode());

    abc.Add (ButtonList[3].OnClickAsObservable());
    abcHash.Add(abc[3].GetHashCode());

    foreverach (IObserviceable<Unit>test in abc)
    {
        test.Subscribe(
            _=>ButtonClicked(abcHash.IndexOf(I want to get my own hash value)))
            );
    }
}

private void ButtonClicked (int index)
{
    Debug.Log("index="+index.ToString()));
}

c# unity3d

2022-09-30 19:30

2 Answers

You can refer to the lambda expression by substituting a value of i for the variables in each loop.

 for (inti=0;i<abc.Count;i++)
{
    int n = i; // substitutes variables in the loop
    abc[i].Subscribe(
        _=>ButtonClicked(n)
        );

}

The scope of i is outside each loop, so the value will eventually be abc.Count, which is not the behavior you intended to reference.


2022-09-30 19:30

What does View hand over to Presenter?
List<Button>?
List<IObserviceable<Unit>>?

If you want to pass it by List<Button> because the code is public,

List<Button>ButtonList=newList<Button>() {newButton(), newButton(), newButton(), newButton();;

//------

foreach (var x in buttonList)
{
    x.OnClickAsObservable().Subscribe(_=>ButtonClicked(buttonList.IndexOf(x)));
}

buttonList[0].Click();//->index=0
buttonList[1].Click();//->index=1
buttonList[2].Click();//->index=2

List<IObserviceable<Unit>> if you want to pass it
"In the first place, I would like to notify you of ""what button was pressed"", so
" I think it's better to use IObservable<int>.


2022-09-30 19:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.