备份CatanBuilding瘦身独立工程
This commit is contained in:
147
Assets/Scripts/UI/Advert/AdvertGiftPanel.cs
Normal file
147
Assets/Scripts/UI/Advert/AdvertGiftPanel.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using asap.core;
|
||||
using cfg;
|
||||
using game;
|
||||
using GameCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UniRx;
|
||||
|
||||
public class AdvertGiftPanel : MonoBehaviour
|
||||
{
|
||||
Tables _tables;
|
||||
PlayerShopData shopData;
|
||||
TMP_Text text_time;
|
||||
AdvertGiftItem[] advertGiftItems;
|
||||
Timer timer;
|
||||
private IDisposable ads;
|
||||
AdConfig curAdConfig;
|
||||
AdvertGiftItem advertGiftItem;
|
||||
private AdvertPopupType _curOpenAdType = AdvertPopupType.None;
|
||||
private void Awake()
|
||||
{
|
||||
_tables = GContext.container.Resolve<Tables>();
|
||||
shopData = GContext.container.Resolve<PlayerShopData>();
|
||||
text_time = transform.Find("title2/bg_time/text_info").GetComponent<TMP_Text>();
|
||||
advertGiftItems = transform.GetComponentsInChildren<AdvertGiftItem>();
|
||||
}
|
||||
protected void Start()
|
||||
{
|
||||
Init();
|
||||
StartTimer();
|
||||
}
|
||||
|
||||
void Init()
|
||||
{
|
||||
var DataList = _tables.TbAdConfig.DataList;
|
||||
int viplv = GContext.container.Resolve<PlayerData>().vip;
|
||||
for (int i = 0; i < advertGiftItems.Length; i++)
|
||||
{
|
||||
AdConfig adConfig = DataList[i];
|
||||
var drop = _tables.TbDrop.GetOrDefault(adConfig.Reward);
|
||||
var itemDropPackage = _tables.TbItemDropPackage.GetOrDefault(adConfig.Reward);
|
||||
advertGiftItems[i].adConfig = adConfig;
|
||||
advertGiftItems[i].vipFree = viplv >= adConfig.VipFree;
|
||||
advertGiftItems[i].btn_watch.SetData(() =>
|
||||
{
|
||||
shopData.AddAdvertCount(curAdConfig.Reward);
|
||||
//广告成功
|
||||
OnBuySuccess();
|
||||
}, AdvertPopupType.Shop);
|
||||
advertGiftItems[i].btn_free.onClick.AddListener(OnBuySuccess);
|
||||
|
||||
if (itemDropPackage.ItemDisplay.Count > 0)
|
||||
{
|
||||
List<string> CountDisplay = GContext.container.Resolve<PlayerItemData>().ShowItemDropPackage(itemDropPackage);
|
||||
advertGiftItems[i].rewardItem1.SetData(itemDropPackage.ItemDisplay[0], CountDisplay[0], true);
|
||||
advertGiftItems[i].rewardItem2.SetData(itemDropPackage.ItemDisplay[1], CountDisplay[1], true);
|
||||
}
|
||||
}
|
||||
}
|
||||
async void ReStart()
|
||||
{
|
||||
await Awaiters.Seconds(0.5f);
|
||||
StartTimer();
|
||||
}
|
||||
void SetState()
|
||||
{
|
||||
shopData.ClearAdvertCount();
|
||||
int adCount = shopData.VIPAdPackData.index;
|
||||
for (int i = 0; i < advertGiftItems.Length; i++)
|
||||
{
|
||||
advertGiftItems[i].current.SetActive(i == adCount);
|
||||
advertGiftItems[i].done.SetActive(i < adCount);
|
||||
advertGiftItems[i].unlock.SetActive(i > adCount);
|
||||
advertGiftItems[i].unlock2.SetActive(i > adCount);
|
||||
|
||||
if (i == adCount)
|
||||
{
|
||||
advertGiftItem = advertGiftItems[i];
|
||||
advertGiftItem.btn_free.gameObject.SetActive(advertGiftItem.vipFree);
|
||||
advertGiftItem.btn_watch.gameObject.SetActive(!advertGiftItem.vipFree);
|
||||
curAdConfig = advertGiftItems[i].adConfig;
|
||||
}
|
||||
}
|
||||
}
|
||||
void StartTimer()
|
||||
{
|
||||
if (timer != null)
|
||||
{
|
||||
timer.Cancel();
|
||||
timer = null;
|
||||
}
|
||||
var newTime = ZZTimeHelper.UtcNow().UtcNowOffset().Date.AddDays(1);
|
||||
TimeSpan timeSpan = newTime - ZZTimeHelper.UtcNow().UtcNowOffset();
|
||||
SetState();
|
||||
timer = this.AttachTimer((float)timeSpan.TotalSeconds, ReStart, (elapsed) =>
|
||||
{
|
||||
TimeSpan now = newTime - ZZTimeHelper.UtcNow().UtcNowOffset();
|
||||
text_time.text = LocalizationMgr.GetFormatTextValue("UI_COMMON_refresh", $"<size=40>{ConvertTools.ConvertTime2(now)}</size>");
|
||||
}, useRealTime: true);
|
||||
}
|
||||
void OnClickGet()
|
||||
{
|
||||
advertGiftItem.btn_watch.enabled = false;
|
||||
//拉起广告
|
||||
AdsDispose();
|
||||
ads = GContext.OnEvent<AdsResult>().Subscribe(SetResult);
|
||||
_curOpenAdType = AdvertPopupType.Shop;
|
||||
GContext.container.Resolve<IAdsService>().ShowRewarded(_curOpenAdType);
|
||||
}
|
||||
void SetResult(AdsResult adsResult)
|
||||
{
|
||||
AdsDispose();
|
||||
advertGiftItem.btn_watch.enabled = true;
|
||||
if (adsResult.Result == 0 && adsResult.Type == AdvertPopupType.Shop)
|
||||
{
|
||||
shopData.AddAdvertCount(curAdConfig.Reward);
|
||||
//广告成功
|
||||
OnBuySuccess();
|
||||
}
|
||||
}
|
||||
void AdsDispose()
|
||||
{
|
||||
if (ads != null)
|
||||
{
|
||||
_curOpenAdType = AdvertPopupType.None;
|
||||
ads.Dispose();
|
||||
ads = null;
|
||||
}
|
||||
}
|
||||
private void OnBuySuccess()
|
||||
{
|
||||
GContext.container.Resolve<PlayerItemData>().AddItemByDrop(curAdConfig.Reward);
|
||||
shopData.SetVIPPackNext();
|
||||
GContext.Publish(new ShowData());
|
||||
SetState();
|
||||
shopData.SetAdvRedPoint();
|
||||
}
|
||||
protected void OnDestroy()
|
||||
{
|
||||
if (timer != null)
|
||||
{
|
||||
timer.Cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user