2024-12-18 11:12:52 +08:00

176 lines
4.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TTSDK;
using TMPro;
using UnityEngine.UI;
using TTSDK.UNBridgeLib.LitJson;
using System;
/// <summary>
/// 抖音侧边栏
/// </summary>
public class Sidebar : MonoBehaviour
{
/// <summary>
/// 侧边栏领取奖励按钮
/// </summary>
private Button receiveBtn;
/// <summary>
/// 侧边栏关闭按钮
/// </summary>
private Button closeBtn;
/// <summary>
/// 是否可以领取奖励
/// </summary>
private bool canReceiveReward = false;
void OnEnable()
{
receiveBtn = transform.Find("Sidebar/ReceiveBtn").GetComponent<Button>();
receiveBtn.onClick.AddListener(OnReceiveBtnClick);
closeBtn = transform.Find("Sidebar/CloseBtn").GetComponent<Button>();
closeBtn.onClick.AddListener(OnCloseBtnClick);
//如果今天没有领取过奖励,则可以领取
if(DataManager.NextDay("Sidebar")){
//如果是从侧边栏进入的,则可以领取奖励
if (DYSidebarInit.isEnterFromSidebar)
{
receiveBtn.transform.Find("Text").GetComponent<Text>().text = "领取奖励";
receiveBtn.interactable = true;
}
//如果不是从侧边栏进入的,则需要打开侧边栏
else
{
receiveBtn.transform.Find("Text").GetComponent<Text>().text = "打开侧边栏";
receiveBtn.interactable = true;
}
}
//如果今天已经领取过奖励,则不能领取
else
{
receiveBtn.interactable = false;
receiveBtn.transform.Find("Text").GetComponent<Text>().text = "已领取";
}
}
/// <summary>
/// 侧边栏领取奖励按钮点击事件
/// </summary>
private void OnReceiveBtnClick()
{
//如果是从侧边栏进入的,则领取奖励
if (DYSidebarInit.isEnterFromSidebar)
{
//TODO:领取奖励逻辑
//记录领取奖励时间
SetReceiveTime();
receiveBtn.interactable = false;
receiveBtn.transform.Find("Text").GetComponent<Text>().text = "已领取";
DataManager.SetPhysical(5);
DataManager.GetTime("Sidebar");
}
//如果不是从侧边栏进入的,则打开侧边栏
else
{
JsonData data = new JsonData
{
["scene"] = "sidebar",
};
TT.NavigateToScene(data,
() =>
{
BoxDebug.Log("打开侧边栏成功");
},
() =>
{
gameObject.SetActive(false);
},
(errCode, errMsg) =>
{
BoxDebug.Log($"打开侧边栏失败, errCode:{errCode}, errMsg:{errMsg}");
});
}
}
/// <summary>
/// 判断当前时间是否可以领取奖励
/// </summary>
/// <returns></returns>
private bool ThisTimeCanReceiveReward()
{
if (HasDate())
{
string receiveTime = LoadDate();
DateTime toDay = DateTime.Today;
int year = toDay.Year;
int month = toDay.Month;
int day = toDay.Day;
string nowTime = year + "-" + month + "-" + day;
//如果今天已经领取过奖励,则不能领取
if (nowTime == receiveTime)
{
return false;
}
else
{
return true;
}
}
return true;
}
/// <summary>
/// 设置领取奖励时间
/// </summary>
private void SetReceiveTime()
{
DateTime toDay = DateTime.Today;
int year = toDay.Year;
int month = toDay.Month;
int day = toDay.Day;
SaveDate(year + "-" + month + "-" + day);
}
/// <summary>
/// 侧边栏关闭按钮点击事件
/// </summary>
private void OnCloseBtnClick()
{
gameObject.SetActive(false);
}
/// <summary>
/// 保存日期广告
/// </summary>
private void SaveDate(string data)
{
TT.PlayerPrefs.SetString(Application.productName + "_Date", data);
}
/// <summary>
/// 加载日期广告
/// </summary>
private string LoadDate()
{
return TT.PlayerPrefs.GetString(Application.productName + "_Date");
}
/// <summary>
/// 判断是否存储了日期数据
/// </summary>
private bool HasDate()
{
return TT.PlayerPrefs.HasKey(Application.productName + "_Date");
}
}