Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using AnimalsSimulator.Project;
- using TMPro;
- using UniRx;
- using UnityEngine;
- using UnityEngine.UI;
- using Zenject;
- namespace AnimalsSimulator.Common.UI
- {
- [RequireComponent(typeof(Button))]
- public class BuyButton : MonoBehaviour
- {
- [SerializeField] private TextMeshProUGUI _priceText;
- [SerializeField] private Button _button;
- [Inject] private PurchaseService _purchaseService;
- private CompositeDisposable _disposable;
- private void OnValidate() => _button ??= GetComponent<Button>();
- public IObservable<Unit> BindOnClick(IReadOnlyReactiveProperty<int> price)
- {
- _disposable?.Dispose();
- _disposable = new CompositeDisposable();
- price.Subscribe(SetPrice)
- .AddTo(_disposable);
- var reactiveCommand = price.Select(_purchaseService.CanBuy)
- .Merge(_purchaseService.Money.Select(CanBuy))
- .ToReactiveCommand();
- reactiveCommand.BindToOnClick(_button, _ => _purchaseService.Purchase(price.Value))
- .AddTo(_disposable);
- return reactiveCommand;
- bool CanBuy(int money) =>
- price.Value <= money;
- }
- private void SetPrice(int price)
- {
- var text = price.ToString();
- _priceText.SetText(text);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement