87 lines
2.1 KiB
C#
87 lines
2.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class RewardButton : MonoBehaviour
|
|
{
|
|
[SerializeField] protected Button _button;
|
|
[SerializeField] private Text _takeMessage;
|
|
[SerializeField] private Text _loadingMessage;
|
|
[SerializeField] private Image _statusIcon;
|
|
[Header("Prefabs")]
|
|
[SerializeField] private Sprite _loadingSprite;
|
|
[SerializeField] private Sprite _readySprite;
|
|
private Action<bool> _rewardCallback;
|
|
|
|
private string _placement = "non";
|
|
|
|
private void Awake()
|
|
{
|
|
_button.onClick.AddListener(ClickHandler);
|
|
Init();
|
|
}
|
|
|
|
protected virtual void Init() { }
|
|
|
|
public void SetPlacement(string placement)
|
|
{
|
|
_placement = placement;
|
|
}
|
|
|
|
public void SetHandler(Action<bool> handler)
|
|
{
|
|
_rewardCallback = handler;
|
|
}
|
|
|
|
public virtual void Begin()
|
|
{
|
|
StartCoroutine(RewardRoutine());
|
|
}
|
|
|
|
private IEnumerator RewardRoutine()
|
|
{
|
|
_takeMessage.gameObject.Hide();
|
|
_loadingMessage.gameObject.Show();
|
|
yield return ADsLoadingRoutine();
|
|
_takeMessage.gameObject.Show();
|
|
_loadingMessage.gameObject.Hide();
|
|
OnRewardLoaded();
|
|
}
|
|
|
|
protected virtual void OnRewardLoaded()
|
|
{
|
|
|
|
}
|
|
|
|
private IEnumerator ADsLoadingRoutine()
|
|
{
|
|
_button.gameObject.Show();
|
|
|
|
do
|
|
{
|
|
bool rewardAvailable = Ads.HasRewardVideo();
|
|
_button.interactable = rewardAvailable;
|
|
_statusIcon.sprite = rewardAvailable ? _readySprite : _loadingSprite;
|
|
yield return null;
|
|
}
|
|
while (_button.interactable == false);
|
|
}
|
|
|
|
private void ClickHandler()
|
|
{
|
|
Ads.ShowReward(
|
|
_placement,
|
|
() =>
|
|
{
|
|
_button.gameObject.Hide();
|
|
_rewardCallback.Invoke(true);
|
|
},
|
|
null,
|
|
() =>
|
|
{
|
|
_rewardCallback.Invoke(false);
|
|
});
|
|
}
|
|
}
|