116 lines
3.2 KiB
C#
116 lines
3.2 KiB
C#
using asap.core;
|
|
using game;
|
|
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UniRx;
|
|
using TMPro;
|
|
using GameCore;
|
|
|
|
public class AdvertButton : MonoBehaviour
|
|
{
|
|
public Button advBtn;
|
|
public Button adticketBtn;
|
|
public TMP_Text adticket_text;
|
|
public Button advBtnGray;
|
|
public Button adticketBtnGray;
|
|
public TMP_Text adticket_text_gray;
|
|
private IDisposable ads;
|
|
private AdvertPopupType curOpenAdType = AdvertPopupType.None;
|
|
private Action OnBuySuccess;
|
|
private IDisposable disposable;
|
|
int quantityRequired = 1;
|
|
|
|
bool isGray = false;
|
|
//private void Awake()
|
|
//{
|
|
// advBtn = transform.Find("btn_green1").GetComponent<Button>();
|
|
// adticketBtn = transform.Find("btn_green2").GetComponent<Button>();
|
|
//}
|
|
private void Start()
|
|
{
|
|
advBtn.onClick.AddListener(OnClickAds);
|
|
adticketBtn.onClick.AddListener(OnClickCoupon);
|
|
}
|
|
public void SetData(Action onBuySuccess, AdvertPopupType openAdType)
|
|
{
|
|
this.OnBuySuccess = onBuySuccess;
|
|
this.curOpenAdType = openAdType;
|
|
}
|
|
private void OnEnable()
|
|
{
|
|
disposable?.Dispose();
|
|
OnAdticketChangeEvent();
|
|
disposable = GContext.OnEvent<ResAddEvent>().Where(x => x.id == 1006).Subscribe(x =>
|
|
{
|
|
OnAdticketChangeEvent();
|
|
});
|
|
}
|
|
void OnAdticketChangeEvent()
|
|
{
|
|
if (isGray) { return; }
|
|
int count = GContext.container.Resolve<PlayerShopData>().GetAdticketCount();
|
|
advBtn.gameObject.SetActive(count < quantityRequired);
|
|
adticketBtn.gameObject.SetActive(count >= quantityRequired);
|
|
adticket_text.text = $"{count}/{quantityRequired}";
|
|
}
|
|
public void SetGray()
|
|
{
|
|
int count = GContext.container.Resolve<PlayerShopData>().GetAdticketCount();
|
|
advBtn.gameObject.SetActive(false);
|
|
adticketBtn.gameObject.SetActive(false);
|
|
if (advBtnGray != null)
|
|
{
|
|
advBtnGray.gameObject.SetActive(count < quantityRequired);
|
|
}
|
|
if (adticketBtnGray != null)
|
|
{
|
|
adticketBtnGray.gameObject.SetActive(count >= quantityRequired);
|
|
adticket_text_gray.text = $"{count}/{quantityRequired}";
|
|
}
|
|
}
|
|
void OnClickCoupon()
|
|
{
|
|
if (OnBuySuccess == null)
|
|
{
|
|
return;
|
|
}
|
|
//消耗广告券 quantityRequired
|
|
GContext.container.Resolve<PlayerShopData>().AddAdticket(-quantityRequired);
|
|
OnBuySuccess?.Invoke();
|
|
}
|
|
void OnClickAds()
|
|
{
|
|
if (OnBuySuccess == null)
|
|
{
|
|
return;
|
|
}
|
|
advBtn.enabled = false;
|
|
//拉起广告
|
|
AdsDispose();
|
|
ads = GContext.OnEvent<AdsResult>().Subscribe(SetResult);
|
|
GContext.container.Resolve<IAdsService>().ShowRewarded(curOpenAdType);
|
|
}
|
|
void SetResult(AdsResult adsResult)
|
|
{
|
|
AdsDispose();
|
|
advBtn.enabled = true;
|
|
if (adsResult.Result == 0 && adsResult.Type == curOpenAdType)
|
|
{
|
|
//广告成功
|
|
OnBuySuccess?.Invoke();
|
|
}
|
|
}
|
|
void AdsDispose()
|
|
{
|
|
ads?.Dispose();
|
|
ads = null;
|
|
}
|
|
private void OnDisable()
|
|
{
|
|
disposable?.Dispose();
|
|
disposable = null;
|
|
AdsDispose();
|
|
}
|
|
}
|